Hi guys
Basically what I'm trying to build is a remote control vehicle, with an autonomous function. I'm using a transmitter and receiver for the remote control bit. I would like the car to go into autonomous mode when I flick the toggle switch on my transmitter, but I'm having problems :(. So far I have got the car being controlled manually with the transmitter, but struggling with the autonomous mode.
Here's the code so far
int motor1_a = 5; // motor 1 outputs
int motor1_b = 9;
int motor2_a = 6; // motor 2 outputs
int motor2_b = 10;
int servo1 = 2; // r/c channel 1
int servo2 = 3; // r/c channel 2
int ppm = 7;
int servo_val;
int LED_pin = 13;
//int relay_Pin = 8;
unsigned int relay_val;
volatile unsigned long servo1_startPulse; //values for channel 1 signal capture
volatile unsigned servo1_val;
volatile int adj_val1;
volatile int servo1_Ready;
volatile unsigned long servo2_startPulse; //values for channel 2 signal capture
volatile unsigned servo2_val;
volatile int adj_val2;
volatile int servo2_Ready;
int deadband_high = 275;
int deadband_low = 235;
int pwm_ceiling = 256;
int pwm_floor = 255;
int low1 = 1108; // adjust these values if your R/C readings are above or below these for channels 1 and 2.
int high1 = 1908;
int low2 = 1096;
int high2 = 1900;
int n = 0;
void setup() {
//TCCR0B = TCCR0B & 0b11111000 | 0x03;
//TCCR1B = TCCR1B & 0b11111000 | 0x01;
Serial.begin(9600);
//motor1 pins
pinMode(motor1_a, OUTPUT);
pinMode(motor1_b, OUTPUT);
pinMode(motor2_a, OUTPUT);
pinMode(motor2_b, OUTPUT);
pinMode(LED_pin, OUTPUT); // declare output
//PPM inputs from RC receiver
pinMode(servo1, INPUT); //Pin 2 as input
pinMode(servo2, INPUT); //Pin 3 as input
pinMode(ppm, INPUT); // declare input
//digitalWrite(relay_Pin, LOW);
delay(1200);
attachInterrupt(0, rc1_begin, RISING); // catch interrupt 0 (digital pin 2) going HIGH and send to rc1()
attachInterrupt(1, rc2_begin, RISING); // catch interrupt 1 (digital pin 3) going HIGH and send to rc2()
}
////////// attach servo signal interrupts to catch signals as they go HIGH then again as they go LOW, then calculate the pulse length.
void rc1_begin() { // enter rc1_begin when interrupt pin goes HIGH.
servo1_startPulse = micros(); // record microseconds() value as servo1_startPulse
detachInterrupt(0); // after recording the value, detach the interrupt from rc1_begin
attachInterrupt(0, rc1_end, FALLING); // re-attach the interrupt as rc1_end, so we can record the value when it goes low
}
void rc1_end() {
servo1_val = micros() - servo1_startPulse; // when interrupt pin goes LOW, record the total pulse length by subtracting previous start value from current micros() vlaue.
detachInterrupt(0); // detach and get ready to go HIGH again
attachInterrupt(0, rc1_begin, RISING);
}
void rc2_begin() {
servo2_startPulse = micros();
detachInterrupt(1);
attachInterrupt(1, rc2_end, FALLING);
}
void rc2_end() {
servo2_val = micros() - servo2_startPulse;
detachInterrupt(1);
attachInterrupt(1, rc2_begin, RISING);
}
/////// servo interrupts end
/////// MAIN LOOP
void loop() {
// use pulseIn() command to read the length of the pulse on D2, with a timeout of 20mS (20000uS)
servo_val = pulseIn(ppm, HIGH, 20000);
// check the pulse length to see if it is above 1600 microseconds (ie. Above neutral)
if (servo_val > 1600){
// if so, turn LED off
digitalWrite(LED_pin, HIGH);
Serial.println("Failsafe Active!");
///// channel 1 good signal check
if (servo1_val < 600 || servo1_val > 2400) { // only set the servo1_Ready flag if the value is a valid Servo pulse between 600-2400 microseconds.
servo1_Ready = false;
servo1_val = 1500;
}
else {
servo1_Ready = true; // if not, don't pass the value to be processed
}
///// channel 1 good signal check
if (servo2_val < 600 || servo2_val > 2400) {
servo2_Ready = false;
servo2_val = 1500;
}
else {
servo2_Ready = true;
}
////////// channel 1 motor update
if (servo1_Ready) {
servo1_Ready = false;
adj_val1 = map(constrain(servo1_val, 600, 2400), low1, high1, 0, 511);
constrain(adj_val1, 0, 511);
if (adj_val1 < 0) {
adj_val1 = 0;
}
if (adj_val1 > 511) {
adj_val1 = 511;
}
if (adj_val1 > deadband_high) {
//Forward
digitalWrite(motor2_a , LOW);
analogWrite(motor1_a, adj_val1 - pwm_ceiling);
}
else if (adj_val1 < deadband_low) {
//REVERSE
digitalWrite(motor1_a, LOW);
analogWrite(motor2_a, pwm_floor - adj_val1);
}
else {
//STOP
digitalWrite(motor1_a, LOW);
digitalWrite(motor2_a, LOW);
}
}
///////////// channel 2 motor update
if (servo2_Ready) {
servo2_Ready = false;
adj_val2 = map(constrain(servo2_val, 600, 2400), low1, high1, 0, 505);
constrain(adj_val2, 0, 505);
if (adj_val2 < 0) {
adj_val2 = 0;
}
if (adj_val2 > 505) {
adj_val2 = 505;
}
if (adj_val2 > deadband_high) {
//Forward
digitalWrite(motor2_b, LOW);
analogWrite(motor1_b, adj_val2 - pwm_ceiling);
}
else if (adj_val2 < deadband_low) {
//REVERSE
digitalWrite(motor1_b, LOW);
analogWrite(motor2_b, pwm_floor - adj_val2);
}
else {
//STOP
digitalWrite(motor1_b, LOW);
digitalWrite(motor2_b, LOW);
}
}
else {
delay(3000);
// otherwise if signal is below 1600 uS, turn LED Off
digitalWrite(LED_pin, 0);
Serial.println("Failsafe turned Off");
}
}
//print values to Serial monitor in Arduino IDE
Serial.print("ch1: ");
Serial.print(adj_val1);
Serial.print(" ");
Serial.print("rc1: ");
Serial.print(servo1_val);
Serial.print(" ");
Serial.print("ch2: ");
Serial.print(adj_val2);
Serial.print(" ");
Serial.print("rc2: ");
Serial.print(servo2_val);
Serial.print(" ");
Serial.print("loop counter: ");
Serial.print(n);
Serial.println(" ");
//n++;
}
The car has tank steering setup.
When I flick the toggle switch on the transmitter, which is connected to pin 7 on the arduino, the Led is meant to turn off, but it doesn't. As you'v probably guessed this is where I would like my autonomous section to be implemented., when I flick the toggle switch.
Can someone help me out
Btw have a great christmas n a happy new year
cheers
Sam