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-06-08
Revision:
2:8e7b51353f32
Parent:
1:450090bdb6f4
Child:
3:43cb067ecd00

File content as of revision 2:8e7b51353f32:

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

//#define SPEED_CONTROL_ENABLE    //  Includes engine revs servo control loop

const   uint32_t    TICKOVER_RPM = 2500;
const   uint32_t    PWM_OFF_RPM_LIMIT = (TICKOVER_RPM * 9) / 10;
const   uint32_t    MAX_RPM_LIMIT = 7500;
const   double      MAX_FIELD_PWM = 0.47;
const   double      SERVO_MAX = 0.5;
const   double      DRIVER_NEUTRAL = 0.18;
const   uint32_t    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:
    uint64_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 = 0L;
    }   ;
}   ;

class   eeprom_settings {
    char        settings    [36];
  public:
    eeprom_settings (); //  Constructor
    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 = 12;    //  Up from 10 May 2020
struct  parameters  {
    int32_t times[50];
    int32_t position_in_list, last_time, numof_dbls;
    double  dbl[MAX_PARAMS];
}   ;

const   int PWM_PERIOD_US = 1800    ;   //  Was 2400, May want to reduce this, note would require change of resistor value on board