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

cli.cpp

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

File content as of revision 1:450090bdb6f4:

/*
Command Line Interpreter code module.
Purpose -
    Provides easy interface to pc terminal programme for use during programme development, debug etc.
    Also usable as comms subsystem in finished code for accepting commands, reporting data etc.
*/
#include "mbed.h"
#include "Alternator.h"
//#include "BufferedSerial.h"
#include <cctype>
using namespace std;

extern  eeprom_settings     user_settings     ;
//eeprom_settings     mode     ;

//extern  int ver, vef, measured_pw_us;
extern  void    set_throttle_limit    (struct parameters & a)    ;
extern  void    speed_control_factor_set    (struct parameters & a)    ;
extern  void    query_system    (struct parameters & a)    ;
extern  uint32_t    ReadEngineRPM  ()   ;
extern  double  Read_BatteryVolts   ()  ;
extern  void Read_Ammeter   (double *)  ;





//  WithOUT RTOS
//extern  BufferedSerial pc;

#ifdef  TARGET_NUCLEO_L432KC    //
extern  Serial pc;
#else
extern  BufferedSerial pc;
#endif
//extern  double  test_pot;    //  These used in knifeandfork code testing only

//extern  int numof_eeprom_options2    ;
//extern  struct  optpar const option_list2[]  ;
extern  struct optpar  option_list2[]    ;

/**void    mode_cmd (struct parameters & a)       //  With no params, reads eeprom contents. With params sets eeprom contents
*   mode_cmd called only from pc comms. No sense calling from Touch Screen Controller
*
*   Called without parameters - Lists to pc terminal current settings
*
*/
void    mode19_cmd (struct parameters & a)       //  With no params, reads eeprom contents. With params sets eeprom contents
{

    char            temps[36];
    int i;
    pc.printf   ("\r\nmode - Set system data in EEPROM - Jan 2019\r\nSyntax 'mode' with no parameters lists current state.\r\n");
    if  (a.numof_dbls)  {           //  If more than 0 parameters supplied
        for (i = 0; i < a.numof_dbls; i++)
            temps[i] = (char)a.dbl[i];          //  recast doubles to char
        while   (i < 33)
            temps[i++] = 0;
        i = (int)a.dbl[0];
        switch  (i) {
            case    0:    case    1:    case    2:    case    3:    case    4:
            case    5:    case    6:    case    7:    case    8:
                if  (temps[1] >= option_list2[i].min && temps[1] <= option_list2[i].max)
                    user_settings.wr(temps[1], RPM0 + i);
                break;
            case    37: //  set pwm scale factor
                if  (temps[1] >= option_list2[PWM_SCALE].min && temps[1] <= option_list2[PWM_SCALE].max)
                    user_settings.wr(temps[1], PWM_SCALE);
                break;
            case    83: //  set to defaults
                user_settings.set_defaults   ();
                break;
            case    9:      //  9 Save settings
                user_settings.save   ();
                pc.printf   ("Saving settings to EEPROM\r\n");
                break;
            default:
                break;
        }       //  endof   switch
    }           //  endof   //  If more than 0 parameters supplied
    else    {
        pc.printf   ("No Changes\r\n");
    }
    pc.printf   ("mode 0\t%s, [%d]\r\n", option_list2[0].t, user_settings.rd(RPM0));
    pc.printf   ("mode 1\t%s, [%d]\r\n", option_list2[1].t, user_settings.rd(RPM1));
    pc.printf   ("mode 2\t%s, [%d]\r\n", option_list2[2].t, user_settings.rd(RPM2));
    pc.printf   ("mode 3\t%s, [%d]\r\n", option_list2[3].t, user_settings.rd(RPM3));
    pc.printf   ("mode 4\t%s, [%d]\r\n", option_list2[4].t, user_settings.rd(RPM4));
    pc.printf   ("mode 5\t%s, [%d]\r\n", option_list2[5].t, user_settings.rd(RPM5));
    pc.printf   ("mode 6\t%s, [%d]\r\n", option_list2[6].t, user_settings.rd(RPM6));
    pc.printf   ("mode 7\t%s, [%d]\r\n", option_list2[7].t, user_settings.rd(RPM7));
    pc.printf   ("mode 8\t%s, [%d]\r\n", option_list2[8].t, user_settings.rd(RPM8));

    pc.printf   ("mode 37\t%s, [%d]\r\n", option_list2[PWM_SCALE].t, user_settings.rd(PWM_SCALE));
    pc.printf   ("mode 83\tSet to defaults\r\n");
    pc.printf   ("mode 9\tSave settings\r\r\n");

}

void    gpcmd   (struct parameters & a)   {
    pc.printf   ("pwm=%.3f\r\n", user_settings.get_pwm    ((int)a.dbl[0]));
}

extern  VEXT_Data   Field;

void    rfcmd   (struct parameters & a)   {
    pc.printf   ("Field.measured_period = %d, Field.measured_pw_us = %d, duty_cycle = %.3f\r\n", Field.measured_period, Field.measured_pw_us, Field.duty_cycle());
}

extern  void    set_RPM_demand  (uint32_t   d)  ;

void    set_rpm_cmd   (struct parameters & a)   {
    pc.printf   ("setting RPM to %d\r\n",(int)a.dbl[0]);
    set_RPM_demand  ((uint32_t)a.dbl[0]);
}

void    speedcmd   (struct parameters & a)   {
    int s = ReadEngineRPM  ();
    pc.printf   ("speed %d, pwm %.3f\r\n", s, user_settings.get_pwm(s));
}

void    vcmd    (struct parameters & a)   {
    pc.printf   ("volts %.2f\r\n", Read_BatteryVolts());
}

void    icmd    (struct parameters & a)   {
    double  results[4];
    //double * ampsptr = 
    Read_Ammeter(results)   ;
    pc.printf   ("amps %.3f, offset %.3f\r\n", results[0], results[1]);
}

extern  void    set_servo   (double p)  ;   //  Only for test, called from cli

void    set_servo_cmd    (struct parameters & a)   {
    double p = a.dbl[0] / 100.0;
    pc.printf   ("servo %.2f\r\n", p);
    set_servo   (p);
}

extern  bool    set_pwm (double)   ;    //  Range 0.0 to 1.0
void    p_cmd (struct parameters & a)   {
//    int32_t    i = (int32_t)a.dbl[0];
    pc.printf   ("Setting PWM to %d percent\r\n", (int)(a.dbl[0] * 100.0));
    set_pwm (a.dbl[0]);
}

void    null_cmd (struct parameters & a)   {
    pc.printf   ("At null_cmd, parameters : First %.3f, second %.3f\r\n", a.dbl[0], a.dbl[1]);
}

void    menucmd (struct parameters & a);

struct kb_command  {
    const char * cmd_word;         //  points to text e.g. "menu"
    const char * explan;
    void (*f)(struct parameters &);   //  points to function
}  ;

struct  kb_command const command_list[] = {
    {"?", "Lists available commands, same as ls", menucmd},
    {"rf", "Check rise and fall on VEXT", rfcmd},
    {"s", "Speed, RPM", speedcmd},
    {"v", "Read Battery volts", vcmd},
    {"i", "Read Ammeter", icmd},
    {"p", "Set PWM 0 to 2400???", p_cmd},
    {"q", "Query system - toggle message stream on/off", query_system},
    {"gp","Get pwm from RPM", gpcmd},
    {"mode", "See or set eeprom values", mode19_cmd},
    {"nu", "do nothing", null_cmd},
#ifndef SPEED_CONTROL_ENABLE    //  Includes engine revs servo control loop
    {"ser","set throttle servo direct 0 - 99", set_servo_cmd},
#endif
    {"sf","set speed control factor", speed_control_factor_set},
    {"sv","set engine RPM demand 2500 - 6000", set_rpm_cmd},
    {"tl","set throttle_limit 0.0-1.0", set_throttle_limit},
};

const int numof_menu_items = sizeof(command_list) / sizeof(kb_command);
void    menucmd (struct parameters & a)
{
    pc.printf("\r\nIntelligent Alternator Controller - Jon Freeman 2020\r\nAt menucmd function - listing commands:-\r\n");
    for(int i = 0; i < numof_menu_items; i++)
        pc.printf("[%s]\t\t%s\r\n", command_list[i].cmd_word, command_list[i].explan);
    pc.printf("End of List of Commands\r\n");
}

void    command_line_interpreter    ()
{

    const int MAX_CMD_LEN = 120;
    static  char    cmd_line[MAX_CMD_LEN + 4];
    static  int     cl_index = 0;
    int ch;
    char * pEnd;
    static struct  parameters  param_block  ;
    while  (pc.readable()) {
        ch = tolower(pc.getc());
       // pc.printf("%c", ch);
        if  (cl_index > MAX_CMD_LEN)  {   //  trap out stupidly long command lines
            pc.printf   ("Error!! Stupidly long cmd line\r\n");
            cl_index = 0;
        }
        if  (ch == '\r' || ch >= ' ' && ch <= 'z')
            pc.printf("%c", ch);
        else    {                   //  Using <Ctrl>+ 'F', 'B' for Y, 'L', 'R' for X, 'U', 'D' for Z
            cl_index = 0;           //                 6    2          12   18         21   4
            pc.printf("[%d]", ch);
            //nudger  (ch); //  was used on cnc to nudge axes a tad
        }
        if(ch != '\r')  //  was this the 'Enter' key?
            cmd_line[cl_index++] = ch;  //  added char to command being assembled
        else    {   //  key was CR, may or may not be command to lookup
            cmd_line[cl_index] = 0; //  null terminate command string
            if(cl_index)    {   //  If have got some chars to lookup
                int i, wrdlen;
                for (i = 0; i < numof_menu_items; i++)   {   //  Look for input match in command list
                    wrdlen = strlen(command_list[i].cmd_word);
                    if(strncmp(command_list[i].cmd_word, cmd_line, wrdlen) == 0 && !isalpha(cmd_line[wrdlen]))  {   //  If match found
                        for (int k = 0; k < MAX_PARAMS; k++)    {
                            param_block.dbl[k] = 0.0;
                        }
                        param_block.position_in_list = i;
                        param_block.last_time = clock    ();
                        param_block.numof_dbls = 0;
                        pEnd = cmd_line + wrdlen;
                        while   (*pEnd)  {          //  Assemble all numerics as doubles
                            param_block.dbl[param_block.numof_dbls++] = strtod    (pEnd, &pEnd);
                            while   (*pEnd && !isdigit(*pEnd) && '-' != *pEnd && '+' != *pEnd)  {   
                                pEnd++;
                            }
                        }
                        pc.printf   ("\r\n");
//                        for (int k = 0; k < param_block.numof_dbls; k++)
//                            pc.printf   ("Read %.3f\r\n", param_block.dbl[k]);
                        param_block.times[i] = clock();
                        command_list[i].f(param_block);   //  execute command
                        i = numof_menu_items + 1;    //  to exit for loop
                    }   //  end of match found
                }       // End of for numof_menu_items
                if(i == numof_menu_items)
                    pc.printf("No Match Found for CMD [%s]\r\n", cmd_line);
            }           //  End of If have got some chars to lookup
            pc.printf("\r\n>");
            cl_index = 0;
        }               // End of else key was CR, may or may not be command to lookup
    }                   //  End of while (pc.readable())

}