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
Diff: Alternator.h
- Revision:
- 1:450090bdb6f4
- Parent:
- 0:77803b3ee157
- Child:
- 2:8e7b51353f32
--- a/Alternator.h Fri Jun 28 19:32:51 2019 +0000 +++ b/Alternator.h Sat Apr 25 15:35:58 2020 +0000 @@ -1,13 +1,30 @@ #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]; - int max_pwm_lut [lut_size + 4]; + 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) ; @@ -15,8 +32,9 @@ void build_lut () ; public: eeprom_settings (); // Constructor - int get_pwm (int) ; + 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 @@ -32,5 +50,12 @@ const char * t; // description } ; -const int PWM_PERIOD_US = 3200 ; +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 ; +