Microduino

Dependencies:   mbed

Fork of CubeFine by Li Weiyi

Committer:
lixianyu
Date:
Thu Jun 02 04:03:31 2016 +0000
Revision:
0:362c1482232c
work fine.

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