Added user-friendly interface.
authorMartin Prudek <prudemar@fel.cvut.cz>
Sat, 16 May 2015 09:54:47 +0000 (11:54 +0200)
committerMartin Prudek <prudemar@fel.cvut.cz>
Sat, 16 May 2015 09:54:47 +0000 (11:54 +0200)
pmsm-control/test_sw/cmd_proc.c

index da882c0..d5e3020 100644 (file)
 #include <stdlib.h>
 #include <stdio.h>
+#include <string.h>
 
 #include "cmd_proc.h"
 
 #define PRUM_PROUD     2061
 #define PRUM_SOUC      6183
 
+static char doPrint = 1;
+
+/*
+ * \brief
+ * Help
+ */
+static void printHelp(){
+       doPrint=0;
+       puts("start - Pripravi rizeni, zapne enable bity pwm.");
+       puts("stop - Vypne pwm a rizeni. Enable bity na nulu.");
+       puts("0 - Vypne pwm a rizeni. Enable bity na nulu.");
+       puts("ga:[hodnota] - Zapne rizeni na zvolenou absolutni pozici.");
+       puts("print - Zapne nebo vypne pravidelne vypisovani hodnot.");
+       puts("help - Vypne vypisovani hodnot a zobrazi tuto napovedu.");
+       puts("exit - Bezpecne ukonci program.");
+}
+
+
+/*
+ * \brief
+ * V prikazech je hodnota od zneni prikazu delena dvojteckou
+ * tato funkce dvojtecku nahradi mezerou
+ */
+static void delCol(char * txt){
+        unsigned i=0;
+        while(txt[i]!='\0'){
+                if (txt[i]==':') txt[i]=' ';
+                i++;
+        }
+}
+
+/*
+ * Nastavi enable bity na pwm,
+ * zapne komutaci
+ */
+static void start(struct rpi_state* state){
+       sem_wait(&state->thd_par_sem);
+       state->test=0x70;       /*konfiguracni byte*/
+       state->commutate=1;
+       sem_post(&state->thd_par_sem);
+}
+
+/*
+ * \brief
+ * Zastavi komutaci, vypne pwm
+ */
+static void stop(struct rpi_state* state){
+       sem_wait(&state->thd_par_sem);
+       state->test=0;          /*konfiguracni byte*/
+       state->commutate=0;
+       state->pwm1=0;
+       state->pwm2=0;
+       state->pwm3=0;
+       sem_post(&state->thd_par_sem);
+}
+
+/*
+ * \brief
+ * Nastavi pevnou sirku plneni
+ */
+static void dutySet(struct rpi_state* state, int duty){
+
+}
+
+/*
+ * \brief
+ * Zapne rizeni na zvolenou polohu vztazenou k pozici pri startu
+ */
+static void goAbsolute(struct rpi_state* state, int pos){
+       sem_wait(&state->thd_par_sem);
+       state->desired_pos=pos;
+       sem_post(&state->thd_par_sem);
+}
+
+/*
+ * \brief
+ * Zapne nebo vypne pravidelne vypisovani hodnot.
+ */
+static void changePrint(){
+       doPrint=!doPrint;
+}
+
+/*
+ * \brief
+ * Bezpecne ukonci program.
+ */
+static void exitApp(struct rpi_state* state){
+       stop(state);
+       /* Note: atexit() is set before*/
+       exit(0);
+}
+
 /**
  * \brief
  * Commands detection.
  */
 void poll_cmd(struct rpi_state* state){
        unsigned int tmp;
-       /*
-        * Note:
-        * pri pouziti scanf("%u",&simple_hall_duty); dochazelo
-        * k preukladani hodnot na promenne test. Dost divne.
-        */
-       while (1){
-               scanf("%u",&tmp);
-               printf("volba=%u\n",tmp);
-               switch (tmp){
-               case 1:
-                       scanf("%u",&tmp);
-                       sem_wait(&state->thd_par_sem);
-                       state->pwm1=tmp&0xFFF;
-                       sem_post(&state->thd_par_sem);
-                       break;
-               case 2:
-                       scanf("%u",&tmp);
-                       sem_wait(&state->thd_par_sem);
-                       state->pwm2=tmp&0xFFF;
-                       sem_post(&state->thd_par_sem);
-                       break;
-               case 3:
-                       scanf("%u",&tmp);
-                       sem_wait(&state->thd_par_sem);
-                       state->pwm3=tmp&0xFFF;
-                       sem_post(&state->thd_par_sem);
-                       break;
-               case 4:
-                       scanf("%u",&tmp);
-                       sem_wait(&state->thd_par_sem);
-                       state->test=tmp&0xFF;
-                       sem_post(&state->thd_par_sem);
-                       break;
-               case 5:
-                       sem_wait(&state->thd_par_sem);
-                       state->commutate=!state->commutate;
-                       /* switch off pwms at the end of commutation */
-                       state->pwm1&=state->commutate*0xFFFF;
-                       state->pwm2&=state->commutate*0xFFFF;
-                       state->pwm3&=state->commutate*0xFFFF;
-                       sem_post(&state->thd_par_sem);
-                       break;
-               case 6:
-                       scanf("%d",&tmp);
-                       sem_wait(&state->thd_par_sem);
-                       state->duty=tmp;
-                       sem_post(&state->thd_par_sem);
-                       break;
-               case 7:
-                       scanf("%d",&tmp);
-                       sem_wait(&state->thd_par_sem);
-                       state->desired_pos=tmp;
-                       sem_post(&state->thd_par_sem);
-                       break;
-
-               default:
-                       break;
-               }
+        char buff[50];
+        char cmd[30];
+        int val;
+
+        while(1){
+                scanf("%49s",buff);
+                delCol(buff);
+                sscanf(buff,"%s %d",cmd,&val);
+
+
+                if (!strcmp(cmd,"start")){
+                       start(state);
+                }else if (!strcmp(cmd,"0")){
+                       stop(state);
+                }else if (!strcmp(cmd,"stop")){
+                        stop(state);
+                }else if (!strcmp(cmd,"ga")){
+                       goAbsolute(state,val);
+                }else if (!strcmp(cmd,"duty")){
+                        dutySet(state,val);
+                }else if (!strcmp(cmd,"help")){
+                        printHelp();
+                }else if (!strcmp(cmd,"print")){
+                        changePrint();
+                }else if (!strcmp(cmd,"exit")){
+                        exitApp(state);
+                }
+        }
 
-       }
-       return ;
 }
+
 /*
  * pocita procentualni odchylku od prumerneho proudu
  */
@@ -91,6 +153,8 @@ float diff_s(float value){
  * tiskne potrebna data
  */
 void printData(struct rpi_state* state){
+       if (!doPrint) return;
+
        struct rpi_in data_p;
        struct rpi_state s;     /*state*/
        float cur0, cur1, cur2;