Page 1 of 1

crawler bot - need table and estimate routine to go with it.

Posted: Sat 5. Jun 2010, 20:07
by maglinvinn
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(){}

Re: crawler bot - need table and estimate routine to go with

Posted: Sun 6. Jun 2010, 13:30
by designer2k2
Hello,

can you post your raw sensor data? distance to analog read.

in our code we use lookup tables for example for the TypK and the VDO sensors, so code is not the problem ;)

but with your data we could look if maybe a formula would be easier :)

Re: crawler bot - need table and estimate routine to go with

Posted: Sun 6. Jun 2010, 15:28
by maglinvinn
int scan()
{
cm = 0;
for (int i=0; i <= 2; i++){
avgscan = analogRead(rangesensor);
}

int averagevoltage = (avgscan[0]+avgscan[1]+avgscan[2])/3;

//cm = pow(averagevoltage, (1/-.9042))/4187.8;
cm = pow((4187.8/averagevoltage), 1.1060);

return cm;
}

cm = pow((4187.8/averagevoltage), 1.1060);

so i think this is it. its based on a PDF i found regarding IR curve fitting, and it uses my sensor as an example, and gives this equation:

IRValue = 4187.8(Range)-0.9042

where IRValue is the value returned by the infrared sensor, and Range is the actual distance to the object in units
of centimeters.


obviously, i had the IR value, i needed the range, in the script, so i can make decisions based on 'distance to target' they also had this equation:

range = (4187.8/IRValue)^1.1060

so all in all, i think i got it??? not sure. its a little abstract to me.


Meanwhile:
http://www.youtube.com/watch?v=bDhWUjw0pA0

a stepper motor instrument cluster. i TOTALLY think this is going to be my feedback off of multi display, for my 71 opel gt. i have a lot of guages but they're all analog. i'd love to make the transition to this rig. i'd still have a 24x4 lcd in there, but i could play back other information from that.

Re: crawler bot - need table and estimate routine to go with

Posted: Tue 8. Jun 2010, 20:26
by designer2k2
i think its looking right...

easiest is to attach it to an analog input, get a ruler and place it in front of an wall and make a short table with distance to analog read.
put this into excel and you should be able to get a nice formula for it, and it will be perfect for exactly YOUR sensor :)


put an bmw tach into an opel would be nice :lol:

Re: crawler bot - need table and estimate routine to go with

Posted: Sat 19. Jun 2010, 20:12
by Funky Diver
Sorry, just joined up... but the e36 cluster... COOOL AS!!

Anyways, better go introduce myself :)