LPC824

Dependencies:   mbed

Fork of CubeFine by wu le

Committer:
beian10
Date:
Fri Jul 01 09:41:15 2016 +0000
Revision:
1:54a2d380f8c7
Parent:
0:362c1482232c
Child:
2:7964622fb5a5
cube

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lixianyu 0:362c1482232c 1 #include "mbed.h"
lixianyu 0:362c1482232c 2 //#include "rtos.h"
lixianyu 0:362c1482232c 3 #include "MicroduinoPinNames.h"
lixianyu 0:362c1482232c 4 #include "userDef.h"
lixianyu 0:362c1482232c 5 #include "Protocol.h"
lixianyu 0:362c1482232c 6 #include "Microduino_Motor.h"
lixianyu 0:362c1482232c 7
beian10 1:54a2d380f8c7 8 DigitalOut led_red(D5);
beian10 1:54a2d380f8c7 9
lixianyu 0:362c1482232c 10 Timer g_cubeTimer;
beian10 1:54a2d380f8c7 11 //Timeout g_cubeTimeout;
lixianyu 0:362c1482232c 12 #if 1
lixianyu 0:362c1482232c 13 Motor MotorLeft(motor_pin0A, motor_pin0B);
lixianyu 0:362c1482232c 14 Motor MotorRight(motor_pin1A, motor_pin1B);
lixianyu 0:362c1482232c 15 ///////////////////////////////////////////////////////////
lixianyu 0:362c1482232c 16 #define CHANNEL_NUM 8
lixianyu 0:362c1482232c 17 uint16_t channalData[CHANNEL_NUM]; //8通道数据
lixianyu 0:362c1482232c 18 bool mode = 0; //nrf或者ble模式
lixianyu 0:362c1482232c 19 int16_t throttle = 0; //油门
lixianyu 0:362c1482232c 20 int16_t steering = 0; //转向
lixianyu 0:362c1482232c 21 int safe_ms = 0;
lixianyu 0:362c1482232c 22 DigitalOut myled(D13);
lixianyu 0:362c1482232c 23 InterruptIn wkp(D0);
lixianyu 0:362c1482232c 24 InterruptIn wkp1(D1);
lixianyu 0:362c1482232c 25 static long map(long x, long in_min, long in_max, long out_min, long out_max)
lixianyu 0:362c1482232c 26 {
lixianyu 0:362c1482232c 27 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
lixianyu 0:362c1482232c 28 }
lixianyu 0:362c1482232c 29
beian10 1:54a2d380f8c7 30 /*
lixianyu 0:362c1482232c 31 static void wake_up(void)
lixianyu 0:362c1482232c 32 {
lixianyu 0:362c1482232c 33 //myled = 0;
lixianyu 0:362c1482232c 34 }
beian10 1:54a2d380f8c7 35 */
lixianyu 0:362c1482232c 36
lixianyu 0:362c1482232c 37 int main()
lixianyu 0:362c1482232c 38 {
beian10 1:54a2d380f8c7 39 #if 0// To test wake up from deepsleep(), failed....but wake up from sleep() work. sleep() will reduce 2mA.
lixianyu 0:362c1482232c 40 wkp.fall(wake_up);
lixianyu 0:362c1482232c 41 wkp.rise(&wake_up);
lixianyu 0:362c1482232c 42 wkp1.fall(&wake_up);
lixianyu 0:362c1482232c 43 wkp1.rise(&wake_up);
lixianyu 0:362c1482232c 44 #endif
beian10 1:54a2d380f8c7 45 led_red = 0;
lixianyu 0:362c1482232c 46 //myled = 1;
beian10 1:54a2d380f8c7 47 //g_cubeTimer.start();
lixianyu 0:362c1482232c 48
lixianyu 0:362c1482232c 49 mode = protocolSetup(); //遥控接收器初始化
lixianyu 0:362c1482232c 50
lixianyu 0:362c1482232c 51 MotorLeft.Fix(motor_fixL);
lixianyu 0:362c1482232c 52 MotorRight.Fix(motor_fixR);
beian10 1:54a2d380f8c7 53
beian10 1:54a2d380f8c7 54 MotorLeft.Driver(255);
beian10 1:54a2d380f8c7 55 MotorRight.Driver(255);
beian10 1:54a2d380f8c7 56
beian10 1:54a2d380f8c7 57 wait(0.5);
lixianyu 0:362c1482232c 58 //mypc.baud(115200);
beian10 1:54a2d380f8c7 59
lixianyu 0:362c1482232c 60 while (1) {
beian10 1:54a2d380f8c7 61
lixianyu 0:362c1482232c 62 if (protocolRead(channalData, mode)) { //判断是否接收到遥控信号
lixianyu 0:362c1482232c 63 throttle = map(channalData[CHANNEL_THROTTLE], 1000, 2000, -MAX_THROTTLE, MAX_THROTTLE);
lixianyu 0:362c1482232c 64 steering = map(channalData[CHANNEL_STEERING], 1000, 2000, -MAX_STEERING, MAX_STEERING);
lixianyu 0:362c1482232c 65
lixianyu 0:362c1482232c 66 MotorLeft.Driver(MotorLeft.GetData(throttle, steering, CHAN_LEFT));
lixianyu 0:362c1482232c 67 MotorRight.Driver(MotorRight.GetData(throttle, steering, CHAN_RIGHT));
lixianyu 0:362c1482232c 68
lixianyu 0:362c1482232c 69 safe_ms = g_cubeTimer.read_ms();
lixianyu 0:362c1482232c 70 }
lixianyu 0:362c1482232c 71
lixianyu 0:362c1482232c 72 if (safe_ms > g_cubeTimer.read_ms()) {
lixianyu 0:362c1482232c 73 safe_ms = g_cubeTimer.read_ms();
lixianyu 0:362c1482232c 74 }
lixianyu 0:362c1482232c 75 if (g_cubeTimer.read_ms() - safe_ms > SAFE_TIME_OUT) {
lixianyu 0:362c1482232c 76 MotorLeft.Free();
lixianyu 0:362c1482232c 77 MotorRight.Free();
beian10 1:54a2d380f8c7 78 //sleep();//待机待机电流可减少4mA左右
lixianyu 0:362c1482232c 79 //deepsleep();
lixianyu 0:362c1482232c 80 }
beian10 1:54a2d380f8c7 81
lixianyu 0:362c1482232c 82 } // while
lixianyu 0:362c1482232c 83 }
beian10 1:54a2d380f8c7 84
lixianyu 0:362c1482232c 85 #else
lixianyu 0:362c1482232c 86 DigitalOut myled(D13);
lixianyu 0:362c1482232c 87 PwmOut PWM_A(motor_pin0A);
lixianyu 0:362c1482232c 88 PwmOut PWM_B(motor_pin0B);
lixianyu 0:362c1482232c 89 int pv = 0;
lixianyu 0:362c1482232c 90 int main()
lixianyu 0:362c1482232c 91 {
lixianyu 0:362c1482232c 92 myled.write(1);
lixianyu 0:362c1482232c 93 PWM_A.period_us(255);
lixianyu 0:362c1482232c 94 PWM_B.period_us(255);
lixianyu 0:362c1482232c 95
lixianyu 0:362c1482232c 96 PWM_A.pulsewidth_us(200);
lixianyu 0:362c1482232c 97 PWM_B.pulsewidth_us(0);
lixianyu 0:362c1482232c 98 while (1) {
lixianyu 0:362c1482232c 99 #if 0
lixianyu 0:362c1482232c 100 PWM_B.pulsewidth_us(pv);
lixianyu 0:362c1482232c 101 pv++;
lixianyu 0:362c1482232c 102 #endif
lixianyu 0:362c1482232c 103 wait(0.5);
lixianyu 0:362c1482232c 104 myled = !myled;
lixianyu 0:362c1482232c 105 if (pv >= 255) {
lixianyu 0:362c1482232c 106 pv = 0;
lixianyu 0:362c1482232c 107 }
lixianyu 0:362c1482232c 108 }
lixianyu 0:362c1482232c 109 }
lixianyu 0:362c1482232c 110 #endif