crawler bot - need table and estimate routine to go with it.
Posted: Sat 5. Jun 2010, 20:07
ok guys... i need some help.
i need to create a table w/ ranges and corresponding voltage readings for my range finder. its non-linear, so thats really not going to help me.
I then need a code solution to go to the closest value, as i really only need estimated distances in increments of 5cm.
help? its part of the scan routine, foundin the code below.
#include <Servo.h>
//open analog pins - 3 , 5. open digital pins - 8 10 11 12
//declare servos
Servo panhorizontal;
Servo panverticle;
int panhorizontalpos = 0;
int panverticlepos = 0;
//declare 'scan look positions'
int lookleft = 0;
int lookright = 0;
int lookforward = 0;
int lookdown = 1;
int avgscan[2];
int avgvoltage = 0;
int cm = 0;
//motor drive speed
int stepspeedread = 0;
int outputValue = 0;
//set pins
//analog
int rangesensor = 0;
int motorspeed = 1;
int legpotleft = 2;
int legpotright = 4;
//digital
int led1 = 13;
int led2 = 0;
int driveleft1 =1;
int driveleft2 =2;
int driveright1 =4;
int driveright2 =7;
//pwm
int driveleftenable = 3;
int driverightenable = 5;
void setup()
{
Serial.begin(9600); //???
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(driveleft1, OUTPUT);
pinMode(driveleft2, OUTPUT);
pinMode(driveright1, OUTPUT);
pinMode(driveright2, OUTPUT);
//PWM pins
//pinMode(driveleftenable, OUTPUT);
//pinMode(driverightenable, OUTPUT);
panhorizontal.attach(6);
panverticle.attach(9);
//motortest? via motortest();
}
void loop()
{
scansidetoside();
scandown();
if (lookdown == 1)
{
//turn 180
}
if (lookforward == 1 && lookright == 1 and lookleft == 1)
{
//turn 180
}
if (lookforward == 1 && lookright == 0 && lookleft == 0)
{
//turn right
}
if (lookforward == 1 && lookright == 1 && lookleft == 0)
{
//turn left
}
if (lookforward == 1 && lookleft == 1 && lookright == 0)
{
//turn right
}
//step once on each side
//monitor step pots to ensure no motor jams
}
void takestep(int side, int dir, int delaytime)
{
// read the analog in value:
stepspeedread = analogRead(motorspeed);
// map it to the range of the analog out:
outputValue = map(stepspeedread, 0, 1023, 0, 255);
digitalWrite(dir, HIGH);
analogWrite(side, outputValue);
delay(delaytime);
digitalWrite(dir, LOW);
analogWrite(side, 0);
}
void scansidetoside()
{
for(panhorizontalpos = 0; panhorizontalpos < 90; panhorizontalpos += 1)
{
panhorizontal.write(panhorizontalpos);
delay(15);
}
lookright = scan();
for(panhorizontalpos = 90; panhorizontalpos > -90; panhorizontalpos-=1)
{
panhorizontal.write(panhorizontalpos);
delay(15);
}
lookleft = scan();
for(panhorizontalpos = -90; panhorizontalpos <= 0; panhorizontalpos+=1)
{
panhorizontal.write(panhorizontalpos);
delay(15);
}
lookforward = scan();
}
void scandown()
{
for(panverticlepos = 0; panverticlepos < 45; panhorizontalpos += 1)
{
panverticle.write(panhorizontalpos);
delay(15);
}
lookdown = scan();
for(panverticlepos = 45; panverticlepos > 0; panhorizontalpos -= 1)
{
panverticle.write(panhorizontalpos);
delay(15);
}
}
int scan()
{
cm = 0;
for (int i=0; i <= 2; i++){
avgscan = analogRead(rangesensor);
}
averagevoltage = (avgscan[0]+avgscan[1]+avgscan[2])/3;
//select distance based on reading value
//need distance table.
//return distance in cm
return cm;
}
void turn180(){}
void turnright(){}
void turnleft(){}
void takestep(){}
void motortest(){}
i need to create a table w/ ranges and corresponding voltage readings for my range finder. its non-linear, so thats really not going to help me.
I then need a code solution to go to the closest value, as i really only need estimated distances in increments of 5cm.
help? its part of the scan routine, foundin the code below.
#include <Servo.h>
//open analog pins - 3 , 5. open digital pins - 8 10 11 12
//declare servos
Servo panhorizontal;
Servo panverticle;
int panhorizontalpos = 0;
int panverticlepos = 0;
//declare 'scan look positions'
int lookleft = 0;
int lookright = 0;
int lookforward = 0;
int lookdown = 1;
int avgscan[2];
int avgvoltage = 0;
int cm = 0;
//motor drive speed
int stepspeedread = 0;
int outputValue = 0;
//set pins
//analog
int rangesensor = 0;
int motorspeed = 1;
int legpotleft = 2;
int legpotright = 4;
//digital
int led1 = 13;
int led2 = 0;
int driveleft1 =1;
int driveleft2 =2;
int driveright1 =4;
int driveright2 =7;
//pwm
int driveleftenable = 3;
int driverightenable = 5;
void setup()
{
Serial.begin(9600); //???
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(driveleft1, OUTPUT);
pinMode(driveleft2, OUTPUT);
pinMode(driveright1, OUTPUT);
pinMode(driveright2, OUTPUT);
//PWM pins
//pinMode(driveleftenable, OUTPUT);
//pinMode(driverightenable, OUTPUT);
panhorizontal.attach(6);
panverticle.attach(9);
//motortest? via motortest();
}
void loop()
{
scansidetoside();
scandown();
if (lookdown == 1)
{
//turn 180
}
if (lookforward == 1 && lookright == 1 and lookleft == 1)
{
//turn 180
}
if (lookforward == 1 && lookright == 0 && lookleft == 0)
{
//turn right
}
if (lookforward == 1 && lookright == 1 && lookleft == 0)
{
//turn left
}
if (lookforward == 1 && lookleft == 1 && lookright == 0)
{
//turn right
}
//step once on each side
//monitor step pots to ensure no motor jams
}
void takestep(int side, int dir, int delaytime)
{
// read the analog in value:
stepspeedread = analogRead(motorspeed);
// map it to the range of the analog out:
outputValue = map(stepspeedread, 0, 1023, 0, 255);
digitalWrite(dir, HIGH);
analogWrite(side, outputValue);
delay(delaytime);
digitalWrite(dir, LOW);
analogWrite(side, 0);
}
void scansidetoside()
{
for(panhorizontalpos = 0; panhorizontalpos < 90; panhorizontalpos += 1)
{
panhorizontal.write(panhorizontalpos);
delay(15);
}
lookright = scan();
for(panhorizontalpos = 90; panhorizontalpos > -90; panhorizontalpos-=1)
{
panhorizontal.write(panhorizontalpos);
delay(15);
}
lookleft = scan();
for(panhorizontalpos = -90; panhorizontalpos <= 0; panhorizontalpos+=1)
{
panhorizontal.write(panhorizontalpos);
delay(15);
}
lookforward = scan();
}
void scandown()
{
for(panverticlepos = 0; panverticlepos < 45; panhorizontalpos += 1)
{
panverticle.write(panhorizontalpos);
delay(15);
}
lookdown = scan();
for(panverticlepos = 45; panverticlepos > 0; panhorizontalpos -= 1)
{
panverticle.write(panhorizontalpos);
delay(15);
}
}
int scan()
{
cm = 0;
for (int i=0; i <= 2; i++){
avgscan = analogRead(rangesensor);
}
averagevoltage = (avgscan[0]+avgscan[1]+avgscan[2])/3;
//select distance based on reading value
//need distance table.
//return distance in cm
return cm;
}
void turn180(){}
void turnright(){}
void turnleft(){}
void takestep(){}
void motortest(){}