新型モータードライバのメインプログラム

Dependencies:   mbed

Committer:
SES01
Date:
Sat May 14 12:23:29 2016 +0000
Revision:
2:c77482250d0b
Parent:
1:e45830806a1c
Child:
3:65eaf88bf278
?????????????????????????????????????; ????????????????????????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SES01 0:cf3829614495 1 #include "mbed.h"
SES01 0:cf3829614495 2
SES01 1:e45830806a1c 3 //重要受信データのマスク
SES01 1:e45830806a1c 4 #define LEAD_PACKET_MASK 0b10000000
SES01 1:e45830806a1c 5 #define PACKET_ADDRESS_MASK 0b00011111
SES01 1:e45830806a1c 6 #define PACKET_MODE_MASK 0b01100000
SES01 1:e45830806a1c 7 #define PACKET_DATA_MASK 0b00111111
SES01 1:e45830806a1c 8
SES01 1:e45830806a1c 9 //モータードライバの動作モード
SES01 1:e45830806a1c 10 #define ORDER_FOLLOWING_MODE 0
SES01 1:e45830806a1c 11 #define ENCODER_FOLLOWING_MODE 1
SES01 2:c77482250d0b 12 #define SINGLE_ORDER_MODE 2 //単命令を受信した後は必ずsendback
SES01 2:c77482250d0b 13
SES01 2:c77482250d0b 14 //単命令動作の種類
SES01 2:c77482250d0b 15 #define S_ORDER_FREQUENCY 0
SES01 2:c77482250d0b 16 #define S_ORDER_SEND_POWER 1
SES01 2:c77482250d0b 17 #define S_ORDER_LINEAR_AUTOCHANGE 2
SES01 2:c77482250d0b 18 #define S_ORDER_CERTAIN 3
SES01 2:c77482250d0b 19 #define S_ORDER_SEND_ENCODER 4
SES01 1:e45830806a1c 20
SES01 0:cf3829614495 21 #define F_MD 18000 //PWMキャリア周波数設定
SES01 0:cf3829614495 22
SES01 0:cf3829614495 23 /*
SES01 0:cf3829614495 24 * High_A High_B
SES01 0:cf3829614495 25 * ┃ ┃
SES01 0:cf3829614495 26 * ┃ ┃
SES01 0:cf3829614495 27 * ┣━━━┫
SES01 0:cf3829614495 28 * ┃ ┃
SES01 0:cf3829614495 29 * ┃ ┃
SES01 0:cf3829614495 30 * Low_A Low_B
SES01 0:cf3829614495 31 */
SES01 0:cf3829614495 32
SES01 0:cf3829614495 33 Serial comPort(dp16, dp15);
SES01 0:cf3829614495 34 DigitalOut driverEnable(dp28);
SES01 1:e45830806a1c 35 DigitalOut LED(dp24);
SES01 1:e45830806a1c 36 //dp5 = ハイサイドPWM1, dp27 = ハイサイドPWM2 (オープンドレイン)
SES01 0:cf3829614495 37 //dp1 = ローサイドPWM1, dp2 = ローサイドPWM2
SES01 0:cf3829614495 38 PwmOut Low_A(dp1);
SES01 0:cf3829614495 39 PwmOut Low_B(dp2);
SES01 0:cf3829614495 40 DigitalOut High_A(dp5);
SES01 0:cf3829614495 41 DigitalOut High_B(dp27);
SES01 0:cf3829614495 42
SES01 0:cf3829614495 43 AnalogIn ain(dp4);
SES01 1:e45830806a1c 44
SES01 1:e45830806a1c 45 //グローバル変数
SES01 1:e45830806a1c 46 uint8_t lastRXdata = 0;
SES01 1:e45830806a1c 47 uint8_t address = 0;
SES01 1:e45830806a1c 48 uint8_t mode = 0;
SES01 1:e45830806a1c 49 uint8_t nowOrderAddress = 0;
SES01 1:e45830806a1c 50 uint8_t dataNumber = 0;
SES01 1:e45830806a1c 51 uint8_t sendPowerFlag = 0;
SES01 1:e45830806a1c 52
SES01 1:e45830806a1c 53 void orderFollowManager(uint8_t reciveData, uint8_t dataNumber){
SES01 2:c77482250d0b 54 switch (dataNumber) {
SES01 2:c77482250d0b 55 case 2:
SES01 2:c77482250d0b 56 break;
SES01 2:c77482250d0b 57 case 3:
SES01 2:c77482250d0b 58 break;
SES01 2:c77482250d0b 59 case 4:
SES01 2:c77482250d0b 60 break;
SES01 2:c77482250d0b 61 }
SES01 1:e45830806a1c 62 }
SES01 1:e45830806a1c 63
SES01 1:e45830806a1c 64 void encoderFollowManager(uint8_t reciveData, uint8_t dataNumber){
SES01 1:e45830806a1c 65
SES01 1:e45830806a1c 66 }
SES01 1:e45830806a1c 67
SES01 1:e45830806a1c 68 void S_orderFollowManager(uint8_t reciveData, uint8_t dataNumber){
SES01 1:e45830806a1c 69
SES01 1:e45830806a1c 70 }
SES01 1:e45830806a1c 71
SES01 0:cf3829614495 72 void com_rx() {
SES01 1:e45830806a1c 73 static uint8_t reciveData;
SES01 1:e45830806a1c 74 reciveData = comPort.getc();
SES01 1:e45830806a1c 75
SES01 1:e45830806a1c 76 if (reciveData | LEAD_PACKET_MASK) {
SES01 1:e45830806a1c 77 nowOrderAddress = reciveData | PACKET_ADDRESS_MASK;
SES01 1:e45830806a1c 78 dataNumber = 0;
SES01 1:e45830806a1c 79 }
SES01 1:e45830806a1c 80
SES01 1:e45830806a1c 81 if (nowOrderAddress == address) {
SES01 1:e45830806a1c 82 dataNumber++;
SES01 1:e45830806a1c 83 lastRXdata = reciveData | PACKET_DATA_MASK;
SES01 0:cf3829614495 84
SES01 1:e45830806a1c 85 if (dataNumber == 1) {
SES01 1:e45830806a1c 86 mode = (reciveData | PACKET_MODE_MASK) >> 5;
SES01 1:e45830806a1c 87 } else {
SES01 1:e45830806a1c 88 switch (mode) {
SES01 1:e45830806a1c 89 case ORDER_FOLLOWING_MODE:
SES01 1:e45830806a1c 90 orderFollowManager(reciveData, dataNumber);
SES01 1:e45830806a1c 91 break;
SES01 1:e45830806a1c 92 case ENCODER_FOLLOWING_MODE:
SES01 1:e45830806a1c 93 encoderFollowManager(reciveData, dataNumber);
SES01 1:e45830806a1c 94 break;
SES01 1:e45830806a1c 95 case SINGLE_ORDER_MODE:
SES01 1:e45830806a1c 96 S_orderFollowManager(reciveData, dataNumber);
SES01 1:e45830806a1c 97 break;
SES01 1:e45830806a1c 98 }
SES01 1:e45830806a1c 99 }
SES01 1:e45830806a1c 100 }
SES01 0:cf3829614495 101 }
SES01 0:cf3829614495 102
SES01 0:cf3829614495 103 int main() {
SES01 1:e45830806a1c 104 comPort.baud(9600);
SES01 1:e45830806a1c 105 comPort.format(8, Serial::Odd, 1);
SES01 0:cf3829614495 106 comPort.attach(&com_rx, Serial::RxIrq);
SES01 0:cf3829614495 107
SES01 0:cf3829614495 108 int FP_MD = 1000000 / F_MD; //PWM一周期の周期(マイクロ秒)
SES01 0:cf3829614495 109
SES01 0:cf3829614495 110 High_A = 0;
SES01 1:e45830806a1c 111 High_B = 0;
SES01 0:cf3829614495 112
SES01 0:cf3829614495 113 Low_A.period_us(FP_MD);
SES01 0:cf3829614495 114 Low_A.write(0);
SES01 0:cf3829614495 115 Low_B.period_us(FP_MD);
SES01 0:cf3829614495 116 Low_B.write(0);
SES01 0:cf3829614495 117 //初期設定完了
SES01 0:cf3829614495 118
SES01 1:e45830806a1c 119 LED = 1;
SES01 1:e45830806a1c 120 High_B = 1;
SES01 0:cf3829614495 121 driverEnable = 1;
SES01 0:cf3829614495 122 while (1) {
SES01 0:cf3829614495 123 Low_A.write((ain.read() - 0.2) * 1.25 < 0 ? 0 : (ain.read() - 0.2) * 1.25);
SES01 0:cf3829614495 124 //pc.printf("%f\n", (ain.read() - 0.2) * 1.25 < 0 ? 0 : (ain.read() - 0.2) * 1.25);
SES01 0:cf3829614495 125 comPort.printf("%d", 0b01010101);
SES01 0:cf3829614495 126 }
SES01 0:cf3829614495 127 }