]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - rpp-test-sw/commands/cmd_hbr.c
Update library
[pes-rpp/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  * This document contains proprietary information belonging to Czech
10  * Technical University in Prague. Passing on and copying of this
11  * document, and communication of its contents is not permitted
12  * without prior written authorization.
13  *
14  * File : cmd_hbr.c
15  *
16  * Abstract:
17  *      This file contains commands controlling H-bridge
18  *
19  */
20
21 #include "cmd_hbr.h"
22 #include "stdio.h"
23
24 #ifndef DOCGEN
25
26 #include "rpp/rpp.h"
27 #include "cmdproc_utils.h"
28
29
30 /**
31  * @brief       Enable HBR driver with given period of PWM
32  *
33  * @param[in]   cmd_io  Pointer to IO stack
34  * @param[in]   des             Pointer to command descriptor
35  * @param[in]   param   Parameters of command
36  * @return      0 when OK or error code
37  */
38 int cmd_do_hbr_enable(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
39 {
40         uint32_t period;
41         int ret;
42         char *p = param[1];
43         char spareParams;
44
45         if (sscanf(p, "%d %1s", &period, &spareParams) != 1)
46                 return -CMDERR_BADPAR;
47
48         ret = rpp_hbr_enable(period);
49         if (ret == FAILURE) {
50                 rpp_sci_printf("Enable procedure failed.\n");
51                 return -1;
52         }
53
54         rpp_sci_printf("hbrenable =%d\n", period, ret);
55         return 0;
56 }
57
58 /**
59  * @brief       Control HBR driver with given value <-100; 100>
60  *
61  * @param[in]   cmd_io  Pointer to IO stack
62  * @param[in]   des             Pointer to command descriptor
63  * @param[in]   param   Parameters of command
64  * @return      0 when OK or error code
65  */
66 int cmd_do_hbr_control(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
67 {
68         int cmd;
69         int ret;
70         char *p = param[1];
71         char spareParams;
72
73         if (sscanf(p, "%d %1s", &cmd, &spareParams) != 1)
74                 return -CMDERR_BADPAR;
75
76         ret = rpp_hbr_control((double)cmd/(double)100);
77         if (ret == -1) {
78                 rpp_sci_printf("H-bridge not enabled.\n");
79                 return -1;
80         }
81         else if (ret == -2) {
82                 rpp_sci_printf("Command out of range.\n");
83                 return -CMDERR_BADPAR;
84         }
85         rpp_sci_printf("hbrcontrol =%d\n", cmd, ret);
86         return 0;
87 }
88
89 /**
90  * @brief       Disable HBR driver.
91  *
92  * @param[in]   cmd_io  Pointer to IO stack
93  * @param[in]   des             Pointer to command descriptor
94  * @param[in]   param   Parameters of command
95  * @return      0 when OK or error code
96  */
97 int cmd_do_hbr_disable(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
98 {
99         int ret;
100
101         ret = rpp_hbr_disable();
102         if (ret == FAILURE)
103                 rpp_sci_printf("H-bridge already disabled.\n");
104         return cmd_opchar_replong(cmd_io, param, ret, 0, 10);
105 }
106
107 #endif  /* DOCGEN */
108
109 /** Command descriptor for HBR enable command */
110 cmd_des_t const cmd_des_hbr_enable = {
111         0, 0,
112         "hbrenable*","Enable the H-bridge and set its PWM period",
113         "### Command syntax ###\n"
114         "\n"
115         "    hbrenable<PER>\n"
116         "\n"
117         "where `<PER>` is PWM period in microseconds.\n"
118         "\n"
119         "### Description ###\n"
120         "\n"
121         "This command enables the H-bridge (HBR pin), i.e. the enable signal\n"
122         "the H-bridge chip is set, the watchdog reset task is started and the\n"
123         "PWM is configured with the specified period and duty cycle 0%.\n"
124         "\n"
125         "If the period is zero, the default frequency of 18kHz is\n"
126         "used instead. Minimum period is 50. This command should be\n"
127         "called before any other command\n"
128         "starting with hbr is used. If H-bridge is already enabled, an error is\n"
129         "printed.\n"
130         "\n"
131         "### Example ###\n"
132         "\n"
133         "    --> hbrenable1000\n"
134         "    hbrenable =1000\n"
135         "\n"
136         "Enables HBR with period 1000 microseconds (frequency 1 kHz). HBR\n"
137         "output is still inactive, but ready for other commands.\n",
138         CMD_HANDLER(cmd_do_hbr_enable), (void *)&cmd_list_hbr
139 };
140
141 /** Command descriptor for HBR control command */
142 cmd_des_t const cmd_des_hbr_control = {
143         0, 0,
144         "hbrcontrol*","Set the motor voltage direction and size in percent",
145         "### Command syntax ###\n"
146         "\n"
147         "    hbrcontrol<SPEED>\n"
148         "\n"
149         "where `<SPEED>` specifies direction and PWM duty cycle in percent (a\n"
150         "number in range -100, 100).\n"
151         "\n"
152         "### Description ###\n"
153         "\n"
154         "The command sets the direction and the size of the voltage at the HBR\n"
155         "output.\n"
156         "\n"
157         "HBR has to be enabled by the `hbrenable` command before calling this\n"
158         "command.\n"
159         "\n"
160         "### Examples ###\n"
161         "\n"
162         "    --> hbrcontrol-25\n"
163         "    hbrcontrol =-25\n"
164         "\n"
165         "Rotates the motor to the left with 25% duty cycle.\n"
166         "\n"
167         "    --> hbrcontrol25\n"
168         "    hbrcontrol =25\n"
169         "\n"
170         "Rotates the motor to the right with 25% duty cycle.\n"
171         "\n"
172         "    --> hbrcontrol0\n"
173         "    hbrcontrol =0\n"
174         "\n"
175         "Stops the motor, but the H-Bridge output is active (low-side active\n"
176         "free wheeling).\n",
177         CMD_HANDLER(cmd_do_hbr_control), (void *)&cmd_list_hbr
178 };
179
180 /** Command descriptor for HBR disable command */
181 cmd_des_t const cmd_des_hbr_disable = {
182         0, 0,
183         "hbrdisable","Disable the H-bridge",
184         "### Command syntax ###\n"
185         "\n"
186         "    hbrdisable\n"
187         "\n"
188         "### Description ###\n"
189         "\n"
190         "The command disables the H-bridge HBR, which means that the PWM is\n"
191         "stopped and the enable signal is cleared. The watchdog task is left\n"
192         "running, because it is harmless.\n"
193         "\n"
194         "After the H-bridge is disabled, it cannot be controlled by any command\n"
195         "until it is enabled again by the `hbrenable` command.\n"
196         "\n"
197         "### Example ###\n"
198         "\n"
199         "    --> hbrdisable\n"
200         "    hbrdisable=0\n"
201         "\n"
202         "Stops motor and disables the H-bridge.\n",
203         CMD_HANDLER(cmd_do_hbr_disable), (void *)&cmd_list_hbr
204 };
205
206 /** List of commands for hbr, defined as external */
207 cmd_des_t const *cmd_list_hbr[] = {
208         &cmd_des_hbr_enable,
209         &cmd_des_hbr_control,
210         &cmd_des_hbr_disable,
211         NULL
212 };