]> rtime.felk.cvut.cz Git - rpp-test-sw.git/blob - rpp-test-sw/commands/cmd_hbr.c
Change license to MIT
[rpp-test-sw.git] / rpp-test-sw / commands / cmd_hbr.c
1 /*
2  * Copyright (C) 2012-2014 Czech Technical University in Prague
3  *
4  * Created on: 28.2.2013
5  *
6  * Authors:
7  *     - Michal Horn
8  *
9  * Permission is hereby granted, free of charge, to any person
10  * obtaining a copy of this software and associated documentation
11  * files (the "Software"), to deal in the Software without
12  * restriction, including without limitation the rights to use,
13  * copy, modify, merge, publish, distribute, sublicense, and/or sell
14  * copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following
16  * conditions:
17  *
18  * The above copyright notice and this permission notice shall be
19  * included in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28  * OTHER DEALINGS IN THE SOFTWARE.
29  *
30  * File : cmd_hbr.c
31  *
32  * Abstract:
33  *      This file contains commands controlling H-bridge
34  *
35  */
36
37 #include "cmd_hbr.h"
38 #include "stdio.h"
39
40 #ifndef DOCGEN
41
42 #include "rpp/rpp.h"
43 #include "cmdproc_utils.h"
44
45
46 /**
47  * @brief       Enable HBR driver with given period of PWM
48  *
49  * @param[in]   cmd_io  Pointer to IO stack
50  * @param[in]   des             Pointer to command descriptor
51  * @param[in]   param   Parameters of command
52  * @return      0 when OK or error code
53  */
54 int cmd_do_hbr_enable(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
55 {
56         uint32_t period;
57         int ret;
58         char *p = param[1];
59         char spareParams;
60
61         if (sscanf(p, "%d %1s", &period, &spareParams) != 1)
62                 return -CMDERR_BADPAR;
63
64         ret = rpp_hbr_enable(period);
65         if (ret == FAILURE) {
66                 rpp_sci_printf("Enable procedure failed.\n");
67                 return -1;
68         }
69
70         rpp_sci_printf("hbrenable =%d\n", period, ret);
71         return 0;
72 }
73
74 /**
75  * @brief       Control HBR driver with given value <-100; 100>
76  *
77  * @param[in]   cmd_io  Pointer to IO stack
78  * @param[in]   des             Pointer to command descriptor
79  * @param[in]   param   Parameters of command
80  * @return      0 when OK or error code
81  */
82 int cmd_do_hbr_control(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
83 {
84         int cmd;
85         int ret;
86         char *p = param[1];
87         char spareParams;
88
89         if (sscanf(p, "%d %1s", &cmd, &spareParams) != 1)
90                 return -CMDERR_BADPAR;
91
92         ret = rpp_hbr_control((double)cmd/(double)100);
93         if (ret == -1) {
94                 rpp_sci_printf("H-bridge not enabled.\n");
95                 return -1;
96         }
97         else if (ret == -2) {
98                 rpp_sci_printf("Command out of range.\n");
99                 return -CMDERR_BADPAR;
100         }
101         rpp_sci_printf("hbrcontrol =%d\n", cmd, ret);
102         return 0;
103 }
104
105 /**
106  * @brief       Disable HBR driver.
107  *
108  * @param[in]   cmd_io  Pointer to IO stack
109  * @param[in]   des             Pointer to command descriptor
110  * @param[in]   param   Parameters of command
111  * @return      0 when OK or error code
112  */
113 int cmd_do_hbr_disable(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
114 {
115         int ret;
116
117         ret = rpp_hbr_disable();
118         if (ret == FAILURE)
119                 rpp_sci_printf("H-bridge already disabled.\n");
120         return cmd_opchar_replong(cmd_io, param, ret, 0, 10);
121 }
122
123 #endif  /* DOCGEN */
124
125 /** Command descriptor for HBR enable command */
126 cmd_des_t const cmd_des_hbr_enable = {
127         0, 0,
128         "hbrenable*","Enable the H-bridge and set its PWM period",
129         "### Command syntax ###\n"
130         "\n"
131         "    hbrenable<PER>\n"
132         "\n"
133         "where `<PER>` is PWM period in microseconds.\n"
134         "\n"
135         "### Description ###\n"
136         "\n"
137         "This command enables the H-bridge (HBR pin), i.e. the enable signal\n"
138         "the H-bridge chip is set, the watchdog reset task is started and the\n"
139         "PWM is configured with the specified period and duty cycle 0%.\n"
140         "\n"
141         "If the period is zero, the default frequency of 18kHz is\n"
142         "used instead. Minimum period is 50. This command should be\n"
143         "called before any other command\n"
144         "starting with hbr is used. If H-bridge is already enabled, an error is\n"
145         "printed.\n"
146         "\n"
147         "### Example ###\n"
148         "\n"
149         "    --> hbrenable1000\n"
150         "    hbrenable =1000\n"
151         "\n"
152         "Enables HBR with period 1000 microseconds (frequency 1 kHz). HBR\n"
153         "output is still inactive, but ready for other commands.\n",
154         CMD_HANDLER(cmd_do_hbr_enable), (void *)&cmd_list_hbr
155 };
156
157 /** Command descriptor for HBR control command */
158 cmd_des_t const cmd_des_hbr_control = {
159         0, 0,
160         "hbrcontrol*","Set the motor voltage direction and size in percent",
161         "### Command syntax ###\n"
162         "\n"
163         "    hbrcontrol<SPEED>\n"
164         "\n"
165         "where `<SPEED>` specifies direction and PWM duty cycle in percent (a\n"
166         "number in range -100, 100).\n"
167         "\n"
168         "### Description ###\n"
169         "\n"
170         "The command sets the direction and the size of the voltage at the HBR\n"
171         "output.\n"
172         "\n"
173         "HBR has to be enabled by the `hbrenable` command before calling this\n"
174         "command.\n"
175         "\n"
176         "### Examples ###\n"
177         "\n"
178         "    --> hbrcontrol-25\n"
179         "    hbrcontrol =-25\n"
180         "\n"
181         "Rotates the motor to the left with 25% duty cycle.\n"
182         "\n"
183         "    --> hbrcontrol25\n"
184         "    hbrcontrol =25\n"
185         "\n"
186         "Rotates the motor to the right with 25% duty cycle.\n"
187         "\n"
188         "    --> hbrcontrol0\n"
189         "    hbrcontrol =0\n"
190         "\n"
191         "Stops the motor, but the H-Bridge output is active (low-side active\n"
192         "free wheeling).\n",
193         CMD_HANDLER(cmd_do_hbr_control), (void *)&cmd_list_hbr
194 };
195
196 /** Command descriptor for HBR disable command */
197 cmd_des_t const cmd_des_hbr_disable = {
198         0, 0,
199         "hbrdisable","Disable the H-bridge",
200         "### Command syntax ###\n"
201         "\n"
202         "    hbrdisable\n"
203         "\n"
204         "### Description ###\n"
205         "\n"
206         "The command disables the H-bridge HBR, which means that the PWM is\n"
207         "stopped and the enable signal is cleared. The watchdog task is left\n"
208         "running, because it is harmless.\n"
209         "\n"
210         "After the H-bridge is disabled, it cannot be controlled by any command\n"
211         "until it is enabled again by the `hbrenable` command.\n"
212         "\n"
213         "### Example ###\n"
214         "\n"
215         "    --> hbrdisable\n"
216         "    hbrdisable=0\n"
217         "\n"
218         "Stops motor and disables the H-bridge.\n",
219         CMD_HANDLER(cmd_do_hbr_disable), (void *)&cmd_list_hbr
220 };
221
222 /** List of commands for hbr, defined as external */
223 cmd_des_t const *cmd_list_hbr[] = {
224         &cmd_des_hbr_enable,
225         &cmd_des_hbr_control,
226         &cmd_des_hbr_disable,
227         NULL
228 };