新型モータードライバのメインプログラム
Dependencies: mbed
main.cpp
- Committer:
- SES01
- Date:
- 2016-05-14
- Revision:
- 1:e45830806a1c
- Parent:
- 0:cf3829614495
- Child:
- 2:c77482250d0b
File content as of revision 1:e45830806a1c:
#include "mbed.h" //重要受信データのマスク #define LEAD_PACKET_MASK 0b10000000 #define PACKET_ADDRESS_MASK 0b00011111 #define PACKET_MODE_MASK 0b01100000 #define PACKET_DATA_MASK 0b00111111 //モータードライバの動作モード #define ORDER_FOLLOWING_MODE 0 #define ENCODER_FOLLOWING_MODE 1 #define SINGLE_ORDER_MODE 2 #define SEND_POWER_MODE 3 #define F_MD 18000 //PWMキャリア周波数設定 /* * High_A High_B * ┃ ┃ * ┃ ┃ * ┣━━━┫ * ┃ ┃ * ┃ ┃ * Low_A Low_B */ Serial comPort(dp16, dp15); DigitalOut driverEnable(dp28); DigitalOut LED(dp24); //dp5 = ハイサイドPWM1, dp27 = ハイサイドPWM2 (オープンドレイン) //dp1 = ローサイドPWM1, dp2 = ローサイドPWM2 PwmOut Low_A(dp1); PwmOut Low_B(dp2); DigitalOut High_A(dp5); DigitalOut High_B(dp27); AnalogIn ain(dp4); //グローバル変数 uint8_t lastRXdata = 0; uint8_t address = 0; uint8_t mode = 0; uint8_t nowOrderAddress = 0; uint8_t dataNumber = 0; uint8_t sendPowerFlag = 0; void orderFollowManager(uint8_t reciveData, uint8_t dataNumber){ } void encoderFollowManager(uint8_t reciveData, uint8_t dataNumber){ } void S_orderFollowManager(uint8_t reciveData, uint8_t dataNumber){ } void sendPowerManager(uint8_t reciveData, uint8_t dataNumber){ } void com_rx() { static uint8_t reciveData; reciveData = comPort.getc(); if (reciveData | LEAD_PACKET_MASK) { nowOrderAddress = reciveData | PACKET_ADDRESS_MASK; dataNumber = 0; } if (nowOrderAddress == address) { dataNumber++; lastRXdata = reciveData | PACKET_DATA_MASK; if (dataNumber == 1) { mode = (reciveData | PACKET_MODE_MASK) >> 5; } else { switch (mode) { case ORDER_FOLLOWING_MODE: orderFollowManager(reciveData, dataNumber); break; case ENCODER_FOLLOWING_MODE: encoderFollowManager(reciveData, dataNumber); break; case SINGLE_ORDER_MODE: S_orderFollowManager(reciveData, dataNumber); break; case SEND_POWER_MODE: sendPowerManager(reciveData, dataNumber); break; } } } } int main() { comPort.baud(9600); comPort.format(8, Serial::Odd, 1); comPort.attach(&com_rx, Serial::RxIrq); int FP_MD = 1000000 / F_MD; //PWM一周期の周期(マイクロ秒) High_A = 0; High_B = 0; Low_A.period_us(FP_MD); Low_A.write(0); Low_B.period_us(FP_MD); Low_B.write(0); //初期設定完了 LED = 1; High_B = 1; driverEnable = 1; while (1) { Low_A.write((ain.read() - 0.2) * 1.25 < 0 ? 0 : (ain.read() - 0.2) * 1.25); //pc.printf("%f\n", (ain.read() - 0.2) * 1.25 < 0 ? 0 : (ain.read() - 0.2) * 1.25); comPort.printf("%d", 0b01010101); } }