I have this code that I have been working on for a long time and its finally coming to together. for now it is a code to read a RC receiver and use that out put to control two serial motor controllers connected to a deferential robotic platform. The issue I am having is that the RC controller is bouncing around a lot (-+ 15 when at center position) and causing the motors to move forward or backwards. I am wondering if there is a way to create a "dead zone" for the center position of each channel. I am using a chipkit max32. The typical output of the receiver for each channel is 3500-4500. so for the only way I have solved this issue is by ignoring the last two digits of the numbers. ie(3500/100), but i would like to have the resolution back.
As of this point in time I dont believe I can use interrupts due to software.
unsigned long counter = 0;
unsigned long Channel1Value;
unsigned long Channel2Value;
unsigned long lastgood1;
unsigned long lastgood2;
unsigned long InitialSteer;
unsigned long InitialThrottle;
unsigned long InitialMotorLimit;
long MotorLimit;
int Channel3Value;
boolean Right;
//motor stuff
int Steer;
int Thrust;
int RightMotor;
int LeftMotor;
int Chan1 = 11;
int Chan2 = 10;
int Chan3 = 9;
HardwareSerial mySerial = Serial1;
/**************************************************************
* Subroutine to exit saft start;
***************************************************************/
void exitSafeStartBoth()
{
mySerial.print(0x83, BYTE);
}
/**************************************************************
* Subroutine to control right motor= motorRight(speed, direction);
***************************************************************/
void setMotorSpeedleft(int speedleft)
{
if (speedleft < 0)
{
mySerial.print(0xAA, BYTE);
mySerial.print(0xd, BYTE);
mySerial.print(0x6, BYTE); // motor reverse command
speedleft = -speedleft; // make speed positive
}
else
{
mySerial.print(0xAA, BYTE);
mySerial.print(0xd, BYTE);
mySerial.print(0x5, BYTE); // motor forward command
}
mySerial.print((unsigned char)(speedleft & 0x1F), BYTE);
mySerial.print((unsigned char)(speedleft >> 5), BYTE);
}
/**************************************************************
* Subroutine to control left motor
***************************************************************/
void setMotorSpeedright(int speedright)
{
if (speedright < 0)
{
mySerial.print(0xAA, BYTE);
mySerial.print(0xa, BYTE);
mySerial.print(0x06, BYTE); // motor reverse command
speedright = -speedright; // make speed positive
}
else
{
mySerial.print(0xAA, BYTE);
mySerial.print(0xa, BYTE);
mySerial.print(0x05, BYTE); // motor forward command
}
mySerial.print((unsigned char)(speedright & 0x1F), BYTE);
mySerial.print((unsigned char)(speedright >> 5), BYTE);
}
void setup()
{
mySerial.begin(19200);
Serial.println("Ready");
pinMode (Chan3, INPUT); // connect Rx channel 1 to PD3, which is labled "D3" on the Arduino board
pinMode (Chan2, INPUT); // connect Rx channel 2 to PD4, which is labled "D4" on the Arduino board
pinMode (Chan1, INPUT); // connect Rx channel 3 to PD5, which is labled "D5" on the Arduino board
InitialSteer = (pulseIn (Chan2, HIGH)/100); //read RC channel 1
lastgood1 = InitialSteer;
InitialThrottle = (pulseIn (Chan1, HIGH)/100); //read RC channel 2
lastgood2 = InitialThrottle;
InitialMotorLimit = (pulseIn (Chan3, HIGH)/100);
}
void loop()
{
// counter++; // this just increments a counter for benchmarking the impact of the pulseIn's on CPU performance
Channel1Value = (pulseIn (Chan1, HIGH, 50000)/100); //read RC channel 1
Channel2Value = (pulseIn (Chan2, HIGH, 35000)/100); //read RC channel 2
Channel3Value = (pulseIn (Chan3, HIGH, 50000)/100); // read RC channel 3
if (Channel1Value == 0) {
Channel1Value = lastgood1;
}
else {
lastgood1 = Channel1Value;
}
if (Channel1Value < InitialSteer) {
Right = false;
}
else {
Right = true;
}
if (Right) {
Steer = Channel1Value - InitialSteer;
}
else {
// Steer = InitialSteer - Channel1Value;
}
Steer = map(Channel1Value, 34, 56, -(MotorLimit), MotorLimit); // convert to -3200 to 3200
constrain (Steer, -MotorLimit, MotorLimit); //just in case
// Thrust = Channel2Value - InitialThrottle;
Thrust = map(Channel2Value, 36, 55, -(MotorLimit), MotorLimit); // convert to 0-100 range
constrain (Thrust, -(MotorLimit), MotorLimit); //just in case
MotorLimit = map(Channel3Value, 35, 54, 0, 3200); // map to 0-3200
constrain (MotorLimit, 0, 3200);
Serial.print ("Channel 1: "); // if you turn on your serial monitor you can see the readings.
Serial.println (Channel1Value);
Serial.print("Channel 2: ");
Serial.println (Channel2Value);
Serial.print ("Channel 3: ");
Serial.println (Channel3Value);
Serial.println(LeftMotor);
Serial.println(RightMotor);
Serial.println("");
Serial.println("");
Serial.println("");
Serial.println("");
if (Right == true) // turn right
{
RightMotor = Thrust - Steer; // reduce power to the motor in the direction you want to go
// if (RightMotor < 0) RightMotor = 0; //mike changed this so that they will run backwards
LeftMotor = Thrust + Steer; // increase power to the motor opposite the direction you want to go
if (LeftMotor > 3200) LeftMotor = 3200;
}
if (Right == false) //turn left
{
LeftMotor = Thrust - Steer; // reduce power to the motor in the direction you want to go
// if (LeftMotor < 0) LeftMotor = 0; //mike changed this so that they will run backwards
RightMotor = Thrust + Steer; // increase power to the motor opposite the direction you want to go
if (RightMotor > 3200) RightMotor = 3200;
}
constrain (LeftMotor, -3200, 3200);
constrain (RightMotor, -3200, 3200);
setMotorSpeedleft(LeftMotor);
setMotorSpeedright(RightMotor);
delay(500);
}