I am having the weirdest problem with a simple assignment operator. I have a robot with 2 sonars and 2 rotary encoders for the wheels. the arduino monitors an RC receiver, does some calculations to get the angles and throttle, and calculates its heading based on the distance the wheels have traveled. As the loop() runs, it updates its heading by adding to or subtracting from the variable 'heading'. The angle that is going to be added to heading is 'temp4'. However something very strange happens when i take the //comment from the line '//heading = heading + temp4'. 'heading' seems to become a null variable or something, and the 'if' comparisons that check the heading against the RC angle stop working entirely. these 'if' checks are directly after the 'heading = heading + temp4'. the wheels are told to move forward at the speed of 'chthrottle'. but if the 'rca' is greater or less than 'heading' it tells the opposite wheel to reverse. Only, the one simple line 'heading = heading + temp4' does something, and the wheels only go forward. all the variables involved are of type float, and i believe are initialized properly. is the 'heading' variable off limits or something? I tried different variables and still same problem. I tried monitoring 'heading' with the serial.print, and heading always = 0.00. i tested a simple line like 'heading = heading + 1.0' and that worked. And I monitored 'temp4', it was reading correct numbers but as sson as i try to add 'heading' and 'temp4' I get issues. What am I missing??? Thanks in advance to anyone who can help!
#include <Servo.h>
#include "math.h"
Servo wheelL;
Servo wheelR;
const int ch1Pin = 8;//RC Receiver Ch 1
const int ch2Pin = 9;//RC Receiver Ch 2
const int encpinL = 28;//Left motor encoder
const int encpinR = 29;//Right motor encoder
const int pwPinL = 7;//Left sonar
const int pwPinR = 6;//Right sonar
const int rxPinL = 30;//rx pin on Left sonar
const int rxPinR = 31;//rx pin on Right sonar
int thrinc, thrdec;
int ch1, ch1max, ch1min, ch1mid, ch1range;
int ch2, ch2max, ch2min, ch2mid, ch2range;
int inchesL, inchesR;
unsigned long pulseL, pulseR;
float mphL, mphR;
float wheelLpwm, wheelRpwm, wheelLpwmset, wheelRpwmset, wheelLmax, wheelRmax, wheeltemp;
int toggle1, toggle2, toggle3, toggle4, toggle5, toggle6, togglemain;//Toggles for activating different parts of the main program.
unsigned long enctickL, enctickR, timer, timer1, timer2;
float pi = 3.141592;
float temp1, temp2, temp3, temp4, heading, chthrottle, mpl, iplL, iplR;
float rca, rcx, rcy, posx, posy;
void setup() {
wheelL.attach(2);
wheelR.attach(3);
wheelLpwmset = 91.0; wheelRpwmset = 91.0; wheelLmax = 1.0; wheelRmax = 1.0;
thrinc = 2; ;thrdec = 2;//Throttle increase and decrease speeds
pinMode(ch1Pin, INPUT);
pinMode(ch2Pin, INPUT);
pinMode(encpinL, INPUT);
pinMode(encpinR, INPUT);
pinMode(pwPinL, INPUT);
pinMode(pwPinR, INPUT);
pinMode(rxPinL, OUTPUT);
pinMode(rxPinR, OUTPUT);
Serial.begin(9600); delay(100);
//Set receiver max and min ranges
ch1max = 2050; ch1mid = 1450; ch1min = 850; ch2max = 2080; ch2mid = 1480; ch2min = 880;
ch1range = ch1max - ch1min; ch2range = ch2max - ch2min;
heading = 0.0; temp4 = 0.0;
toggle1 = 0; toggle2 = 0; toggle3 = 1; toggle4 = 1; toggle5 = 0; toggle6 = 0; togglemain = 0;
}
void loop() {
//Turn on sonar
if (toggle1 == 0) {//Sonar Sequence 1
digitalWrite(rxPinL, HIGH);//Turn on Left sonar
toggle1 = 1; toggle2 = 0;
}
if (toggle3 == 0) {//Sonar Sequence 3
digitalWrite(rxPinR, HIGH);//Turn on Right sonar
toggle3 = 1; toggle4 = 0;
}
//End turn on sonar
//Begin motor encoder readings, keep close to motor.write command to stay accurate
enctickL = pulseIn(encpinL, HIGH, 40000);
enctickR = pulseIn(encpinR, HIGH, 40000);
timer2 = timer1; timer1 = millis();
mpl = (float)timer1 - (float)timer2;//temp3 = time in milliseconds between encoder readings
temp1 = 1.0 / (float)enctickL * 1000.0;//temp1 = ticks per millisecond
temp2 = 1.0 / (float)enctickR * 1000.0;
temp1 = temp1 * 8125.0 / 84.0;//temp1 = inches per second
temp2 = temp2 * 8125.0 / 84.0;
mphL = temp1 * 5.0 / 88.0;//convert inches per second to MPH
mphR = temp2 * 5.0 / 88.0;
iplL = temp1 / 1000.0 * mpl;//iplL = inches per loop
iplR = temp2 / 1000.0 * mpl;
if (wheelLpwmset < 91.0) iplL = 0.0 - iplL;//make iplL negative if in reverse
if (wheelRpwmset < 91.0) iplR = 0.0 - iplR;
//End motor encoder readings
//Begin Sonar Readings
if (millis() - timer >= 40 && toggle2 == 0) {//Sonar Sequence 2
pulseL = pulseIn(pwPinL, HIGH, 50000);
inchesL = pulseL/147;//147uS per inch
digitalWrite(rxPinL, LOW);//Turn off Left sonar
toggle2 = 1; toggle3 = 0;
}
if (millis() - timer >= 40 && toggle4 == 0) {//Sonar Sequence 4
pulseR = pulseIn(pwPinR, HIGH, 50000);
inchesR = pulseR/147;
digitalWrite(rxPinR, LOW);//Turn off Right sonar
toggle4 = 1; toggle1 = 0; togglemain = 1;
}
//End Sonar Readings
//Begin RC receiver code
ch1 = pulseIn(ch1Pin, HIGH, 40000);
ch2 = pulseIn(ch2Pin, HIGH, 40000);
if (ch1 > 2200 || ch1 < 700) ch1 = ch1mid;//Set to mid if RC too far out of range
if (ch2 > 2200 || ch2 < 700) ch2 = ch2mid;
if (ch1 > ch1mid - 60 && ch1 < ch1mid + 60) ch1 = ch1mid;//Set deadzones for RC
if (ch2 > ch2mid - 60 && ch2 < ch2mid + 60) ch2 = ch2mid;
//End RC receiver code
//Send commands to motors
rcx = ch1 - ch1mid;
rcx = rcx / ch1range * 2;
rcy = ch2 - ch2mid;
rcy = rcy / ch2range * 2;
//rcx and rcy range from -1.0 to 1.0
chthrottle = sq(rcx) + sq(rcy);
chthrottle = sqrt(chthrottle) / sqrt(2);
//chthrottle ranges from 0.0 to 1.0 depening on joystick position
rca = atan2(rcy, rcx) - pi / 2.0;//convert receiver into angles for use in heading
if (rca < 0.0 - pi) rca = rca + 2 * pi;
//calculate heading based on distance wheels have traveled
//Angle changed = (2(iplR-iplL)/39)
temp4 = iplR - iplL;
temp4 = temp4 / 19.5;
//heading = heading + temp4;
//apply wheel direction based on rca angle and heading
wheelLpwm = chthrottle;
wheelRpwm = chthrottle;
if (rca >= heading + 0.01) wheelLpwm = 0.0 - chthrottle;
if (rca <= heading - 0.01) wheelRpwm = 0.0 - chthrottle;
//Begin motor adjustments according to sonars
if (inchesR < 95) {
wheelLmax = (float)inchesR - 5.0;
wheelLmax = wheelLmax / 180.0;
wheelLmax = constrain(wheelLmax, 0.0, 1.0);
wheelLpwm = constrain(wheelLpwm, 0.0 - wheelLmax, wheelLmax);
}
if (inchesL < 95) {
wheelRmax = (float)inchesL - 5.0;
wheelRmax = wheelRmax / 180.0;
wheelRmax = constrain(wheelRmax, 0.0, 1.0);
wheelRpwm = constrain(wheelRpwm, 0.0 - wheelRmax, wheelRmax);
}
if (wheelLpwm > 0.0 && inchesL < 10) {
wheelLpwm = 0.0; wheelLpwmset = 91;
}
if (wheelRpwm > 0.0 && inchesR < 10) {
wheelRpwm = 0.0; wheelRpwmset = 91;
}
//End motor sonar adjustments
wheelLpwm = wheelLpwm * 89 + 91;//convert -1 to 1 range to 2 to 180
wheelRpwm = wheelRpwm * 89 + 91;
//Throttle wheels to prevent sudden increase or decrease
if (wheelLpwm > wheelLpwmset) wheelLpwmset = wheelLpwmset + 0.5;//Set throttle of wheels
if (wheelLpwm < wheelLpwmset) wheelLpwmset = wheelLpwmset - 0.5;
if (wheelRpwm > wheelRpwmset) wheelRpwmset = wheelRpwmset + 0.5;
if (wheelRpwm < wheelRpwmset) wheelRpwmset = wheelRpwmset - 0.5;
if (wheelLpwm > wheelLpwmset + thrinc) wheelLpwmset = wheelLpwmset + thrinc;//Set throttle of wheels
if (wheelLpwm < wheelLpwmset - thrdec) wheelLpwmset = wheelLpwmset - thrdec;
if (wheelRpwm > wheelRpwmset + thrinc) wheelRpwmset = wheelRpwmset + thrinc;
if (wheelRpwm < wheelRpwmset - thrdec) wheelRpwmset = wheelRpwmset - thrdec;
//End throttle
wheelL.write(wheelLpwmset);
wheelR.write(wheelRpwmset);
//End motor commands
//check if full loop is expired
if (togglemain == 1) {
Serial.println();
//Serial.print("Milliseconds per full cycle:");
//Serial.print(millis() - timer);
//Serial.println();
Serial.print("Milliseconds per loop:");
Serial.print(mpl);
Serial.println();
Serial.print("Left Encoder:");
Serial.print(enctickL); Serial.println();
Serial.print("Right Encoder:");
Serial.print(enctickR); Serial.println();
Serial.print("L Wheel"); Serial.println();
Serial.print(wheelLpwm); Serial.println();
Serial.print("R Wheel"); Serial.println();
Serial.print(wheelRpwm); Serial.println();
Serial.print("Left wheel MPH:");Serial.print(mphL);Serial.println();
Serial.print("Right wheel MPH:");Serial.print(mphR);Serial.println();
Serial.print("Left wheel distance per loop:");Serial.print(iplL);Serial.println();
Serial.print("Right wheel distance per loop:");Serial.print(iplR);Serial.println();
Serial.print("chthrottle:");Serial.print(chthrottle);Serial.println();
Serial.print("RC Angle:");Serial.print(rca);Serial.println();
Serial.print("temp4:");Serial.print(temp4);Serial.println();
Serial.print("Heading:");Serial.print(heading);Serial.println();
timer = millis();
togglemain = 0;
}
}