Dozer Arduino Coding
![userHead](https://dfimg.dfrobot.com/dfPortrait2.png)
I had upgraded the dozer with a 3 POT joystick....that will spring back to center (Neutral) when let go; in neutral has a relay to cut the power off to the left and right solenoids from the Joystick. I can have other safety measures in place.....to me safety always comes first
There are 3 POTs in the Joystick:
FNR POT = Is for the forward-Neutral-Reverse; this one is set to 2.50 DCV in center, 1.5 DCV in full forward, and 3.5 DCV in full revers position
Left POT = Is to control the Left Track; in center position of the joystick it will output 1.5 DCV and 3.5 DCV in full left.
Right POT = is to control the Right Track; in center position of the joystick it will output 1.5 DCV and 3.5 DCV in full right.
I also have a Motor Controller on top of the Arduino to operate the motors
Below is the code that is working but need help with.
The issue I am having is its too sensitive; when moving the Joystick forward or backward it would move in a way that would jerk; I would need it to be more smoothing as a slow smoother ramp than as a jerking motion. Thanks for the help
// Analog Input pins:
const int LeftMotor_CurrentPin = A0; // Channel A Motor Current Pin (0V - 0A to 3.3V - 2A TODO ADCRange?)
const int RightMotor_CurrentPin = A1; // Channel B Motor Current Pin (0V - 0A to 3.3V - 2A TODO ADCRange?)
// TODO Figure out how to limit motor current
const byte Y_AIPin = A2; // Full Forward=300, Full Reverse = 711
const int Y_FullForward = 300;
const int Y_FullReverse = 711;
const byte XLeft_AIPin = A3; // 305 center to 712 left
const byte XRight_AIPin = A4; // 305 center to 712 right
const int X_Neutral = 305; // Center/Neutral position
const int X_FullTurn = 712; // Full right/left
// Outputs
const int LeftMotor_BrakePin = 9; // Channel A Brake Pin
const int RightMotor_BrakePin = 8; // Channel B Brake Pin
const int Brake_Off = LOW;
const int Brake_On = HIGH;
const int LeftMotor_SpeedPin = 3; // Channel A Motor Speed Pin
const int RightMotor_SpeedPin = 11; // Channel B Motor Speed Pin
const int Motor_FullSpeed = 255; // Range for the motor outputs
const int Motor_Stop = 0; // Range for the motor outputs
const int LeftMotor_DirPin = 12; // Channel A Motor Direction Pin
const int LeftForward = HIGH; // Change from LOW to HIGH
const int LeftReverse = LOW; // Changed from HIGH to LOW
const int RightMotor_DirPin = 13; // Channel B Motor Direction Pin
const int RightForward = LOW;
const int RightReverse = HIGH;
// Configuration
const int ThrottleMax = 255;
const int DeadZone = 15; // Adjust to allow room for neutral
int PrintLimitCnt = 0;
void setup()
{
// Initialize the motor control pins
pinMode(LeftMotor_CurrentPin, INPUT);
pinMode(RightMotor_CurrentPin, INPUT);
pinMode(Y_AIPin,INPUT);
pinMode(XLeft_AIPin,INPUT);
pinMode(XRight_AIPin,INPUT);
pinMode(LeftMotor_BrakePin,OUTPUT);
pinMode(RightMotor_BrakePin,OUTPUT);
pinMode(LeftMotor_SpeedPin,OUTPUT);
pinMode(RightMotor_SpeedPin,OUTPUT);
pinMode(LeftMotor_DirPin,OUTPUT);
pinMode(RightMotor_DirPin,OUTPUT);
Serial.begin(9600);
}
void loop()
{
// https://www.arduino.cc/reference/en/language/functions/math/map/
// map takes integer values and scales them to another range Ex: 300-711 => (-255)-255
int throttleScaled = map(analogRead(Y_AIPin), 300, 711, -ThrottleMax, +ThrottleMax);
int throttle = map(throttleScaled, Y_FullReverse, Y_FullForward, -ThrottleMax, +ThrottleMax);
// Limit steering to current throttle
int steerLeft = map(analogRead(XLeft_AIPin), X_Neutral, X_FullTurn, 0, throttle);
int steerRight = map(analogRead(XRight_AIPin), X_Neutral, X_FullTurn, 0, throttle);
// https://www.arduino.cc/reference/en/language/functions/math/constrain/
// For the left motor, go slower for turning left (negative steering)
int speedLeft = constrain(throttle - steerLeft, -255, +255); // -255 Full reverse to +255 full forward
// Opposite for the right motor
int speedRight = constrain(throttle - steerRight, -255, +255); // -255 Full reverse to +255 full forward
// Run the left motor
if (speedLeft < -DeadZone) // backwards
{
// Run left motor backwards at PWM -speedLeft
digitalWrite(LeftMotor_BrakePin, Brake_Off);
digitalWrite(LeftMotor_DirPin, LeftReverse);
int speed = map(-speedLeft, 0, 255, Motor_Stop, Motor_FullSpeed);
analogWrite(LeftMotor_SpeedPin, speed);
}
else if (speedLeft > DeadZone) // forwards
{
// Run left motor forward at PWM speedLeft
digitalWrite(LeftMotor_BrakePin, Brake_Off);
digitalWrite(LeftMotor_DirPin, LeftForward);
int speed = map(speedLeft, 0, 255, Motor_Stop, Motor_FullSpeed);
analogWrite(LeftMotor_SpeedPin, speed);
}
else
{
// Stop left motor
analogWrite(LeftMotor_SpeedPin, Motor_Stop);
digitalWrite(LeftMotor_BrakePin, Brake_On);
}
// Run the motors:
if (speedRight < -DeadZone) // backwards
{
// Run right motor backwards at PWM -speedRight
digitalWrite(RightMotor_BrakePin, Brake_Off);
digitalWrite(RightMotor_DirPin, RightReverse);
int speed = map(-speedRight, 0, 255, Motor_Stop, Motor_FullSpeed);
analogWrite(RightMotor_SpeedPin, speed);
}
else if (speedRight > DeadZone) // forwards
{
// Run right motor forward at PWM speedRight
digitalWrite(RightMotor_BrakePin, Brake_Off);
digitalWrite(RightMotor_DirPin, RightForward);
int speed = map(speedRight, 0, 255, Motor_Stop, Motor_FullSpeed);
analogWrite(RightMotor_SpeedPin, speed);
}
else
{
// Stop right motor
analogWrite(RightMotor_SpeedPin, Motor_Stop);
digitalWrite(RightMotor_BrakePin, Brake_On);
}
// TODO Limit motor current?
int currentLeft = analogRead(LeftMotor_CurrentPin);
int currentRight = analogRead(RightMotor_CurrentPin);
// Printing to console is slow, only do it on occasion
if (!(++PrintLimitCnt % 1000)) // Increase to slow down prints
{
Serial.print("throttle=");
Serial.print(throttle);
Serial.print(" \tsteerLeft=");
Serial.print(steerLeft);
Serial.print(" \tsteerRight=");
Serial.print(steerRight);
Serial.print(" \tspeedLeft=");
Serial.print(speedLeft);
Serial.print(" \tspeedRight=");
Serial.print(speedRight);
Serial.print(" \tcurrentLeft=");
Serial.print(currentLeft);
Serial.print(" \tcurrentRight=");
Serial.println(currentRight);
}
}