2014 sift / Mbed 2 deprecated TVDctrller2017_brdRev1_PandA

Dependencies:   mbed

Fork of TVDctrller2017_brdRev1_ver6 by 2014 sift

main.cpp

Committer:
sift
Date:
2017-07-01
Revision:
27:37a8b4f6d28d
Parent:
26:331e77bb479b
Child:
28:47e9531a3a9d

File content as of revision 27:37a8b4f6d28d:

#include "mbed.h"
#include "TVDCTRL.h"
#include "Steering.h"
#include "MCP4922.h"
#include "Global.h"

Serial pc(USBTX, USBRX); // tx, rx

////////////////////////////////////////
//IO宣言
SPI spi(p5,p6,p7);
MCP4922 mcp(p5,p7,p8);      // MOSI, SCLK, CS
DigitalOut ioExpCs(p9);
DigitalOut indicatorLed(p10);
DigitalOut shutDown(p11);
DigitalOut brakeSignal(p12);
DigitalIn sdState(p13);
DigitalIn RTDSW(p14);
AnalogIn apsS(p15);         //"S"econdary
AnalogIn apsP(p16);         //"P"rimary
AnalogIn brake(p17);
AnalogOut STR2AN(p18);
DigitalIn SLCTSW[3] = {p19, p20, p21};
InterruptIn rightMotorPulse(p22);
InterruptIn leftMotorPulse(p23);
InterruptIn rightTirePulse1(p24);
InterruptIn rightTirePulse2(p25);
InterruptIn leftTirePulse1(p26);
InterruptIn leftTirePulse2(p27);
DigitalOut WDT(p28);
CAN can(p30, p29);
DigitalOut LED[] = {LED1, LED2, LED3, LED4};

Timer timer;

#define indicateSystem(x)       (indicatorLed.write(x))
#define shutdownSystem(void)    (shutDown.write(0))
#define bootSystem(void)        (shutDown.write(1))
#define isPressedRTD(void)      (!RTDSW.read())
#define isShutdownSystem(void)  (SDState.read())

//++++++++++++++++++++++++++
//CANデータのバイト数
#define CANDATANUM_REQTRQ 2
#define CANDATANUM_BRAKE 1
#define CANDATANUM_MOTORTRQ_R 2
#define CANDATANUM_MOTORTRQ_L 2
#define CANDATANUM_MOTORTRQ_L 1
//++++++++++++++++++++++++++

void init(void)
{
    indicatorLed = 0;
    shutDown = 0;
    brakeSignal = 0;
    LED[0] = LED[1] = LED[2] = LED[3] = 0;

    sdState.mode(PullNone);
    RTDSW.mode(PullNone);
    SLCTSW[0].mode(PullNone);
    SLCTSW[1].mode(PullNone);
    SLCTSW[2].mode(PullNone);

    rightMotorPulse.mode(PullNone);
    leftMotorPulse.mode(PullNone);
    rightTirePulse1.mode(PullNone);
    rightTirePulse2.mode(PullNone);
    leftTirePulse1.mode(PullNone);
    leftTirePulse2.mode(PullNone);

    can.frequency(500000);      //500kHz

    indicateSystem(1);
    bootSystem();
}

int main(void)
{
    int f_whatchdog=0;
    char canSendData[1];

    wait(1);

    pc.baud(115200);
    printf("\033[2J");          //teratermの画面クリア
    printf("\033[1;1H");        //teratermの画面クリア
    printf("\r\nVersion:TVDctrller2017_brdRev1...start!!!!!\r\n");

    init();       //IOポート初期化

    initTVD();

    initSteering();

    timer.start();

    float time;

    struct errCounter_t eCounter= {0,0,0,0,0,0,0,0};

    wait(1);

    while(1) {
        //f_whatchdog = ~f_whatchdog;
        //WDT.write(f_whatchdog);
        //getCurrentErrCount(&eCounter);

        timer.reset();

        driveTVD(0, isPressedRTD());

        //printf("%f\n\r", 45.0/0xFFFF * calcRequestTorque());

        canSendData[0] = (char)(255.0f * (calcRequestTorque()-MAX_OUTPUT_TORQUE_REGENERATIVE)/(MAX_OUTPUT_TORQUE_POWER-MAX_OUTPUT_TORQUE_REGENERATIVE));
        printf("%2.2f\t%3.2f\t%1.2f\tsend:%d\r\n", 45.0f / 0xffff * calcRequestTorque(), getVelocity(), getSteerAngle(), canSendData[0]);

        can.write(CANMessage(0x0f, canSendData, 1));

        //printf("%1.6f\r\n", time);
        //printf("%d\r\n", eCounter.brakeOverRide);
        time = timer.read();
        while(timer.read_ms() < CONTROL_CYCLE_MS);      //制御周期管理 関数内処理時間より短い時間の制御周期の設定は禁止

        //printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t\r\n", eCounter.apsUnderVolt, eCounter.apsExceedVolt, eCounter.apsErrorTolerance, eCounter.apsStick, eCounter.brakeUnderVolt, eCounter.brakeExceedVolt, eCounter.brakeFuzzyVolt, eCounter.brakeOverRide);
        //printf("apsP:%1.2f, apsS:%1.2f, brake:%1.2f\r", 3.3f/65535 * getRawSensor(APS_PRIMARY), 3.3f/65535 * getRawSensor(APS_SECONDARY), 3.3f/65535 * getRawSensor(BRAKE));
    }
}