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:
- 3:43cb067ecd00
- Parent:
- 2:8e7b51353f32
- Child:
- 4:28cc0cf01570
--- a/Alternator.h Mon Jun 08 13:46:52 2020 +0000 +++ b/Alternator.h Mon Jul 27 08:44:59 2020 +0000 @@ -1,57 +1,64 @@ -#include "Servo.h" -#include "BufferedSerial.h" -//#define SPEED_CONTROL_ENABLE // Includes engine revs servo control loop +//#define GPS_ // Not the crap one I tried! -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 double ALTERNATOR_DESIGN_VOLTAGE = 14.0; // Used to scale down max field pwm when available voltage higher than this +const double DRIVER_NEUTRAL = 0.18; // Proportion of driver's pot travel deemed to be zero power request +const uint32_t eeprom_page = 17; // Determines where in eeprom 'settings' reside +const int eeprom_page_size = 32; +const int PWM_PERIOD_US = 1800 ; // Was 2400, May want to reduce this, note would require change of resistor value on board -//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 +enum {TABL0, TABL1, TABL2, TABL3, TABL4, TABL5, TABL6, TABL7, TABL8, TABL9, TABL10, + TABL11, TABL12, TABL13, TABL14, TABL15, TABL16, TABL17, TABL18, TABL19, TABL20, + WARM_UP_DELAY, WARMUP_SERVO_POS, OP_MODE, SPEED_CTRL_P, SERVO_DIR, FUT27, FUT28, FUT29, FUT30, FUT31 } ; -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; - } ; +struct sldandt { + const uint32_t min, max, de_fault; // min, max, default + const char * txt; // description + char val; } ; -class eeprom_settings { - char settings [36]; + +class ee_settings_2020 { + char new_settings[eeprom_page_size + 2]; 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 + ee_settings_2020 () ; // Constructor + int load () ; + int save () ; + char rd (uint32_t i) ; + bool wr (char c, uint32_t i) ; // Write one setup char value to private buffer 'settings' + sldandt * inform (uint32_t which) ; } ; 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; +struct parameters { // Used in Command Line Interpreter, stores user input values + int32_t position_in_list, 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 +/* +- Position the Cursor: + \033[<L>;<C>H + Or + \033[<L>;<C>f + puts the cursor at line L and column C. +- Move the cursor up N lines: + \033[<N>A +- Move the cursor down N lines: + \033[<N>B +- Move the cursor forward N columns: + \033[<N>C +- Move the cursor backward N columns: + \033[<N>D + +- Clear the screen, move to (0,0): + \033[2J +- Erase to end of line: + \033[K + +- Save cursor position: + \033[s might not work +- Restore cursor position: + \033[u might not work +*/ +