work fine.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 //#include "rtos.h"
00003 #include "MicroduinoPinNames.h"
00004 #include "userDef.h"
00005 #include "Protocol.h"
00006 #include "Microduino_Motor.h"
00007 
00008 Timer g_cubeTimer;
00009 Timeout g_cubeTimeout;
00010 #if 1
00011 Motor MotorLeft(motor_pin0A, motor_pin0B);
00012 Motor MotorRight(motor_pin1A, motor_pin1B);
00013 ///////////////////////////////////////////////////////////
00014 #define CHANNEL_NUM 8
00015 uint16_t channalData[CHANNEL_NUM]; //8通道数据
00016 bool mode = 0; //nrf或者ble模式
00017 int16_t throttle = 0; //油门
00018 int16_t steering = 0; //转向
00019 int safe_ms = 0;
00020 DigitalOut myled(D13);
00021 InterruptIn wkp(D0);
00022 InterruptIn wkp1(D1);
00023 static long map(long x, long in_min, long in_max, long out_min, long out_max)
00024 {
00025     return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
00026 }
00027 
00028 static void wake_up(void)
00029 {
00030     //myled = 0;
00031 }
00032 
00033 int main()
00034 {
00035 #if 1// To test wake up from deepsleep(), failed....but wake up from sleep() work. sleep() will reduce 2mA.
00036     wkp.fall(wake_up);
00037     wkp.rise(&wake_up);
00038     wkp1.fall(&wake_up);
00039     wkp1.rise(&wake_up);
00040 #endif
00041     //myled = 1;
00042     g_cubeTimer.start();
00043 
00044     mode = protocolSetup();  //遥控接收器初始化
00045 
00046     MotorLeft.Fix(motor_fixL);
00047     MotorRight.Fix(motor_fixR);
00048     //mypc.baud(115200);
00049     while (1) {
00050         if (protocolRead(channalData, mode)) { //判断是否接收到遥控信号
00051             throttle = map(channalData[CHANNEL_THROTTLE], 1000, 2000, -MAX_THROTTLE, MAX_THROTTLE);
00052             steering = map(channalData[CHANNEL_STEERING], 1000, 2000, -MAX_STEERING, MAX_STEERING);
00053 
00054             MotorLeft.Driver(MotorLeft.GetData(throttle, steering, CHAN_LEFT));
00055             MotorRight.Driver(MotorRight.GetData(throttle, steering, CHAN_RIGHT));
00056 
00057 #ifdef _DEBUG
00058             Serial.print("DATA OK :[");
00059             for (int a = 0; a < CHANNEL_NUM; a++) {
00060                 Serial.print(channalData[a]);
00061                 Serial.print(" ");
00062             }
00063             Serial.print("],throttle:");
00064             Serial.print(throttle);
00065             Serial.print(",steering:");
00066             Serial.println(steering);
00067 #endif
00068             safe_ms = g_cubeTimer.read_ms();
00069         }
00070 
00071         if (safe_ms > g_cubeTimer.read_ms()) {
00072             safe_ms = g_cubeTimer.read_ms();
00073         }
00074         if (g_cubeTimer.read_ms() - safe_ms > SAFE_TIME_OUT) {
00075             MotorLeft.Free();
00076             MotorRight.Free();
00077             sleep();//待机待机电流可减少4mA左右
00078             //deepsleep();
00079         }
00080     } // while
00081 }
00082 #else
00083 DigitalOut myled(D13);
00084 PwmOut PWM_A(motor_pin0A);
00085 PwmOut PWM_B(motor_pin0B);
00086 int pv = 0;
00087 int main()
00088 {
00089     myled.write(1);
00090     PWM_A.period_us(255);
00091     PWM_B.period_us(255);
00092 
00093     PWM_A.pulsewidth_us(200);
00094     PWM_B.pulsewidth_us(0);
00095     while (1) {
00096 #if 0
00097         PWM_B.pulsewidth_us(pv);
00098         pv++;
00099 #endif
00100         wait(0.5);
00101         myled = !myled;
00102         if (pv >= 255) {
00103             pv = 0;
00104         }
00105     }
00106 }
00107 #endif