2 * Copyright (C) 2012-2014 Czech Technical University in Prague
4 * Created on: 28.2.2013
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
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
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.
33 * This file contains commands controlling H-bridge
43 #include "cmdproc_utils.h"
47 * @brief Enable HBR driver with given period of PWM
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
54 int cmd_do_hbr_enable(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
61 if (sscanf(p, "%d %1s", &period, &spareParams) != 1)
62 return -CMDERR_BADPAR;
64 ret = rpp_hbr_enable(period);
66 rpp_sci_printf("Enable procedure failed.\n");
70 rpp_sci_printf("hbrenable =%d\n", period, ret);
75 * @brief Control HBR driver with given value <-100; 100>
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
82 int cmd_do_hbr_control(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
89 if (sscanf(p, "%d %1s", &cmd, &spareParams) != 1)
90 return -CMDERR_BADPAR;
92 ret = rpp_hbr_control((double)cmd/(double)100);
94 rpp_sci_printf("H-bridge not enabled.\n");
98 rpp_sci_printf("Command out of range.\n");
99 return -CMDERR_BADPAR;
101 rpp_sci_printf("hbrcontrol =%d\n", cmd, ret);
106 * @brief Disable HBR driver.
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
113 int cmd_do_hbr_disable(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
117 ret = rpp_hbr_disable();
119 rpp_sci_printf("H-bridge already disabled.\n");
120 return cmd_opchar_replong(cmd_io, param, ret, 0, 10);
125 /** Command descriptor for HBR enable command */
126 cmd_des_t const cmd_des_hbr_enable = {
128 "hbrenable*","Enable the H-bridge and set its PWM period",
129 "### Command syntax ###\n"
133 "where `<PER>` is PWM period in microseconds.\n"
135 "### Description ###\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"
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"
149 " --> hbrenable1000\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
157 /** Command descriptor for HBR control command */
158 cmd_des_t const cmd_des_hbr_control = {
160 "hbrcontrol*","Set the motor voltage direction and size in percent",
161 "### Command syntax ###\n"
163 " hbrcontrol<SPEED>\n"
165 "where `<SPEED>` specifies direction and PWM duty cycle in percent (a\n"
166 "number in range -100, 100).\n"
168 "### Description ###\n"
170 "The command sets the direction and the size of the voltage at the HBR\n"
173 "HBR has to be enabled by the `hbrenable` command before calling this\n"
178 " --> hbrcontrol-25\n"
181 "Rotates the motor to the left with 25% duty cycle.\n"
183 " --> hbrcontrol25\n"
186 "Rotates the motor to the right with 25% duty cycle.\n"
191 "Stops the motor, but the H-Bridge output is active (low-side active\n"
193 CMD_HANDLER(cmd_do_hbr_control), (void *)&cmd_list_hbr
196 /** Command descriptor for HBR disable command */
197 cmd_des_t const cmd_des_hbr_disable = {
199 "hbrdisable","Disable the H-bridge",
200 "### Command syntax ###\n"
204 "### Description ###\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"
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"
218 "Stops motor and disables the H-bridge.\n",
219 CMD_HANDLER(cmd_do_hbr_disable), (void *)&cmd_list_hbr
222 /** List of commands for hbr, defined as external */
223 cmd_des_t const *cmd_list_hbr[] = {
225 &cmd_des_hbr_control,
226 &cmd_des_hbr_disable,