i am trying to write a code for a rc controlled skid=steer robot but i am having issues with the safety side of the coding. the forword/backward and left/ right works well but when the robot is powered up and i turn the rc transmitter off the microcontroller tells the motor controller to run at full speed. i can not seem to change that. any ideas for a safer way? this robot is over 200 lbs and is an lod powered wheel chair. it can easily push a car. any ideas on how to change the code so that it safer if it loses signal from the rc, that way it just cant go crazy? or how to totally over haul the skid-steer code? my hardware is: chipkit max32, (2) pololu simple motor controlers(serial ttl), flysky fs=ct6b rc
unsigned long counter = 0;
unsigned long Channel1Value;
unsigned long Channel2Value;
unsigned long lastgood1;
unsigned long lastgood2;
unsigned long InitialSteer;
unsigned long InitialThrottle;
boolean Right;
//motor stuff
long Steer;
int Thrust;
int RightMotor;
int LeftMotor;
int Chan1 = 11;
int Chan2 = 10;
int newVal1;
int newVal2;
#define MotorLimit 3200
int sensor = A0;
int dist;
/**************************************************************
* Subroutine to exit saft start;
***************************************************************/
void exitSafeStartBoth()
{
Serial3.print(0x83, BYTE);
}
/**************************************************************
* Subroutine to control right motor= motorRight(speed, direction);
***************************************************************/
void setMotorSpeedleft(int speedleft)
{
if (speedleft < 0)
{
Serial3.print(0xAA, BYTE);
Serial3.print(0xa, BYTE);
Serial3.print(0x6, BYTE); // motor reverse command
speedleft = -speedleft; // make speed positive
}
else
{
Serial3.print(0xAA, BYTE);
Serial3.print(0xa, BYTE);
Serial3.print(0x5, BYTE); // motor forward command
}
Serial3.print((unsigned char)(speedleft & 0x1F), BYTE);
Serial3.print((unsigned char)(speedleft >> 5), BYTE);
}
/**************************************************************
* Subroutine to control left motor
***************************************************************/
void setMotorSpeedright(int speedright)
{
if (speedright < 0)
{
Serial3.print(0xAA, BYTE);
Serial3.print(0xd, BYTE);
Serial3.print(0x06, BYTE); // motor reverse command
speedright = -speedright; // make speed positive
}
else
{
Serial3.print(0xAA, BYTE);
Serial3.print(0xd, BYTE);
Serial3.print(0x05, BYTE); // motor forward command
}
Serial3.print((unsigned char)(speedright & 0x1F), BYTE);
Serial3.print((unsigned char)(speedright >> 5), BYTE);
}
void setup()
{
Serial3.begin(19200);
Serial.begin(19200);
Serial.println("Ready");
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
pinMode (sensor, INPUT);
InitialSteer = pulseIn (Chan1, HIGH); //read RC channel 1
lastgood1 = InitialSteer;
InitialThrottle = pulseIn (Chan2, HIGH); //read RC channel 2
lastgood2 = InitialThrottle;
}
void loop()
{
dist = analogRead(sensor);
// counter++; // this just increments a counter for benchmarking the impact of the pulseIn's on CPU performance
newVal1 = pulseIn(Chan1, HIGH, 50000);
newVal2 = pulseIn(Chan2, HIGH, 35000);
if(newVal1 >= Channel1Value ||
newVal1 <= Channel1Value)
Channel1Value = newVal1;
if(newVal2 >= Channel2Value ||
newVal2 <= Channel2Value)
Channel2Value = newVal2; //read RC channel 2
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 = constrain (Steer, -MotorLimit, MotorLimit); //just in case
//Thrust = Channel2Value - InitialThrottle;
Thrust = map(Channel2Value, 3645, 5565, -(MotorLimit), MotorLimit);
Thrust = constrain (Thrust, -(MotorLimit), MotorLimit); //just in case
Serial.print ("Channel 1: "); // if you turn on your serial monitor you can see the readings.
Serial.println (Channel1Value);
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.println(LeftMotor);
Serial.println(RightMotor);
Serial.println(dist);
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;
}
if (Channel1Value <= 0) {
LeftMotor = 0;
RightMotor = 0;
}
if (Channel1Value > 3600) {
exitSafeStartBoth();
}
LeftMotor = constrain (LeftMotor, -3200, 3200);
RightMotor = constrain (RightMotor, -3200, 3200);
setMotorSpeedleft(LeftMotor);
setMotorSpeedright(RightMotor);
delay(50);
}