Electric Locomotive control system. Touch screen driver control, includes regenerative braking, drives 4 brushless motors, displays speed MPH, system volts and power

Dependencies:   BSP_DISCO_F746NG FastPWM LCD_DISCO_F746NG SD_DISCO_F746NG TS_DISCO_F746NG mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cli_nortos.cpp Source File

cli_nortos.cpp

00001 #include "mbed.h"
00002 #include <cctype>
00003 using namespace std;
00004 enum    {FWD, REV}  ;
00005 //typedef float  fl_typ;  //  
00006 typedef double  fl_typ;  //  
00007 struct  singleGparam    {
00008     float          flt;
00009     unsigned long   ul;
00010     int         i,  c;
00011     bool            changed;
00012 }   ;
00013 
00014 //  Cleaned up version for use without RTOS
00015 extern  Serial pc;
00016 
00017 void    setpwm_cmd (struct singleGparam * a)   {
00018     //a[1].i  //  first param
00019     pc.printf   ("First %d, second %d\r\n", a[1].i, a[2].i);
00020 //??    set_pwm   (a[1].i);
00021 }
00022 
00023 extern  int SD_card_erase_all   (void)  ;   //  assumes sd card is 4 Gbyte, erases 4 Gbyte.
00024 void    erase_SD_cmd (struct singleGparam * a)   {
00025     SD_card_erase_all   ()  ;
00026 }
00027 
00028 void    setvref_cmd (struct singleGparam * a)   {
00029     //a[1].i  //  first param
00030     pc.printf   ("First %d, second %d\r\n", a[1].i, a[2].i);
00031 //??    set_I_limit   (a[1].i);
00032 }
00033 
00034 /*
00035 void    setmotpwm_cmd (struct singleGparam * a)   {
00036     //a[1].i  //  first param
00037     pc.printf   ("First %d, second %d\r\n", a[1].i, a[2].i);
00038 //    set_motor_pwm   (a[1].i, a[2].i);
00039 }
00040 //extern  void    set_fwd_rev (int direction) ;
00041 void    set_fwd_cmd (struct singleGparam * a)   {
00042 //    set_fwd_rev   (FWD);
00043 }
00044 void    set_rev_cmd (struct singleGparam * a)   {
00045 //    set_fwd_rev   (REV);
00046 }
00047 
00048 void    set_speed_cmd (struct singleGparam * a)   {
00049     pc.printf   ("Speed entered %d\r\n", a[1].i);
00050 }
00051 
00052 void    read_current_cmd (struct singleGparam * a)   {
00053     pc.printf   ("Read current\r\n");
00054 }
00055 */
00056 void    menucmd (struct singleGparam * a);
00057 
00058 struct kb_command  {
00059     const char * cmd_word;         //  points to text e.g. "menu"
00060     const char * explan;
00061     void (*f)(struct singleGparam *);   //  points to function
00062 }  ;
00063 
00064 struct  kb_command const command_list[] = {
00065     {"menu", "Lists available commands, same as ls", menucmd},
00066     {"ls", "Lists available commands, same as menu", menucmd},
00067     {"pw", "set pwm 0 to 999", setpwm_cmd},
00068     {"vref", "set vref 0 to 999", setvref_cmd},
00069     {"sd erase", "set motors in one direction", erase_SD_cmd},
00070 //    {"rev", "set motors in tother direction", set_rev_cmd},
00071 //    {"s", "set speed", set_speed_cmd},
00072 //    {"i", "Read motor currents", read_current_cmd},
00073 };
00074 
00075 const int numof_menu_items = sizeof(command_list) / sizeof(kb_command);
00076 void    menucmd (struct singleGparam * a)
00077 {
00078     pc.printf("\r\n\nLoco_TS_2017\r\nAt menucmd function - listing commands:-\r\n");
00079     for(int i = 0; i < numof_menu_items; i++)
00080         pc.printf("[%s]\t\t%s\r\n", command_list[i].cmd_word, command_list[i].explan);
00081     pc.printf("End of List of Commands\r\n");
00082 }
00083 
00084 
00085 void    grain_clr   (struct singleGparam & g)  {
00086     g.flt = 0.0;
00087     g.ul = 0L;
00088     g.i = g.c = 0;
00089     g.changed = false;
00090 }
00091 
00092 //void    command_line_interpreter    (void const * name)
00093 void    command_line_interpreter    ()
00094 {
00095 const int MAX_PARAMS = 10, MAX_CMD_LEN = 120;
00096 static  char    cmd_line[MAX_CMD_LEN + 4];
00097 static  struct  singleGparam   params[MAX_PARAMS + 1];
00098 static  int     cl_index = 0, lastalpha = 0;
00099 static  fl_typ  fracmul;
00100 int ch;
00101 ////No RTOS    while   (true)  {
00102         while  (pc.readable()) {
00103             ch = tolower(pc.getc());
00104            // pc.printf("%c", ch);
00105             if  (cl_index > MAX_CMD_LEN)  {   //  trap out stupidly long command lines
00106                 pc.printf   ("Error!! Stupidly long cmd line\r\n");
00107                 cl_index = 0;
00108             }
00109             if  (ch == '\r' || ch >= ' ' && ch <= 'z')
00110                 pc.printf("%c", ch);
00111             else    {                   //  Using <Ctrl>+ 'F', 'B' for Y, 'L', 'R' for X, 'U', 'D' for Z
00112                 cl_index = 0;           //                 6    2          12   18         21   4
00113                 pc.printf("[%d]", ch);
00114                 //nudger  (ch); //  was used on cnc to nudge axes a tad
00115             }
00116             if(ch != '\r')  //  was this the 'Enter' key?
00117                 cmd_line[cl_index++] = ch;  //  added char to command being assembled
00118             else    {   //  key was CR, may or may not be command to lookup
00119                 cmd_line[cl_index] = 0; //  null terminate command string
00120                 if(cl_index)    {   //  If have got some chars to lookup
00121                     int i, wrdlen;
00122                     for (i = 0; i < numof_menu_items; i++)   {   //  Look for input match in command list
00123                         wrdlen = strlen(command_list[i].cmd_word);
00124                         if(strncmp(command_list[i].cmd_word, cmd_line, wrdlen) == 0
00125                             && !isalpha(cmd_line[wrdlen]))  {   //  If match found
00126                             bool negflag = false;
00127                             int state = 0, paramindex;
00128     //                            pc.printf("Found match for word [%s]\r\n", kbc[i].wrd);
00129                             for(paramindex = 0; paramindex < MAX_PARAMS; paramindex++) 
00130                                 grain_clr   (params[paramindex]);
00131                             paramindex = 0;
00132                             //  read any parameters from command line here
00133                             //  Using parameters[0] as count of parameters to follow
00134                             while   (wrdlen <= cl_index)  {
00135                                 ch = cmd_line[wrdlen++];
00136                                 if(isalpha(ch)) lastalpha = ch;
00137                                 if(ch == '-')   negflag = true;
00138                                 if(ch == '+')   negflag = false;
00139                                 switch  (state) {
00140                                     case    0:  //  looking for start of a number string
00141                                         if(isdigit(ch)) {   //  found first digit of a number string
00142                                             paramindex++;
00143                                             if(paramindex > MAX_PARAMS)    {
00144                                                 wrdlen = cl_index;  //  exit condition
00145                                                 pc.printf("WARNING - too many parameters, ignoring extra\r\n");
00146                                             } else    {
00147                                                 params[paramindex].i = ch - '0';
00148                                                 params[paramindex].c = lastalpha;
00149                                                 state = 1;  //  Found first digit char of number string
00150                                             }
00151                                         }
00152                                         break;
00153                                     case    1:  //  looking for end of a number string
00154                                         if(isdigit(ch)) {   //  accumulating integer from string
00155                                             params[paramindex].i *= 10;
00156                                             params[paramindex].i += ch - '0';
00157                                         } else    { //  found non-digit terminating number
00158                                             if  (ch == '.')  {
00159                                                 state = 2;
00160                                                 fracmul = 0.1;
00161                                                 params[paramindex].flt = (fl_typ)params[paramindex].i;
00162                                             } else    {
00163                                                 params[0].i++;    //  count of validated parameters
00164                                                 state = 0;  //  Have read past last digit of number string
00165                                                 if(negflag) {
00166                                                     params[paramindex].i = -params[paramindex].i;
00167                                                     negflag = false;
00168                                                 }
00169                                                 params[paramindex].flt = (fl_typ)params[paramindex].i;
00170                                             }
00171                                         }
00172                                         break;
00173                                     case    2:  //  looking for fractional part of double
00174                                         if(isdigit(ch)) {   //  accumulating fractional part from string
00175                                             params[paramindex].flt += (fl_typ)((ch - '0') * fracmul);
00176                                             fracmul /= 10.0;
00177                                         } else    { //  found non-digit terminating double precision number
00178                                             params[0].i++;    //  count of validated parameters
00179                                             state = 0;  //  Have read past last digit of number string
00180                                             if(negflag) {
00181                                                 params[paramindex].i = -params[paramindex].i;
00182                                                 params[paramindex].flt = -params[paramindex].flt;
00183                                                 negflag = false;
00184                                             }
00185                                         }
00186                                         break;
00187                                     default:
00188                                         break;
00189                                 }   //  end of switch state
00190                             }       //  end of while wrdlen < cl_index
00191     //                            pc.printf("Found match to [%s] with %d parameters\r\n", command_list_ptr[i].wrd, paramindex);
00192                             command_list[i].f(params);   //  execute command
00193                             i = numof_menu_items + 1;    //  to exit for loop
00194                         }
00195                     }       // End of for numof_menu_items
00196                     if(i == numof_menu_items)
00197                         pc.printf("No Match Found for CMD [%s]\r\n", cmd_line);
00198                 }           //  End of If have got some chars to lookup
00199                 pc.printf("\r\n>");
00200                 cl_index = lastalpha = 0;
00201             }               // End of else key was CR, may or may not be command to lookup
00202         }                   //  End of while (pc.readable())
00203 //No RTOS        osThreadYield();  //  Using RTOS on this project
00204 //    }
00205 }
00206 
00207