Quadcopter control software

Dependencies:   Pulse USBDevice mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "Pulse.h"
00003 #include "USBSerial.h"
00004 //#include "Servo.h"
00005 
00006 #define Motor1_Pin                      PTA12      //D3 PWM port
00007 #define AnalogIn_Pin                    PTD1       //A0 Port
00008 
00009 #define I2C_SDA_Pin                     PTB3
00010 #define I2C_SCL_Pin                     PTB2
00011 #define accelerometerAddress            0x6B
00012 
00013 
00014 
00015 DigitalOut      Motor1(Motor1_Pin);
00016 AnalogIn        MotorPowerLevel(AnalogIn_Pin);
00017 
00018 
00019 USBSerial serial; //Virtual serial port over USB
00020 
00021 I2C i2c(I2C_SDA_Pin,I2C_SCL_Pin);
00022 
00023 Ticker ticker1;
00024 
00025 int motor1Status = 0;
00026 
00027 int frequency = 250;
00028 float period = 1.0/frequency;
00029 
00030 
00031 float currentPowerLevel = 0;
00032 
00033 void motor1Pulse(){
00034     ticker1.detach();
00035     if(motor1Status == 0){
00036         Motor1.write(1);
00037         motor1Status = 1;
00038         ticker1.attach(&motor1Pulse, (currentPowerLevel+1.0)/1000.0);
00039     }
00040    else{
00041         Motor1.write(0);
00042         motor1Status = 0;
00043         ticker1.attach(&motor1Pulse, period-(currentPowerLevel+1.0)/1000.0);
00044     }
00045    
00046 }
00047 /*void motor1Pulse(){
00048        ticker1.detach();
00049        Motor1.write(0);
00050        ticker1.attach(&motor1OnPulse, currentPowerLevel/1000.0);      
00051 }*/
00052 
00053 
00054 int main()
00055 {   
00056     //PWM_Motor1.period(.004); //set motor PWM frequency to 250 Hz
00057     //PWM_Motor1.period(.01); //set motor PWM frequency to 100 Hz
00058     //PWM_Motor1 = .5; //any value from 0 to 1
00059     //PWM_Motor1.pulsewidth_ms(2.0);
00060     //wait(4);
00061     //PWM_Motor1.pulsewidth_ms(1.0);
00062     
00063     motor1Status = 1;
00064     ticker1.attach(&motor1Pulse, (currentPowerLevel+1.0)/1000.0);
00065     
00066     
00067     while(1){
00068         currentPowerLevel = MotorPowerLevel.read();
00069         
00070         i2c.read
00071         serial.printf("I am a virtual serial port\r\n");
00072         wait(1);
00073         //PWM_Motor1.pulsewidth(((1.0+currentPowerLevel)/1000.0));       
00074         
00075     }
00076 }