Electric Locomotive control system. Touch screen driver control, includes regenerative braking, drives 4 brushless motors, displays speed MPH, system volts and power

Dependencies:   BSP_DISCO_F746NG FastPWM LCD_DISCO_F746NG SD_DISCO_F746NG TS_DISCO_F746NG mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers throttle.cpp Source File

throttle.cpp

00001 /*
00002 This file about using model control servo to drive the throttle of Honda GX120 engine
00003 
00004 Using a model control servo to set the throttle of Honda GX120 petrol engine
00005 First thoughts  12 Nov 2017.
00006 
00007 Servo driven by positive pulse of duration 1000 to 2000 micro sec. Repetition rate 50Hz
00008 Will first try repeating in 32ms loop giving repetirion rate approx 30 Hz.
00009 Pos pulse generated by setting to 1 in 32ms handler and starting a 'timeout'
00010 
00011     ... as part of 32ms code
00012     if  (torque_demand < 0.01)  {
00013         throttle    (TICKOVER);
00014     }
00015     else    {   //  got positive torque demand
00016         throttle    (MID_REVS);
00017     }
00018     throttle_servo_pulse_out = 1;
00019     throttle_servo_pulse_width.attach_us    (&servo_pulse_lo, global_throttle_us);
00020 */
00021 #include "mbed.h"
00022 #include "Electric_Loco.h"
00023 
00024 static const   int
00025     SERVO_RANGE     = 1000, //  micro sec difference between max and min
00026     SERVO_OFFSET    = 1000, //  min servo pulse width micro sec
00027     TICKOVER        = SERVO_OFFSET + 0,
00028     MID_REVS        = SERVO_OFFSET + 500,
00029     RANGE_MULTIPLIER    = SERVO_RANGE + SERVO_OFFSET - MID_REVS;
00030 
00031 DigitalOut  throttle_servo_pulse_out    (D8);   //  now defined in throttle.cpp
00032 
00033 Timeout throttle_servo_pulse_width;
00034 
00035 void    throttle_servo_pulse_lo  ()  {   //  Interrupt handler called at end of 'Timeout'
00036     throttle_servo_pulse_out = 0;   //  end servo positive pulse
00037 }
00038 
00039 int throttle    (double torque_demand, double speed_mph)    {   //  called from main every 31ms
00040     int duration_us;
00041     if  (throttle_servo_pulse_out != 0)
00042         return  -1; //  pulse positive already
00043     if  (torque_demand < 0.01)  {
00044         duration_us = TICKOVER;
00045     }
00046     else    {   //  got positive torque demand
00047         double  tmpd = torque_demand * RANGE_MULTIPLIER;
00048         duration_us = MID_REVS + (int) tmpd;
00049     }
00050     throttle_servo_pulse_out = 1;   //  start servo positive pulse
00051     throttle_servo_pulse_width.attach_us    (&throttle_servo_pulse_lo, duration_us);
00052     return  0;
00053 }
00054 //  endof New Nov 2017    Controlling throttle of Honda engine
00055 
00056