premiere ebauche

Dependencies:   mbed PinDetect

main.cpp

Committer:
LipSer
Date:
2018-12-17
Revision:
15:e9560502e463
Parent:
13:47806f4dbfcd

File content as of revision 15:e9560502e463:

#include "mbed.h"
#include "speedlimiter.hpp"
#include "recommendedspeed.hpp"
#include "PinDetect.h"

/*
##############################
CONSTANTS
##############################
*/

const PinName pedalInHi = PA_3; // A0 (jaune)
const PinName pedalInLo = PC_0; // A1 (vert)
const PinName pedalOutHi = PA_4; // D9 (left) (jaune)
const PinName pedalOutLo = PA_5; // D13 (right) (vert)
//const PinName testAdcIn = PC_3;

const int CAN_FREQUENCY = 500000;

Ticker ticker;
//InterruptIn button(USER_BUTTON);


Parcours parcours;
SpeedLimiter speedLimiter(pedalInHi, pedalInLo, pedalOutHi, pedalOutLo);

/*
##############################
uC I/O
##############################
*/

//InterruptIn     b1(PC_13);
//InterruptIn     pushButton(PB_3);
PinDetect       pushButton(PB_4);
DigitalOut      led1(LED1);
DigitalOut      led2(LED2);
DigitalOut      led3(LED3);
DigitalOut      pout(PA_7); // D11
CAN             can(PD_0 /*can1 rd*/, PD_1 /*can1 td*/, CAN_FREQUENCY);
Serial          pc(USBTX, USBRX);

/*
##############################

##############################
*/

void canCallback()
{
    CANMessage msg;
    if(can.read(msg)) {
        if (msg.len != 8) {
            ; // drop message
        } else if (msg.id == 0x185) {
            // shift first byte to HI, then fill LO with second byte
            float speed = (msg.data[6] << 8 | msg.data[7]) * 0.1;
//            float power = (msg.data[5]);
            
            if (speed < 45) {
                speedLimiter.setMeasuredSpeed(speed);
            }
        } else if(msg.id == 0x181) {
//            float motorCurrent = (msg.data[2] << 8 | msg.data[3]) * 1.0;
        } else if(msg.id == 0x322) {
//            float energySoc = (msg.data[2] << 8 | msg.data[3]);
        }
    }
}

int main()
{
    speedLimiter.setParcours(&parcours);
    speedLimiter.setReferenceSpeed(0.0);
    can.attach(&canCallback, CAN::RxIrq);
    ticker.attach(callback(&speedLimiter, &SpeedLimiter::controllerCallbackFunction), SpeedLimiter::TRANSFER_FUNCTION_PERIOD);
    pushButton.mode(PullUp);
    pushButton.attach_deasserted(&parcours, &Parcours::incrementIndex);
    pushButton.setSampleFrequency();
//    pushButton.rise(callback(&parcours, &Parcours::incrementIndex));

    pc.printf("Start!");

    while(1) {
        led1 = true;
        wait(1.0);
        led1 = false;
        led2 = true;
        wait(1.0);
        led2 = false;
        led3 = true;
        wait(1.0);
        led3 = false;
        wait(0.01);

//        pout = !pout;
//        speedLimiter.ecoEnabledAlgorithm();
    }
}