Syma s107g Helicopter controller
Dependencies: AccelSensor mbed
Fork of IR_demo by
Revision 2:2ffc4c0b0b90, committed 2014-12-02
- Comitter:
- wsmallwood3
- Date:
- Tue Dec 02 10:35:35 2014 +0000
- Parent:
- 1:6bdeedba218c
- Commit message:
- IR Helicopter Demo
Changed in this revision
AccelSensor.lib | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AccelSensor.lib Tue Dec 02 10:35:35 2014 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/Alegrowin/code/AccelSensor/#9e97f68b2654
--- a/main.cpp Wed Nov 26 19:10:21 2014 +0000 +++ b/main.cpp Tue Dec 02 10:35:35 2014 +0000 @@ -1,34 +1,34 @@ -//http://www.kerrywong.com/2012/08/27/reverse-engineering-the-syma-s107g-ir-protocol/ #include "mbed.h" - +#include "AccelSensor.h" PwmOut IRLED(p21); -//IR send and receive demo +AnalogIn UPDOWN(p16); +DigitalOut Flex(LED1); +Serial PC(USBTX, USBRX); +AccelSensor myAccel( p9,p10); //sda and scl int ROTATION_STATIONARY = 60; -int CAL_BYTE = 52; - +int CAL_BYTE = 52; int Throttle, LeftRight, FwdBack; - - +int acceldata[3]; void PutHeader(void) -{ // CODE HERE WILL RUN WHEN INTERUPT IS GENERATED +{ // This is the pulse and null for the header before each command IRLED = 0.5; -wait(.001964); +wait(.001964); //This is how long the pwm is on IRLED = 0.0; -wait(.001972); +wait(.001972); //This is how long the pwm is off } void PutFooter(void) -{ // CODE HERE WILL RUN WHEN INTERUPT IS GENERATED +{ // This is the pulse and null for the footer at the end of each command IRLED = 0.5; -wait(.000305); +wait(.000305); //Pulse length IRLED = 0.0; -wait(.088682); +wait(.088682); //Null length } - + void PutZero(void) -{ // CODE HERE WILL RUN WHEN INTERUPT IS GENERATED +{ // This is the pulse and null for a digital 0 IRLED = 0.5; wait(.000305); IRLED = 0.0; @@ -36,119 +36,72 @@ } void PutOne(void) -{ // CODE HERE WILL RUN WHEN INTERUPT IS GENERATED +{ // This is the pulse and null for a digital 1 IRLED = 0.5; wait(.000306); IRLED = 0.0; wait(0.000687); } +//This function takes in values for throttle, yaw and pitch and sends out the command. void sendCommand(int throttle, int leftRight, int forwardBackward) { int b; - PutHeader(); - + //LEFT/RIGHT control for (int i = 7; i >=0; i--) { b = ((ROTATION_STATIONARY + leftRight) & (1 << i)) >> i; - if (b > 0) PutOne(); else PutZero(); + if (b > 0) PutOne(); + else PutZero(); } - + //FORWARD/BACKWARD control for (int i = 7; i >=0; i--) { b = ((63 + forwardBackward) & (1 << i)) >> i; - if (b > 0) PutOne(); else PutZero(); - } - + if (b > 0) PutOne(); + else PutZero(); + } + //THROTTLE control for (int i = 7; i >=0; i--) { - b = (throttle & (1 << i)) >> i; - if (b > 0) PutOne(); else PutZero(); + b = (throttle & (1 << i)) >> i; + if (b > 0) PutOne(); + else PutZero(); } for (int i = 7; i >=0; i--) { b = (CAL_BYTE & (1 << i)) >> i; - if (b > 0) PutOne(); else PutZero(); + if (b > 0) PutOne(); + else PutZero(); } - PutFooter(); - -} - -void timerISR() -{ - //read control values from potentiometers - //Throttle = analogRead(0); - //LeftRight = analogRead(1); - //FwdBack = analogRead(2); - - //Throttle = Throttle / 4; //convert to 0 to 255 - Throttle = 65; - //LeftRight = LeftRight / 8 - 64; //convert to -64 to 63 - LeftRight = 0; //convert to -64 to 63 - //FwdBack = FwdBack / 4 - 128; //convert to -128 to 127 - FwdBack = 0; //convert to -128 to 127 - - sendCommand(Throttle, LeftRight, FwdBack); } - -void Thrust(void) //Test function to test flying. -{ - //Header - PutHeader(); - //First Byte - PutZero(); //1 - PutOne(); //2 - PutZero(); //3 - PutZero(); //4 - PutOne(); //5 - PutOne(); //6 - PutZero(); //7 - PutZero(); //8 - //Second Byte - PutZero(); //1 - PutZero(); //2 - PutOne(); //3 - PutOne(); //4 - PutOne(); //5 - PutOne(); //6 - PutOne(); //7 - PutOne(); //8 - //Third Byte - PutZero(); //1 - PutOne(); //2 - PutZero(); //3 - PutZero(); //4 - PutZero(); //5 - PutOne(); //6 - PutOne(); //7 - PutOne(); //8 - //Fourth Byte - PutZero(); //1 - PutOne(); //2 - PutOne(); //3 - PutZero(); //4 - PutOne(); //5 - PutOne(); //6 - PutOne(); //7 - PutZero(); //8 - //Footer - PutFooter(); -} +int main() { +Throttle = 45; //0-127 +LeftRight = 0; //convert to -60 (right) to 60(Left) +FwdBack = 0; //convert to -60(forward) to 60(Backward) +myAccel.init(); +myAccel.active(); + + IRLED.period(1.0/38000.0); //Drive IR LED data pin with 38Khz PWM Carrier - - - -int main() { - //IR Transmit code - IRLED.period(1.0/38000.0); - //Drive IR LED data pin with 38Khz PWM Carrier - while(1) { - //Thrust(); - timerISR(); - + while(1){ + myAccel.readData(acceldata); + LeftRight = int(-60*acceldata[1]/700); //X-axis + FwdBack = int(60*acceldata[0]/700); + if(LeftRight>60){LeftRight=60;} + if(LeftRight<-60){LeftRight=-60;} + if(FwdBack>60){FwdBack=60;} + if(FwdBack<-60){FwdBack=-60;} + if(UPDOWN>0.5) + {Throttle = 85; + Flex=1;} + else{Throttle=45; + Flex=0;} + sendCommand(Throttle, LeftRight, FwdBack); + PC.printf("Throttle:%d, LR:%d, FB:%d\n\r",Throttle,LeftRight,FwdBack); } }