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

Dependencies:   mbed

main.cpp

Committer:
SES01
Date:
2016-05-14
Revision:
2:c77482250d0b
Parent:
1:e45830806a1c
Child:
3:65eaf88bf278

File content as of revision 2:c77482250d0b:

#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 //単命令を受信した後は必ずsendback

//単命令動作の種類
#define S_ORDER_FREQUENCY           0
#define S_ORDER_SEND_POWER          1
#define S_ORDER_LINEAR_AUTOCHANGE   2
#define S_ORDER_CERTAIN             3
#define S_ORDER_SEND_ENCODER        4

#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){
    switch (dataNumber) {
        case 2:
            break;
        case 3:
            break;
        case 4:
            break;
    }
}

void encoderFollowManager(uint8_t reciveData, uint8_t dataNumber){
    
}

void S_orderFollowManager(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;
            }
        }    
    }
}

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);
    }
}