新型モータードライバのメインプログラム
Dependencies: mbed
main.cpp@1:e45830806a1c, 2016-05-14 (annotated)
- Committer:
- SES01
- Date:
- Sat May 14 05:58:39 2016 +0000
- Revision:
- 1:e45830806a1c
- Parent:
- 0:cf3829614495
- Child:
- 2:c77482250d0b
??????
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:e45830806a1c | 12 | #define SINGLE_ORDER_MODE 2 |
SES01 | 1:e45830806a1c | 13 | #define SEND_POWER_MODE 3 |
SES01 | 1:e45830806a1c | 14 | |
SES01 | 0:cf3829614495 | 15 | #define F_MD 18000 //PWMキャリア周波数設定 |
SES01 | 0:cf3829614495 | 16 | |
SES01 | 0:cf3829614495 | 17 | /* |
SES01 | 0:cf3829614495 | 18 | * High_A High_B |
SES01 | 0:cf3829614495 | 19 | * ┃ ┃ |
SES01 | 0:cf3829614495 | 20 | * ┃ ┃ |
SES01 | 0:cf3829614495 | 21 | * ┣━━━┫ |
SES01 | 0:cf3829614495 | 22 | * ┃ ┃ |
SES01 | 0:cf3829614495 | 23 | * ┃ ┃ |
SES01 | 0:cf3829614495 | 24 | * Low_A Low_B |
SES01 | 0:cf3829614495 | 25 | */ |
SES01 | 0:cf3829614495 | 26 | |
SES01 | 0:cf3829614495 | 27 | Serial comPort(dp16, dp15); |
SES01 | 0:cf3829614495 | 28 | DigitalOut driverEnable(dp28); |
SES01 | 1:e45830806a1c | 29 | DigitalOut LED(dp24); |
SES01 | 1:e45830806a1c | 30 | //dp5 = ハイサイドPWM1, dp27 = ハイサイドPWM2 (オープンドレイン) |
SES01 | 0:cf3829614495 | 31 | //dp1 = ローサイドPWM1, dp2 = ローサイドPWM2 |
SES01 | 0:cf3829614495 | 32 | PwmOut Low_A(dp1); |
SES01 | 0:cf3829614495 | 33 | PwmOut Low_B(dp2); |
SES01 | 0:cf3829614495 | 34 | DigitalOut High_A(dp5); |
SES01 | 0:cf3829614495 | 35 | DigitalOut High_B(dp27); |
SES01 | 0:cf3829614495 | 36 | |
SES01 | 0:cf3829614495 | 37 | AnalogIn ain(dp4); |
SES01 | 1:e45830806a1c | 38 | |
SES01 | 1:e45830806a1c | 39 | //グローバル変数 |
SES01 | 1:e45830806a1c | 40 | uint8_t lastRXdata = 0; |
SES01 | 1:e45830806a1c | 41 | uint8_t address = 0; |
SES01 | 1:e45830806a1c | 42 | uint8_t mode = 0; |
SES01 | 1:e45830806a1c | 43 | uint8_t nowOrderAddress = 0; |
SES01 | 1:e45830806a1c | 44 | uint8_t dataNumber = 0; |
SES01 | 1:e45830806a1c | 45 | uint8_t sendPowerFlag = 0; |
SES01 | 1:e45830806a1c | 46 | |
SES01 | 1:e45830806a1c | 47 | void orderFollowManager(uint8_t reciveData, uint8_t dataNumber){ |
SES01 | 1:e45830806a1c | 48 | |
SES01 | 1:e45830806a1c | 49 | } |
SES01 | 1:e45830806a1c | 50 | |
SES01 | 1:e45830806a1c | 51 | void encoderFollowManager(uint8_t reciveData, uint8_t dataNumber){ |
SES01 | 1:e45830806a1c | 52 | |
SES01 | 1:e45830806a1c | 53 | } |
SES01 | 1:e45830806a1c | 54 | |
SES01 | 1:e45830806a1c | 55 | void S_orderFollowManager(uint8_t reciveData, uint8_t dataNumber){ |
SES01 | 1:e45830806a1c | 56 | |
SES01 | 1:e45830806a1c | 57 | } |
SES01 | 1:e45830806a1c | 58 | |
SES01 | 1:e45830806a1c | 59 | void sendPowerManager(uint8_t reciveData, uint8_t dataNumber){ |
SES01 | 1:e45830806a1c | 60 | |
SES01 | 1:e45830806a1c | 61 | } |
SES01 | 0:cf3829614495 | 62 | |
SES01 | 0:cf3829614495 | 63 | void com_rx() { |
SES01 | 1:e45830806a1c | 64 | static uint8_t reciveData; |
SES01 | 1:e45830806a1c | 65 | reciveData = comPort.getc(); |
SES01 | 1:e45830806a1c | 66 | |
SES01 | 1:e45830806a1c | 67 | if (reciveData | LEAD_PACKET_MASK) { |
SES01 | 1:e45830806a1c | 68 | nowOrderAddress = reciveData | PACKET_ADDRESS_MASK; |
SES01 | 1:e45830806a1c | 69 | dataNumber = 0; |
SES01 | 1:e45830806a1c | 70 | } |
SES01 | 1:e45830806a1c | 71 | |
SES01 | 1:e45830806a1c | 72 | if (nowOrderAddress == address) { |
SES01 | 1:e45830806a1c | 73 | dataNumber++; |
SES01 | 1:e45830806a1c | 74 | lastRXdata = reciveData | PACKET_DATA_MASK; |
SES01 | 0:cf3829614495 | 75 | |
SES01 | 1:e45830806a1c | 76 | if (dataNumber == 1) { |
SES01 | 1:e45830806a1c | 77 | mode = (reciveData | PACKET_MODE_MASK) >> 5; |
SES01 | 1:e45830806a1c | 78 | } else { |
SES01 | 1:e45830806a1c | 79 | switch (mode) { |
SES01 | 1:e45830806a1c | 80 | case ORDER_FOLLOWING_MODE: |
SES01 | 1:e45830806a1c | 81 | orderFollowManager(reciveData, dataNumber); |
SES01 | 1:e45830806a1c | 82 | break; |
SES01 | 1:e45830806a1c | 83 | case ENCODER_FOLLOWING_MODE: |
SES01 | 1:e45830806a1c | 84 | encoderFollowManager(reciveData, dataNumber); |
SES01 | 1:e45830806a1c | 85 | break; |
SES01 | 1:e45830806a1c | 86 | case SINGLE_ORDER_MODE: |
SES01 | 1:e45830806a1c | 87 | S_orderFollowManager(reciveData, dataNumber); |
SES01 | 1:e45830806a1c | 88 | break; |
SES01 | 1:e45830806a1c | 89 | case SEND_POWER_MODE: |
SES01 | 1:e45830806a1c | 90 | sendPowerManager(reciveData, dataNumber); |
SES01 | 1:e45830806a1c | 91 | break; |
SES01 | 1:e45830806a1c | 92 | } |
SES01 | 1:e45830806a1c | 93 | } |
SES01 | 1:e45830806a1c | 94 | } |
SES01 | 0:cf3829614495 | 95 | } |
SES01 | 0:cf3829614495 | 96 | |
SES01 | 0:cf3829614495 | 97 | int main() { |
SES01 | 1:e45830806a1c | 98 | comPort.baud(9600); |
SES01 | 1:e45830806a1c | 99 | comPort.format(8, Serial::Odd, 1); |
SES01 | 0:cf3829614495 | 100 | comPort.attach(&com_rx, Serial::RxIrq); |
SES01 | 0:cf3829614495 | 101 | |
SES01 | 0:cf3829614495 | 102 | int FP_MD = 1000000 / F_MD; //PWM一周期の周期(マイクロ秒) |
SES01 | 0:cf3829614495 | 103 | |
SES01 | 0:cf3829614495 | 104 | High_A = 0; |
SES01 | 1:e45830806a1c | 105 | High_B = 0; |
SES01 | 0:cf3829614495 | 106 | |
SES01 | 0:cf3829614495 | 107 | Low_A.period_us(FP_MD); |
SES01 | 0:cf3829614495 | 108 | Low_A.write(0); |
SES01 | 0:cf3829614495 | 109 | Low_B.period_us(FP_MD); |
SES01 | 0:cf3829614495 | 110 | Low_B.write(0); |
SES01 | 0:cf3829614495 | 111 | //初期設定完了 |
SES01 | 0:cf3829614495 | 112 | |
SES01 | 1:e45830806a1c | 113 | LED = 1; |
SES01 | 1:e45830806a1c | 114 | High_B = 1; |
SES01 | 0:cf3829614495 | 115 | driverEnable = 1; |
SES01 | 0:cf3829614495 | 116 | while (1) { |
SES01 | 0:cf3829614495 | 117 | Low_A.write((ain.read() - 0.2) * 1.25 < 0 ? 0 : (ain.read() - 0.2) * 1.25); |
SES01 | 0:cf3829614495 | 118 | //pc.printf("%f\n", (ain.read() - 0.2) * 1.25 < 0 ? 0 : (ain.read() - 0.2) * 1.25); |
SES01 | 0:cf3829614495 | 119 | comPort.printf("%d", 0b01010101); |
SES01 | 0:cf3829614495 | 120 | } |
SES01 | 0:cf3829614495 | 121 | } |