premiere ebauche

Dependencies:   mbed PinDetect

Committer:
shovelcat
Date:
Thu Oct 25 17:43:21 2018 +0000
Revision:
5:aef1fc6c0df1
Parent:
4:a8c9f6a13633
Child:
6:1a4ed88c9a4b
fonctionnel avec ~50% d'overshoot

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shovelcat 0:011f69c1276c 1 #include "mbed.h"
shovelcat 2:06f128641b62 2 #include "speedlimiter.hpp"
shovelcat 0:011f69c1276c 3
shovelcat 3:4da392d2bae8 4 /*
shovelcat 0:011f69c1276c 5 ##############################
shovelcat 4:a8c9f6a13633 6 CONSTANTS
shovelcat 4:a8c9f6a13633 7 ##############################
shovelcat 4:a8c9f6a13633 8 */
shovelcat 4:a8c9f6a13633 9
shovelcat 4:a8c9f6a13633 10 const PinName pedalInHi = PA_3; // A0 (jaune)
shovelcat 4:a8c9f6a13633 11 const PinName pedalInLo = PC_0; // A1 (vert)
shovelcat 4:a8c9f6a13633 12 const PinName pedalOutHi = PA_4; // D9 (left) (jaune)
shovelcat 4:a8c9f6a13633 13 const PinName pedalOutLo = PA_5; // D13 (right) (vert)
shovelcat 4:a8c9f6a13633 14
shovelcat 4:a8c9f6a13633 15 const int CAN_FREQUENCY = 500000;
shovelcat 4:a8c9f6a13633 16
shovelcat 4:a8c9f6a13633 17 Ticker ticker;
shovelcat 4:a8c9f6a13633 18
shovelcat 4:a8c9f6a13633 19 SpeedLimiter speedLimiter(pedalInHi, pedalInLo, pedalOutHi, pedalOutLo);
shovelcat 4:a8c9f6a13633 20
shovelcat 4:a8c9f6a13633 21 /*
shovelcat 4:a8c9f6a13633 22 ##############################
shovelcat 1:c6b4ccebd483 23 uC I/O
shovelcat 1:c6b4ccebd483 24 ##############################
shovelcat 3:4da392d2bae8 25 */
shovelcat 0:011f69c1276c 26
shovelcat 3:4da392d2bae8 27 //InterruptIn b1(PC_13);
shovelcat 3:4da392d2bae8 28 DigitalOut led1(LED1);
shovelcat 3:4da392d2bae8 29 DigitalOut led2(LED2);
shovelcat 3:4da392d2bae8 30 DigitalOut led3(LED3);
shovelcat 5:aef1fc6c0df1 31 DigitalOut pout(PA_7); // D11
shovelcat 4:a8c9f6a13633 32 CAN can(PD_0 /*can1 rd*/, PD_1 /*can1 td*/, CAN_FREQUENCY);
shovelcat 4:a8c9f6a13633 33 Serial pc(USBTX, USBRX);
shovelcat 0:011f69c1276c 34
shovelcat 3:4da392d2bae8 35 /*
shovelcat 4:a8c9f6a13633 36 ##############################
shovelcat 0:011f69c1276c 37
shovelcat 4:a8c9f6a13633 38 ##############################
shovelcat 3:4da392d2bae8 39 */
shovelcat 0:011f69c1276c 40
shovelcat 4:a8c9f6a13633 41 void canCallback()
shovelcat 4:a8c9f6a13633 42 {
shovelcat 4:a8c9f6a13633 43 CANMessage msg;
shovelcat 4:a8c9f6a13633 44 if(can.read(msg)) {
shovelcat 4:a8c9f6a13633 45 if (msg.id == 0x185) {
shovelcat 4:a8c9f6a13633 46 float pedal = (msg.data[1] << 8 | msg.data[0]) * 0.00390625;
shovelcat 4:a8c9f6a13633 47 float speed = (msg.data[6] << 8 | msg.data[7]) * 0.1;
shovelcat 4:a8c9f6a13633 48
shovelcat 4:a8c9f6a13633 49 if (speed < 45) {
shovelcat 4:a8c9f6a13633 50 // pc.printf("Pedal: %.3f\tSpeed: %.3f\n\r", pedal, speed);
shovelcat 4:a8c9f6a13633 51 speedLimiter.setMeasuredSpeed(speed);
shovelcat 4:a8c9f6a13633 52 }
shovelcat 5:aef1fc6c0df1 53 } else if(msg.id == 0x181) {
shovelcat 5:aef1fc6c0df1 54
shovelcat 4:a8c9f6a13633 55 }
shovelcat 4:a8c9f6a13633 56 }
shovelcat 4:a8c9f6a13633 57 }
shovelcat 4:a8c9f6a13633 58
shovelcat 3:4da392d2bae8 59 int main()
shovelcat 3:4da392d2bae8 60 {
shovelcat 4:a8c9f6a13633 61 speedLimiter.setReferenceSpeed(10.0);
shovelcat 4:a8c9f6a13633 62 can.attach(&canCallback, CAN::RxIrq);
shovelcat 4:a8c9f6a13633 63 ticker.attach(callback(&speedLimiter, &SpeedLimiter::ipControllerTransferFunction), SpeedLimiter::TRANSFER_FUNCTION_PERIOD);
shovelcat 5:aef1fc6c0df1 64
shovelcat 5:aef1fc6c0df1 65 pc.printf("Start!");
shovelcat 0:011f69c1276c 66
shovelcat 3:4da392d2bae8 67 while(1) {
shovelcat 3:4da392d2bae8 68 led1 = true;
shovelcat 3:4da392d2bae8 69 wait(1.0);
shovelcat 3:4da392d2bae8 70 led1 = false;
shovelcat 3:4da392d2bae8 71 led2 = true;
shovelcat 3:4da392d2bae8 72 wait(1.0);
shovelcat 3:4da392d2bae8 73 led2 = false;
shovelcat 3:4da392d2bae8 74 led3 = true;
shovelcat 3:4da392d2bae8 75 wait(1.0);
shovelcat 3:4da392d2bae8 76 led3 = false;
shovelcat 4:a8c9f6a13633 77 wait(0.01);
shovelcat 5:aef1fc6c0df1 78
shovelcat 5:aef1fc6c0df1 79 // pout = !pout;
shovelcat 5:aef1fc6c0df1 80 // speedLimiter.ecoEnabledAlgorithm();
shovelcat 1:c6b4ccebd483 81 }
shovelcat 5:aef1fc6c0df1 82 }