controlling 2 motors + 1 servo using interrupts


i trying control 2 motors , 1 servo simultaneously via rc receiver connected arduino blimp project stuck on trying implement interrupts. current code, servo behaves when motors off. when throttle increased, servo moves specific position , gets stuck there. when motors turned off again, servo returns neutral , can controlled. throttle works ok erratic , not smooth. steering (i.e. turning left or right motors on/off) doesn't work. using external interrupts right approach here or should internal interrupts used (i took @ internal interrupts , confused different hex codes required)? should pulsein() function servo replaced interrupt?

code: [select]

#include <servo.h>

boolean ch1_inputready;
boolean ch2_inputready;
boolean ch3_inputready;

const int ch1_pin = 2;  //input pin steering left/right
const int ch2_pin = 4;  //input pin steering up/down
const int ch3_pin = 3;  //input pin throttle
const int leftmotor_pin = 5;  //output pin left motor
const int rightmotor_pin = 6;  //output pin right motor
const int servo_pin = 11;  //output pin servo steering up/down
const unsigned long ch1_min = 1200;
const unsigned long ch1_max = 1800;
const unsigned long ch3_min = 1080;  //minimum ppm value read throttle stick
const unsigned long ch3_max = 1800;  //maximum ppm value read throttle stick

int ch1_value;
int ch3_value;

servo ch2_servo;

unsigned long ch1_pulselength;
unsigned long ch2_pulselength;
unsigned long ch3_pulselength;
unsigned long ch1_starttime;
unsigned long ch2_starttime;
unsigned long ch3_starttime;

void setup() {
 ch2_servo.attach(servo_pin);
 pinmode(ch1_pin, input);
 pinmode(ch3_pin, input);
 pinmode(leftmotor_pin, output);
 pinmode(rightmotor_pin, output);
 
 attachinterrupt(0, ch1_begin, rising);
 attachinterrupt(1, ch3_begin, rising);
 serial.begin(9600);
}

void ch1_begin() {
 ch1_starttime = micros();
 detachinterrupt(0);
 attachinterrupt(0, ch1_end, falling);  
}

void ch1_end() {
 ch1_pulselength = micros() - ch1_starttime;
 detachinterrupt(0);
 attachinterrupt(0, ch1_begin, rising);
}

void ch3_begin() {
 ch3_starttime = micros();
 detachinterrupt(1);
 attachinterrupt(1, ch3_end, falling);  
}

void ch3_end() {
 ch3_pulselength = micros() - ch3_starttime;
 detachinterrupt(1);
 attachinterrupt(1, ch3_begin, rising);
}

void loop() {
 if(ch1_pulselength < 600 || ch1_pulselength > 2400) {
   ch1_inputready = false;
   ch1_pulselength = 1500;
 } else {
   ch1_inputready = true;
 }
 
 if(ch3_pulselength < 600 || ch3_pulselength > 2400) {
   ch3_inputready = false;
   ch3_pulselength = 1500;
 } else {
   ch3_inputready = true;
 }
 
 if(ch1_inputready && ch3_inputready) {
   ch1_inputready = false;
   ch3_inputready = false;
   
   if(ch3_pulselength < ch3_min) {
     ch3_pulselength = ch3_min;
   } else if(ch3_pulselength > ch3_max) {
     ch3_pulselength = ch3_max;
   }
   
   ch1_value = constrain(ch1_pulselength, ch1_min, ch1_max);
   ch3_value = map(ch3_pulselength, ch3_min, ch3_max, 0, 255);
   
   int centralvalue = (ch1_min + ch1_max) / 2;
   
   if(ch1_value < centralvalue - 100) {  //100 spacer value stick has
     analogwrite(leftmotor_pin, 0);      //a bit of play when neutral
     analogwrite(rightmotor_pin, ch3_value);
   } else if(ch1_value > centralvalue + 100) {
     analogwrite(leftmotor_pin, ch3_value);
     analogwrite(rightmotor_pin, 0);
   } else {
     analogwrite(leftmotor_pin, ch3_value);
     analogwrite(rightmotor_pin, ch3_value);
   }
 }
 
 ch2_pulselength = pulsein(ch2_pin, high);
 ch2_servo.writemicroseconds(ch2_pulselength);
 
 //serial.println("ch1 value: " + string(ch1_value)); //for debugging
 //serial.println("ch3 value: " + string(ch3_value) + "\n");
 
 serial.println("ch1: " + string(ch1_pulselength));
 serial.println("ch2: " + string(ch2_pulselength));
 serial.println("ch3: " + string(ch3_pulselength) + "\n");
}



Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > controlling 2 motors + 1 servo using interrupts


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums