Created
December 2, 2012 15:29
-
-
Save lacion/4189325 to your computer and use it in GitHub Desktop.
Arduino RC Car
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Do not remove the include below | |
#include "arduino.h" | |
#include "config.h" | |
#include "ardurc.h" | |
#include "libs/rx.h" | |
Rx rx(THROTTLE, YAW, AUX); | |
//The setup function is called once at startup of the sketch | |
void setup() | |
{ | |
Serial.begin(BAUD); | |
} | |
// The loop function is called in an endless loop | |
void loop() | |
{ | |
rx.ReadRawChanellData(); | |
Serial.print("Throttle: "); | |
Serial.print(rx.getChannel(CH_2)); | |
Serial.print(" Yaw: "); | |
Serial.print(rx.getChannel(CH_3)); | |
Serial.print(" Aux: "); | |
Serial.print(rx.getChannel(CH_1)); | |
Serial.println(""); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Only modify this file to include | |
// - function definitions (prototypes) | |
// - include files | |
// - extern variable definitions | |
// In the appropriate section | |
#ifndef ardurc_H_ | |
#define ardurc_H_ | |
#include "Arduino.h" | |
//add your includes for the project ardurc here | |
//end of add your includes here | |
#ifdef __cplusplus | |
extern "C" { | |
#endif | |
void loop(); | |
void setup(); | |
#ifdef __cplusplus | |
} // extern "C" | |
#endif | |
//add your function definitions for the project ardurc here | |
//Do not add code below this line | |
#endif /* ardurc_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _CONFIG_H_ | |
#define _CONFIG_H_ | |
#define BAUD 115200 | |
#define THROTTLE 5 | |
#define YAW 6 | |
#define AUX 7 | |
#define CH_1 0 | |
#define CH_2 1 | |
#define CH_3 2 | |
#define NEUTRAL 1500 | |
#define MAX 2000 | |
#define MIN 1000 | |
//Arduino Pins PORT | |
//------------ ---- | |
//Digital 0-7 D | |
//Digital 8-13 B | |
//Analog 0-5 C | |
#define NO_PORTB_PINCHANGES | |
#define NO_PORTC_PINCHANGES | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// | |
// | |
#include "Arduino.h" | |
#include "avr/io.h" | |
#include "Config.h" | |
#include "libs/rx.h" | |
#include "PinChangeInt.h" | |
Rx* prx; | |
void ThrottleWrapper() | |
{ | |
prx->getThrottle(); | |
} | |
void YawWrapper() | |
{ | |
prx->getYaw(); | |
} | |
void AUXWrapper() | |
{ | |
prx->getAux(); | |
} | |
Rx::Rx(byte ch1, byte ch2, byte ch3) | |
{ | |
prx = this; | |
bUpdateFlags = 0; | |
THROTTLE_FLAG = 1; | |
STEERING_FLAG = 2; | |
AUX_FLAG = 4; | |
uint16_t ChannelRawData[NUM_CHANNELS] = {0,0,0}; | |
uint16_t ChannelInStart[NUM_CHANNELS] = {0,0,0}; | |
uint16_t ChannelData[NUM_CHANNELS] = {0,0,0}; | |
this->SetTimer(); | |
PCintPort::attachInterrupt(ch1, ThrottleWrapper,CHANGE); | |
PCintPort::attachInterrupt(ch2, YawWrapper,CHANGE); | |
PCintPort::attachInterrupt(ch3, AUXWrapper,CHANGE); | |
} | |
void Rx::SetTimer() | |
{ | |
TCNT1 = 0; // clear the timer count | |
// Initilialise Timer1 | |
TCCR1A = 0; // normal counting mode | |
TCCR1B = 2; // set prescaler of 64 = 1 tick = 4us | |
OCR1A = TCNT1 + 4000; // Start in two milli seconds | |
} | |
uint16_t Rx::getChannel(byte ch) | |
{ | |
return ChannelData[ch]; | |
} | |
void Rx::ReadRawChanellData() | |
{ | |
if(bUpdateFlagsShared) | |
{ | |
noInterrupts(); // turn interrupts off quickly while we take local copies of the shared variables | |
bUpdateFlags = bUpdateFlagsShared; | |
if(bUpdateFlags & THROTTLE_FLAG) | |
{ | |
ChannelData[CH_1] = ChannelRawData[CH_1]; | |
} | |
if(bUpdateFlags & STEERING_FLAG) | |
{ | |
ChannelData[CH_2] = ChannelRawData[CH_2]; | |
} | |
if(bUpdateFlags & AUX_FLAG) | |
{ | |
ChannelData[CH_3] = ChannelRawData[CH_3]; | |
} | |
bUpdateFlagsShared = 0; | |
interrupts(); | |
} | |
bUpdateFlags = 0; | |
} | |
void Rx::getThrottle() | |
{ | |
if(PCintPort::pinState) | |
{ | |
ChannelInStart[CH_1] = TCNT1; | |
} | |
else | |
{ | |
ChannelRawData[CH_1] = (TCNT1 - ChannelInStart[CH_1])>>1; | |
bUpdateFlagsShared |= THROTTLE_FLAG; | |
} | |
} | |
void Rx::getYaw() | |
{ | |
if(PCintPort::pinState) | |
{ | |
ChannelInStart[CH_2] = TCNT1; | |
} | |
else | |
{ | |
ChannelRawData[CH_2] = (TCNT1 - ChannelInStart[CH_2])>>1; | |
bUpdateFlagsShared |= STEERING_FLAG; | |
} | |
} | |
void Rx::getAux() | |
{ | |
if(PCintPort::pinState) | |
{ | |
ChannelInStart[CH_3] = TCNT1; | |
} | |
else | |
{ | |
ChannelRawData[CH_3] = (TCNT1 - ChannelInStart[CH_3])>>1; | |
bUpdateFlagsShared |= AUX_FLAG; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// rx.h | |
#ifndef _RX_h | |
#define _RX_h | |
#include "Arduino.h" | |
#include "Config.h" | |
#define NUM_CHANNELS 3 | |
class Rx | |
{ | |
private: | |
void SetTimer(); | |
uint8_t THROTTLE_FLAG; | |
uint8_t STEERING_FLAG; | |
uint8_t AUX_FLAG; | |
volatile uint8_t bUpdateFlagsShared; | |
volatile uint16_t ChannelRawData[NUM_CHANNELS]; | |
uint16_t ChannelInStart[NUM_CHANNELS]; | |
uint16_t ChannelData[NUM_CHANNELS]; | |
uint8_t bUpdateFlags; | |
public: | |
Rx(byte ch1, byte ch2, byte ch3); | |
void getThrottle(); | |
void getYaw(); | |
void getAux(); | |
void ReadRawChanellData(); | |
uint16_t getChannel(byte ch); | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment