I'm a student drafter/welder on a team developing this project. We're attempting to improve the steering system, but the EE who wrote the original code is unavailable. I posted here previously when I was attempting to write the code for this project myself, but discovered I was out of my league, especially given the time constraints. Another more capable team member who lives overseas (and is stuck there due to the pandemic) took on the task. So I have all the hardware, and he has the know-how for this part of the project. This was our first attempt, and after I uploaded his code I was no longer getting any response from the vehicle when I manipulated the controls. We are trying to do this with distance and language as barriers, so please be kind as you spot errors, as I'm sure there are several.
It's a four wheel vehicle driven and steered by two separate motors on the front powered by up to 24V, and two passive caster wheels in the back (see first photo and wiring diagram). The radio controller (see photo) has a throttle control on the left, and a left/right control on the right. Steering design is achieved by having both motors operate at the same rpms until the left/right steer controller is maneuvered, at which point the rpms on the targeted side are to slow according to how far the stick is pushed (see Motor Voltage Data Tables, including table with example of a right turn with the throttle at 80% - illustration is profile of left/right stick in "action"). Code is below everything else.
#define MTR 5 //Motor right
#define MTL 6 //Motor left
#define DIR_R 7 //right direction
#define DIR_L 8 //left direction
#define FWD LOW
#define RVS HIGH
void setup() {
pinMode(10,INPUT);
pinMode(11,INPUT);
pinMode(9, INPUT);
pinMode(MTR,OUTPUT);
pinMode(MTL,OUTPUT);
pinMode(FWD,OUTPUT);
pinMode(RVS, OUTPUT);
Serial.begin(9600);
}
int steer; //steering number
int throttle; //throtthle number
int pwm = 0; //pwm number
int xpwm=0; // pwm number for steer
int pwmL=0; //pwm for left motor
int pwmR=0; //pwm for right motor
int fwd=1;
int direc = analogRead(A0);
void speedFunct(){
//full power
if(pwmL>=250 && pwmR>=250 ){
analogWrite(MTR, 255);
analogWrite(MTL, 255);
}
//no power
if(pwmL<=0 && pwmR<=0){
analogWrite(MTR, 0);
analogWrite(MTL, 0);
}
//percentage of power (duty cycle)
else{
analogWrite(MTR, pwmR); // send pwm signal to motor
analogWrite(MTL, pwmL);
}
}
int rvs = 0 ;
void steerFunct(){
//drive stright
if(1450 < steer < 1550){
if(fwd==1){
digitalWrite(DIR_R, FWD);
digitalWrite(DIR_L, FWD);
}
else{
digitalWrite(DIR_R, RVS);
digitalWrite(DIR_L, RVS);
}
speedFunct();
delay(5);
}
//steer right
if(steer >= 1550){
digitalWrite(DIR_R, FWD);
digitalWrite(DIR_L, FWD);
speedFunct();
delay(5);
}
//steer left
if(steer <= 1400){
digitalWrite(DIR_R, FWD);
digitalWrite(DIR_L, FWD);
speedFunct();
delay(5);
}
delay(5);
}
void loop() {
steer = pulseIn(10, HIGH, 250000);
throttle = pulseIn(11, HIGH, 250000);
direc = pulseIn (9, HIGH, 250000);
// dead zone
if(1400 < direc && direc < 1500){
digitalWrite(MTR, LOW);
digitalWrite(MTL,LOW);
delay(5);
Serial.print("Direction: ");
Serial.println("Stop");
Serial.print("\n");
}
//forward
if(throttle <= 1000){
rvs = 0;
digitalWrite(DIR_R, FWD);
digitalWrite(DIR_L, FWD);
pwm = map(throttle, 990, 1983,0, 255);
if (steer>= 1550){ //right
xpwm = map(steer, 1550, 1983,0, 255);
pwmL=pwm; //left keep same
pwmR=pwm-xpwm; // decrease right motor
}
else if(steer<= 1400){ //left turn
xpwm = map(steer, 1400, 0,0, 255);
pwmR=pwm; //right keep same
pwmL=pwm-xpwm; // decrease left motor
}
else{
xpwm=0;
}
fwd=1;
steerFunct();
delay(5);
Serial.print("Direction: ");
Serial.println("Forward");
Serial.print("\n");
}
//Reverse
if(throttle >= 1900){
digitalWrite(DIR_R, RVS);
digitalWrite(DIR_L, RVS);
pwm = map(throttle, 990, 1983,0, 255);
if (steer>= 1550){ //right
xpwm = map(steer, 1550, 1983,0, 255);
pwmL=pwm+xpwm; //left keep same
pwmR=pwm-xpwm; // decrease right motor
if(pwmL>255){
pwmL=255;
}
}
else if(steer<= 1400){ //left turn
xpwm = map(steer, 1400, 0,0, 255);
pwmR=pwm+xpwm; //right keep same
pwmL=pwm-xpwm; // decrease left motor
if(pwmR>255){
pwmR=255;
}
}
else{
xpwm=0;
}
fwd=0;
steerFunct();
delay(5);
Serial.print("Direction: ");
Serial.println("Reverse");
Serial.print("\n");
}
delay(5);
}```
I'll loop you in, @StefanL38, as you've been working with me previously, which I appreciate.