Hello Everyone!
I am trying to understand the logic of the following code which I have to use to control a quadcopter using Arduino mega 2560. I don't want to generate soft interrupt thats why I am trying to do it with hard interrupts. I have taken this code from (credits to Omar)
The problem I am facing while understanding the code is in read_rc_rx() function and ISR for interrupts e.g. (void CH1_int_ISR() ).
I am new to arduino. I did work in PIC 18 interrupts earlier so I am having concept of interrupt. The thing I am unable to understand is the logic behind these calculations.
Any kind of help would be appreciating!
Regards.
#define dt 20 // [ms] Task time
#define CH1_int 0 // Channel 1 interrupt #
#define CH1_pin 2 // Respective channel Hardware interrupt pin number
#define CH2_int 1 // Channel 2 interrupt #
#define CH2_pin 3 // Respective channel Hardware interrupt pin number
#define CH3_int 4 // Channel 3 interrupt #
#define CH3_pin 19 // Respective channel Hardware interrupt pin number
#define CH4_int 5 // Channel 4 interrupt #
#define CH4_pin 18 // Respective channel Hardware interrupt pin number
#define valid_pulse_limit 3000 // [uS] Valid output high pulse time limit for RC controller
#define max_high_time 1895 // [uS] Maximum expected high time
#define min_high_time 1090 // [uS] Minimum expected high time
unsigned long t=0;
void init_rc_rx();
void read_rc_rx();
volatile unsigned long CH1_t=0, CH1_delta=0, CH2_t=0, CH2_delta=0, CH3_t=0, CH3_delta=0, CH4_t=0, CH4_delta=0 ;
float CH1,CH2,CH3,CH4;
// Interrupt ISRs
void CH1_int_ISR()
{
if ((micros()-CH1_t) < valid_pulse_limit){
CH1_delta = micros()-CH1_t;
}
CH1_t = micros();
}
void CH2_int_ISR()
{
if ((micros()-CH2_t) < valid_pulse_limit){
CH2_delta = micros()-CH2_t;
}
CH2_t = micros();
}
void CH3_int_ISR()
{
if ((micros()-CH3_t) < valid_pulse_limit){
CH3_delta = micros()-CH3_t;
}
CH3_t = micros();
}
void CH4_int_ISR()
{
if ((micros()-CH4_t) < valid_pulse_limit){
CH4_delta = micros()-CH4_t;
}
CH4_t = micros();
}
// Call able function
void init_rc_rx(){
Serial.print("Channel 1 connected to pin number....\t");
Serial.println(CH1_pin);
Serial.print("Channel 2 connected to pin number....\t");
Serial.println(CH2_pin);
Serial.print("Channel 3 connected to pin number....\t");
Serial.println(CH3_pin);
Serial.print("Channel 4 connected to pin number....\t");
Serial.println(CH4_pin);
pinMode(CH1_pin, INPUT);
pinMode(CH2_pin, INPUT);
pinMode(CH3_pin, INPUT);
pinMode(CH4_pin, INPUT);
attachInterrupt(CH1_int, CH1_int_ISR, CHANGE);
attachInterrupt(CH2_int, CH2_int_ISR, CHANGE);
attachInterrupt(CH3_int, CH3_int_ISR, CHANGE);
attachInterrupt(CH4_int, CH4_int_ISR, CHANGE);
}
void read_rc_rx(){
CH1 = ((float)CH1_delta-(float)min_high_time)*100/(max_high_time-min_high_time);
CH2 = ((float)CH2_delta-(float)min_high_time)*100/(max_high_time-min_high_time);
CH3 = ((float)CH3_delta-(float)min_high_time)*100/(max_high_time-min_high_time);
CH4 = ((float)CH4_delta-(float)min_high_time)*100/(max_high_time-min_high_time);
}
void setup()
{
Serial.begin(115200); // Initlizing serial port before calling init_rc_rx() function
init_rc_rx();
}
void loop()
{
t = micros()/1000;
read_rc_rx(); // CH1, CH2, CH3 and CH4 float variables will be calculated with this function is called
Serial.print(CH1);
Serial.print("\t");
Serial.print(CH2);
Serial.print("\t");
Serial.print(CH3);
Serial.print("\t");
Serial.print(CH4);
Serial.print("\t");
Serial.println(((micros()/1000)- (float)t)*100/dt);
while (dt > (micros()/1000)- t){
// do nothing
}
}