Else..if problem, I think. :( [Robot Rover]
hello!
i'm building little robot rover , stumped on something: works great going forward, reverse, , doing 0 degree turns; however, when tell 1 motor or other go, either both or not depending on motor. =(
the left motor if that's has done, not after has gone forward or reverse, both go. right motor doesn't when asked go itself. behavior between motors trades places when copy , paste "if(rcount<sr) {.....}" after " if(lcount...){...}" instead of before. don't it. (why isn't there smiley face banging one's head against wall?)
any appreciated! also, disclaimer, i'm pretty new this; so, hope code isn't embarrassing. thanks!
i'm building little robot rover , stumped on something: works great going forward, reverse, , doing 0 degree turns; however, when tell 1 motor or other go, either both or not depending on motor. =(
the left motor if that's has done, not after has gone forward or reverse, both go. right motor doesn't when asked go itself. behavior between motors trades places when copy , paste "if(rcount<sr) {.....}" after " if(lcount...){...}" instead of before. don't it. (why isn't there smiley face banging one's head against wall?)
any appreciated! also, disclaimer, i'm pretty new this; so, hope code isn't embarrassing. thanks!
code: [select]
volatile int lcount = 0;
volatile int rcount = 0;
//motor l 1
int stby = 10; //standby
//motor l
int pwma = 6; //speed control
int ain1 = 9; //direction
int ain2 = 8; //direction
//motor r
int pwmb = 5; //speed control
int bin1 = 11; //direction
int bin2 = 12; //direction
int echo = 4;
int trig = 7;
//parsed serial
int sl=0; //encoder counts covered left motor.
int dl=0; //direction of left motor
int sr=0; //encoder counts coevered right motor.
int dr=0; //direction of right motor
int theta=0; //extent of desired 0 turn
int deltas=0; //number of encoder counts covered after 0 turn.
void setup()
{
serial.begin(9600);
attachinterrupt(0, count_1, rising);
attachinterrupt(1, count_2, rising);
pinmode(stby, output);
pinmode(pwma, output);
pinmode(ain1, output);
pinmode(ain2, output);
pinmode(pwmb, output);
pinmode(bin1, output);
pinmode(bin2, output);
}
void loop(){
if (serial.available()){
char c = serial.read();
if (c == 'm'){ //"m" movement
sl = serial.parseint();
dl = serial.parseint();
sr = serial.parseint();
dr = serial.parseint();
move2(sl,dl,sr,dr);}
if (c == 'p'){ //"p" polar, 0 turn, forward motion.
theta = serial.parseint();
deltas = serial.parseint();
if (theta < 0){
theta=abs(theta);
sl = theta*7.74; //7.74 encoder counts per degree.
sr = theta*7.74;
move2(sl,1,sr,0);}
else{
sl = theta*7.74;
sr = theta*7.74;
move2(sl,0,sr,1);}
move2(deltas,1,deltas,1);
}
}
}
void count_1() //isr left motor; counts encoder ticks.
{
lcount += 1;
}
void count_2() //isr right motor.
{
rcount += 1;
}
void move(int motor, int speed, int direction){
//move specific motor @ speed , direction
//motor: 0 b 1 a
//speed: 0 off, , 255 full speed
//direction: 0 clockwise, 1 counter-clockwise
digitalwrite(stby, high); //disable standby
boolean inpin1 = low;
boolean inpin2 = high;
if(direction == 1){
inpin1 = high;
inpin2 = low;
}
if(motor == 1){
digitalwrite(ain1, inpin1);
digitalwrite(ain2, inpin2);
analogwrite(pwma, speed);
}else{
digitalwrite(bin1, inpin1);
digitalwrite(bin2, inpin2);
analogwrite(pwmb, speed);
}
}
void stop() //enable standby
{
digitalwrite(stby, low);
}
void start() //disable standby
{
digitalwrite(stby, high);
}
int move2(int sl, int dl, int sr, int dr){
lcount = 0;
rcount = 0;
while(true){
if(rcount<sr){ //this problem must be...i believe.
move(0,100,dr);}
if(lcount<sl){ //should keep motors going @ same rate.
if(lcount-rcount>0){
move(1,90,dl);}
else{
move(1,100,dl);}}
else{
break;}
}
stop();
serial.println("left motor: " + string(lcount) + " right motor: " + string(rcount));
serial.println("sl: " + string(sl) + " sr: " + string(sr));
}
code: [select]
if(direction == 1){
inpin1 = high;
inpin2 = low;
}
and, if isn't? nothing hardly seems reasonable.
Arduino Forum > Using Arduino > Programming Questions > Else..if problem, I think. :( [Robot Rover]
arduino
Comments
Post a Comment