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

Dependencies:   mbed

Committer:
SES01
Date:
Sun May 15 00:50:05 2016 +0000
Revision:
3:65eaf88bf278
Parent:
2:c77482250d0b
Child:
4:614e752487cc
7?????MB??????; ??????????????????????????????????; ??????????

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 3:65eaf88bf278 34 DigitalOut driverEnable(dp7);
SES01 3:65eaf88bf278 35 DigitalOut LED1(LED1);
SES01 3:65eaf88bf278 36 DigitalOut LED2(LED2);
SES01 3:65eaf88bf278 37 BusIn switch5(dp4, dp6, dp18, dp25, dp26);
SES01 1:e45830806a1c 38 //dp5 = ハイサイドPWM1, dp27 = ハイサイドPWM2 (オープンドレイン)
SES01 0:cf3829614495 39 //dp1 = ローサイドPWM1, dp2 = ローサイドPWM2
SES01 0:cf3829614495 40 PwmOut Low_A(dp1);
SES01 0:cf3829614495 41 PwmOut Low_B(dp2);
SES01 0:cf3829614495 42 DigitalOut High_A(dp5);
SES01 0:cf3829614495 43 DigitalOut High_B(dp27);
SES01 0:cf3829614495 44
SES01 3:65eaf88bf278 45 AnalogIn ain(dp9);
SES01 3:65eaf88bf278 46 //PortIn encoder(Port0, 0b111);
SES01 3:65eaf88bf278 47
SES01 1:e45830806a1c 48
SES01 1:e45830806a1c 49 //グローバル変数
SES01 1:e45830806a1c 50 uint8_t lastRXdata = 0;
SES01 1:e45830806a1c 51 uint8_t address = 0;
SES01 1:e45830806a1c 52 uint8_t mode = 0;
SES01 1:e45830806a1c 53 uint8_t nowOrderAddress = 0;
SES01 1:e45830806a1c 54 uint8_t dataNumber = 0;
SES01 1:e45830806a1c 55 uint8_t sendPowerFlag = 0;
SES01 1:e45830806a1c 56
SES01 1:e45830806a1c 57 void orderFollowManager(uint8_t reciveData, uint8_t dataNumber){
SES01 2:c77482250d0b 58 switch (dataNumber) {
SES01 2:c77482250d0b 59 case 2:
SES01 2:c77482250d0b 60 break;
SES01 2:c77482250d0b 61 case 3:
SES01 2:c77482250d0b 62 break;
SES01 2:c77482250d0b 63 case 4:
SES01 2:c77482250d0b 64 break;
SES01 2:c77482250d0b 65 }
SES01 1:e45830806a1c 66 }
SES01 1:e45830806a1c 67
SES01 1:e45830806a1c 68 void encoderFollowManager(uint8_t reciveData, uint8_t dataNumber){
SES01 1:e45830806a1c 69
SES01 1:e45830806a1c 70 }
SES01 1:e45830806a1c 71
SES01 1:e45830806a1c 72 void S_orderFollowManager(uint8_t reciveData, uint8_t dataNumber){
SES01 1:e45830806a1c 73
SES01 1:e45830806a1c 74 }
SES01 1:e45830806a1c 75
SES01 0:cf3829614495 76 void com_rx() {
SES01 1:e45830806a1c 77 static uint8_t reciveData;
SES01 1:e45830806a1c 78 reciveData = comPort.getc();
SES01 1:e45830806a1c 79
SES01 1:e45830806a1c 80 if (reciveData | LEAD_PACKET_MASK) {
SES01 1:e45830806a1c 81 nowOrderAddress = reciveData | PACKET_ADDRESS_MASK;
SES01 1:e45830806a1c 82 dataNumber = 0;
SES01 1:e45830806a1c 83 }
SES01 1:e45830806a1c 84
SES01 1:e45830806a1c 85 if (nowOrderAddress == address) {
SES01 1:e45830806a1c 86 dataNumber++;
SES01 1:e45830806a1c 87 lastRXdata = reciveData | PACKET_DATA_MASK;
SES01 0:cf3829614495 88
SES01 1:e45830806a1c 89 if (dataNumber == 1) {
SES01 1:e45830806a1c 90 mode = (reciveData | PACKET_MODE_MASK) >> 5;
SES01 1:e45830806a1c 91 } else {
SES01 1:e45830806a1c 92 switch (mode) {
SES01 1:e45830806a1c 93 case ORDER_FOLLOWING_MODE:
SES01 1:e45830806a1c 94 orderFollowManager(reciveData, dataNumber);
SES01 1:e45830806a1c 95 break;
SES01 1:e45830806a1c 96 case ENCODER_FOLLOWING_MODE:
SES01 1:e45830806a1c 97 encoderFollowManager(reciveData, dataNumber);
SES01 1:e45830806a1c 98 break;
SES01 1:e45830806a1c 99 case SINGLE_ORDER_MODE:
SES01 1:e45830806a1c 100 S_orderFollowManager(reciveData, dataNumber);
SES01 1:e45830806a1c 101 break;
SES01 1:e45830806a1c 102 }
SES01 1:e45830806a1c 103 }
SES01 1:e45830806a1c 104 }
SES01 0:cf3829614495 105 }
SES01 0:cf3829614495 106
SES01 0:cf3829614495 107 int main() {
SES01 1:e45830806a1c 108 comPort.baud(9600);
SES01 1:e45830806a1c 109 comPort.format(8, Serial::Odd, 1);
SES01 0:cf3829614495 110 comPort.attach(&com_rx, Serial::RxIrq);
SES01 0:cf3829614495 111
SES01 0:cf3829614495 112 int FP_MD = 1000000 / F_MD; //PWM一周期の周期(マイクロ秒)
SES01 0:cf3829614495 113
SES01 0:cf3829614495 114 High_A = 0;
SES01 1:e45830806a1c 115 High_B = 0;
SES01 0:cf3829614495 116 Low_A.period_us(FP_MD);
SES01 0:cf3829614495 117 Low_A.write(0);
SES01 0:cf3829614495 118 Low_B.period_us(FP_MD);
SES01 0:cf3829614495 119 Low_B.write(0);
SES01 3:65eaf88bf278 120
SES01 3:65eaf88bf278 121 switch5.mode(PullUp);
SES01 3:65eaf88bf278 122 address = switch5.read();
SES01 3:65eaf88bf278 123 comPort.printf("%d", address);
SES01 0:cf3829614495 124 //初期設定完了
SES01 0:cf3829614495 125
SES01 1:e45830806a1c 126 LED = 1;
SES01 1:e45830806a1c 127 High_B = 1;
SES01 0:cf3829614495 128 driverEnable = 1;
SES01 0:cf3829614495 129 while (1) {
SES01 0:cf3829614495 130 Low_A.write((ain.read() - 0.2) * 1.25 < 0 ? 0 : (ain.read() - 0.2) * 1.25);
SES01 0:cf3829614495 131 //pc.printf("%f\n", (ain.read() - 0.2) * 1.25 < 0 ? 0 : (ain.read() - 0.2) * 1.25);
SES01 0:cf3829614495 132 comPort.printf("%d", 0b01010101);
SES01 0:cf3829614495 133 }
SES01 0:cf3829614495 134 }