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