Microduino的cube小车。

Dependencies:   mbed-rtos 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 #if 1
00010 Motor MotorLeft(motor_pin0A, motor_pin0B);
00011 Motor MotorRight(motor_pin1A, motor_pin1B);
00012 ///////////////////////////////////////////////////////////
00013 #define CHANNEL_NUM 8
00014 uint16_t channalData[CHANNEL_NUM]; //8通道数据
00015 bool mode = 0; //nrf或者ble模式
00016 int16_t throttle = 0; //油门
00017 int16_t steering = 0; //转向
00018 int safe_ms = 0;
00019 
00020 //Serial pc(P0_4,P0_0);
00021 
00022 static long map(long x, long in_min, long in_max, long out_min, long out_max)
00023 {
00024     return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
00025 }
00026 
00027 int main()
00028 {
00029     bool freeFlag = true;
00030     g_cubeTimer.start();
00031     mode = protocolSetup();  //遥控接收器初始化
00032 
00033     MotorLeft.Fix(motor_fixL);
00034     MotorRight.Fix(motor_fixR);
00035     //pc.baud(115200);
00036     while(1) {
00037         if (protocolRead(channalData, mode)) { //判断是否接收到遥控信号
00038             throttle = map(channalData[CHANNEL_THROTTLE], 1000, 2000, -MAX_THROTTLE, MAX_THROTTLE);
00039             steering = map(channalData[CHANNEL_STEERING], 1000, 2000, -MAX_STEERING, MAX_STEERING);
00040 
00041             MotorLeft.Driver(MotorLeft.GetData(throttle, steering, CHAN_LEFT));
00042             MotorRight.Driver(MotorRight.GetData(throttle, steering, CHAN_RIGHT));
00043 
00044 #ifdef _DEBUG
00045 asdf
00046             Serial.print("DATA OK :[");
00047             for (int a = 0; a < CHANNEL_NUM; a++) {
00048                 Serial.print(channalData[a]);
00049                 Serial.print(" ");
00050             }
00051             Serial.print("],throttle:");
00052             Serial.print(throttle);
00053             Serial.print(",steering:");
00054             Serial.println(steering);
00055 #endif
00056             safe_ms = g_cubeTimer.read_ms();
00057         }
00058 #if 0
00059         if (safe_ms > g_cubeTimer.read_ms()) {
00060             safe_ms = g_cubeTimer.read_ms();
00061         }
00062         if (g_cubeTimer.read_ms() - safe_ms > SAFE_TIME_OUT) {
00063             MotorLeft.Free();
00064             MotorRight.Free();
00065             //MotorLeft.Brake();
00066             //MotorRight.Brake();
00067         }
00068 #endif
00069         //pc.printf("Hello world\r\n");
00070         //sleep();
00071     }
00072 }
00073 #else
00074 DigitalOut myled(D13);
00075 PwmOut PWM_A(motor_pin0A);
00076 PwmOut PWM_B(motor_pin0B);
00077 int pv = 0;
00078 int main()
00079 {
00080     myled.write(1);
00081     PWM_A.period_us(255);
00082     PWM_B.period_us(255);
00083     
00084     PWM_A.pulsewidth_us(200);
00085     PWM_B.pulsewidth_us(0);
00086     while (1)
00087     {
00088         #if 0
00089         PWM_B.pulsewidth_us(pv);
00090         pv++;
00091         #endif
00092         wait(0.5);
00093         myled = !myled;
00094         if (pv >= 255)
00095         {
00096             pv = 0;
00097         }
00098     }
00099 }
00100 #endif