Code for 'Smart Regulator' featured in 'Model Engineer', November 2020 on. Contains all work to August 2020 including all code described. Top level algorithm development is quite spares, leaving some work for you! Any questions - jon@jons-workshop.com

Dependencies:   mbed BufferedSerial Servo2 PCT2075 I2CEeprom FastPWM

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers rpm.h Source File

rpm.h

00001 #include "mbed.h"
00002 #include "Servo.h"
00003 extern  Timer   microsecs;      //  64 bit counter, rolls over in half million years
00004 
00005 const   uint32_t    TICKOVER_RPM    = 1500; //2500;
00006 const   uint32_t    MIN_RPM         = 1600; //3600;
00007 const   uint32_t    MAX_RPM         = 3600; //6500;
00008 const   uint32_t    MAX_RPM_LIMIT   = 4000; //7500;     //  Used in building lookup table
00009 //const   double      RPM_ERR_DENOM   = (MAX_RPM_LIMIT * 20.0);   //  Larger -> more stable, Smaller -> faster response
00010 const   double      RPM_ERR_DENOM   = (MAX_RPM_LIMIT * 12.0);   //  Larger -> more stable, Smaller -> faster response
00011 //const   uint32_t    RPM_DEADBAND    = (MAX_RPM_LIMIT / 125);     //  Does not attempt to correct speed errors below this
00012 const   uint32_t    RPM_DEADBAND    = (MAX_RPM_LIMIT / 180);     //  Does not attempt to correct speed errors below this
00013 const   uint32_t    DEBOUNCE_US     = (45000000 / MAX_RPM);
00014 
00015 const   double      RPM_FILTER_FACTOR   = 0.25;
00016 const   double      MIN_WORKING_THROTTLE   = 0.23;
00017 const   double      MAX_WORKING_THROTTLE   = 0.99;
00018 /*  Cleans magneto pulses, calculates and attempts to maintain RPM  */
00019 class   Engine_Manager    {
00020     uint32_t    latest_RPM_reading, core_call_count;
00021     uint64_t    magt0, magt1, time_since_last_spark, new_time, last_update_time;
00022     double      servo_position, filtered_measured_RPM, requested_RPM;
00023     InterruptIn MagnetoSignal;
00024     DigitalOut  CleanedMagneto;
00025     Servo       Speed_ControlServo;
00026 //    const uint32_t  debounce;
00027     void        RPM_update  ();
00028     void        MagRise ();
00029     void        MagFall ();
00030     void        magneto_timeoutC    ();
00031     bool        magneto_stretch;
00032     bool        running_flag;
00033     bool        rpm_in_run_range;
00034     Timeout     magneto_timoC;
00035 //    Timer       microseconds;
00036   public:
00037     Engine_Manager    (PinName magneto_signal, PinName cleaned_output, PinName for_servo)  ;   //  
00038     void        Set_Speed_Lever    (double);
00039     bool        running ();
00040     void        manager_core();
00041     double      get_servo_position  ();
00042     uint32_t    RPM_latest  ();
00043     uint32_t    RPM_filtered();
00044     uint32_t    get_RPM_requested ()  ;     //  Returns latest requested RPM
00045     uint32_t    set_RPM_literal (uint32_t);     //  Returns latest measured RPM
00046     uint32_t    set_RPM_percent (uint32_t);     //  Returns latest measured RPM
00047     uint32_t    RPM_percent_to_actual (uint32_t);     //  Returns RPM actual calculated from percent
00048 }   ;
00049