]> rtime.felk.cvut.cz Git - fpga/rpi-motor-control-pxmc.git/blob - src/app/rpi-pmsm-test1/appl_pxmccmds.c
7348848d31d5a4f94d6fcde64b0a35574d320afd
[fpga/rpi-motor-control-pxmc.git] / src / app / rpi-pmsm-test1 / appl_pxmccmds.c
1 /*******************************************************************
2   Motion and Robotic System (MARS) aplication components.
3
4   appl_pxmccmds.c - position controller RoCoN specific commands
5
6   Copyright (C) 2001-2013 by Pavel Pisa - originator
7                           pisa@cmp.felk.cvut.cz
8             (C) 2001-2013 by PiKRON Ltd. - originator
9                     http://www.pikron.com
10
11   This file can be used and copied according to next
12   license alternatives
13    - GPL - GNU Public License
14    - other license provided by project originators
15
16  *******************************************************************/
17
18 #include <system_def.h>
19 #include <pxmc.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27
28 #include "pxmc_cmds.h"
29
30 #include "appl_defs.h"
31 #include "appl_pxmc.h"
32 #include "pxmc_spimc.h"
33
34 #define SPIMC_LOG_CURRENT_SIZE 1024*1024
35
36 int32_t *spimc_logcurrent_buff;
37 int32_t *spimc_logcurrent_pos;
38
39 extern spimc_state_t spimc_state0;
40
41 int spimc_logcurrent(struct pxmc_state *mcs)
42 {
43   /*pxmc_spimc_state_t *mcsrc = pxmc_state2spimc_state(mcs); */
44   /*mcsrc->spimc_state*/
45   spimc_state_t *spimc = &spimc_state0;
46
47   if ((spimc_logcurrent_buff == NULL) ||
48       (spimc_logcurrent_pos == NULL) ||
49       ((char*)spimc_logcurrent_pos -
50        (char*)spimc_logcurrent_buff + 64 >= SPIMC_LOG_CURRENT_SIZE))
51     return 0;
52
53
54   spimc_logcurrent_pos[0] = mcs->pxms_ptindx;
55
56   spimc_logcurrent_pos[1] = spimc->pwm[0];
57   spimc_logcurrent_pos[2] = spimc->pwm[1];
58   spimc_logcurrent_pos[3] = spimc->pwm[2];
59
60   spimc_logcurrent_pos[4] = spimc->curadc_sqn;
61
62   spimc_logcurrent_pos[5] = spimc->curadc_cumsum[0];
63   spimc_logcurrent_pos[6] = spimc->curadc_cumsum[1];
64   spimc_logcurrent_pos[7] = spimc->curadc_cumsum[2];
65
66   spimc_logcurrent_pos += 8;
67
68   return 0;
69 }
70
71 int cmd_do_logcurrent(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
72 {
73   int fd;
74   size_t log_size;
75
76   if (pxmc_main_list.pxml_cnt < 1)
77     return -1;
78
79   if (pxmc_dbgset(pxmc_main_list.pxml_arr[0], NULL, 0) < 0)
80     return -1;
81
82   if (spimc_logcurrent_buff == NULL) {
83     spimc_logcurrent_buff = malloc(SPIMC_LOG_CURRENT_SIZE);
84     if (spimc_logcurrent_buff == NULL)
85       return -1;
86   }
87
88   if (spimc_logcurrent_pos != NULL) {
89     log_size = (char*)spimc_logcurrent_pos - (char*)spimc_logcurrent_buff;
90     printf("Log size %ld\n", (long)log_size);
91
92     if ((spimc_logcurrent_pos > spimc_logcurrent_buff) &&
93         (log_size < SPIMC_LOG_CURRENT_SIZE)) {
94
95       fd = open("currents.bin", O_WRONLY | O_CREAT | O_TRUNC,
96               S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
97
98       if (fd == -1)
99         return -1;
100
101       write(fd, spimc_logcurrent_buff, log_size);
102
103       close(fd);
104     }
105   }
106
107   spimc_logcurrent_pos = spimc_logcurrent_buff;
108
109   if (pxmc_dbgset(pxmc_main_list.pxml_arr[0], spimc_logcurrent, 1) < 0)
110     return -1;
111
112   return 0;
113 }
114
115 cmd_des_t const cmd_des_regcurdp={0, CDESM_OPCHR|CDESM_RW,
116                         "REGCURDP?","current controller d component p parameter", cmd_do_reg_short_val,
117                         {(char*)pxmc_spimc_state_offs(cur_d_p),
118                          0}};
119
120 cmd_des_t const cmd_des_regcurdi={0, CDESM_OPCHR|CDESM_RW,
121                         "REGCURDI?","current controller d component i parameter", cmd_do_reg_short_val,
122                         {(char*)pxmc_spimc_state_offs(cur_d_i),
123                          0}};
124
125 cmd_des_t const cmd_des_regcurqp={0, CDESM_OPCHR|CDESM_RW,
126                         "REGCURQP?","current controller q component p parameter", cmd_do_reg_short_val,
127                         {(char*)pxmc_spimc_state_offs(cur_q_p),
128                          0}};
129
130 cmd_des_t const cmd_des_regcurqi={0, CDESM_OPCHR|CDESM_RW,
131                         "REGCURQI?","current controller q component i parameter", cmd_do_reg_short_val,
132                         {(char*)pxmc_spimc_state_offs(cur_q_i),
133                          0}};
134
135 cmd_des_t const cmd_des_regcurhold={0, CDESM_OPCHR|CDESM_RW,
136                         "REGCURHOLD?","current steady hold value for stepper", cmd_do_reg_short_val,
137                         {(char*)pxmc_spimc_state_offs(cur_hold),
138                          0}};
139
140 cmd_des_t const cmd_des_logcurrent={0, 0,
141                         "logcurrent","log current history", cmd_do_logcurrent,
142                         {(char*)0,
143                          0}};
144
145
146 cmd_des_t const *cmd_appl_pxmc[] =
147 {
148   &cmd_des_regcurdp,
149   &cmd_des_regcurdi,
150   &cmd_des_regcurqp,
151   &cmd_des_regcurqi,
152   &cmd_des_regcurhold,
153   &cmd_des_logcurrent,
154   NULL
155 };