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

Alternator.h

Committer:
JonFreeman
Date:
2020-04-25
Revision:
1:450090bdb6f4
Parent:
0:77803b3ee157
Child:
2:8e7b51353f32

File content as of revision 1:450090bdb6f4:

#include    "Servo.h"
#include    "BufferedSerial.h"

#define SPEED_CONTROL_ENABLE    //  Includes engine revs servo control loop

const   int TICKOVER_RPM = 2500;
const   int MAX_RPM_LIMIT = 7500;
const   double SERVO_MAX = 0.5;
const   int eeprom_page = 17;   //  Determines where in eeprom 'settings' reside

const   int lut_seg_size    =   60; //  steps per thousand RPM
const   int lut_size = lut_seg_size * 8;    //  8 segments - 0-1, 1-2, 2-3, 3-4 etc 000 rpm

class   VEXT_Data   {
    public:
    uint32_t    t_on, t_off, measured_pw_us, measured_period, rise_count, fall_count;
    double      duty_cycle  ()  {
        return  (double) measured_pw_us / (double) measured_period;
    }   ;
    VEXT_Data   ()  {   //  constructor
        t_on = t_off = measured_pw_us =  measured_period = rise_count = fall_count = 0;
    }   ;
}   ;

class   eeprom_settings {
    char        settings    [36];
    double      max_pwm_lut [lut_size + 4];
//    bool        rd_24LC64  (int start_addr, char * dest, int length)    ;
//    bool        wr_24LC64  (int start_addr, char * dest, int length)    ;
//    bool        set_24LC64_internal_address (int    start_addr) ;
//    bool        ack_poll    ()  ;
    void        build_lut   ()  ;
  public:
    eeprom_settings (); //  Constructor
    double      get_pwm (int)   ;
    char        rd  (uint32_t)  ;           //  Read one setup char value from private buffer 'settings'
    bool        rd  (char *, uint32_t)  ;           //  Read one setup char value from private buffer 'settings'
    bool        wr  (char, uint32_t)  ;     //  Write one setup char value to private buffer 'settings'
    bool        save    ()  ;               //  Write 'settings' buffer to EEPROM
    bool        load    ()  ;               //  Get 'settings' from EEPROM
    bool        set_defaults    ();         //  Put default settings into EEPROM and local buffer
//    uint32_t    errs    ()  ;               //  Return errors
}   ;

enum    {RPM0, RPM1, RPM2, RPM3, RPM4, RPM5, RPM6, RPM7, RPM8, 
            PWM_SCALE, FUT1, FUT2, FUT3, FUT4, FUT5}  ;  //  

struct  optpar  {
    int min, max, def;  //  min, max, default
    const char * t;     //  description
}   ;

const   int MAX_PARAMS = 10;
struct  parameters  {
    int32_t times[50];
    int32_t position_in_list, last_time, numof_dbls;
    double  dbl[MAX_PARAMS];
}   ;

const   int PWM_PERIOD_US = 2400    ;