]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/src/rpp/hbr.c
Add error checking to rpp_hbr_enable()
[pes-rpp/rpp-lib.git] / rpp / src / rpp / hbr.c
1 /* Copyright (C) 2013 Czech Technical University in Prague
2  *
3  * Authors:
4  *     - Carlos Jenkins <carlos@jenkins.co.cr>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  * File : hbr.c
20  * Abstract:
21  *     H-Bridge Output RPP API implementation file.
22  *
23  * References:
24  *     hbr.h
25  *     RPP API documentation.
26  */
27
28
29 #include "rpp/rpp.h"
30
31 #if rppCONFIG_INCLUDE_HBR == 1
32
33 #if rppCONFIG_DRV == 1
34 #include "drv/hbridge.h"
35 #endif
36
37 static boolean_t initialized = FALSE;
38
39 int8_t rpp_hbr_init()
40 {
41     if(initialized) {
42         return FAILURE;
43     }
44     initialized = TRUE;
45
46     return SUCCESS;
47 }
48
49
50 // Private function to set the H-Bridge to default OFF settings
51 static void rpp_hdr_reset()
52 {
53     #if rppCONFIG_DRV == 1
54     drv_hbr_set_en(LOW);
55     drv_hbr_pwm_set_duty(0);
56     drv_hbr_pwm_stop();
57     drv_hbr_set_dir(LOW);
58     #endif
59 }
60
61
62 static boolean_t enabled = FALSE;
63
64 int8_t rpp_hbr_enable(int32_t period)
65 {
66     if(enabled) {
67         return FAILURE;
68     }
69
70     if(period < 1) {
71         period = 55; // ~18kHz (18181.818181818 Hz to be precise)
72     }
73
74     #if rppCONFIG_DRV == 1
75     // Configure H-Bridge
76     if (drv_hbr_pwm_set_signal(period, 0) != SUCCESS)
77             return FAILURE;
78     rpp_hdr_reset();
79     // Start watchdog
80     if(drv_hbr_wdg_start() != SUCCESS) {
81         return FAILURE;
82     }
83     #endif
84
85     enabled = TRUE;
86
87     return SUCCESS;
88 }
89
90
91 int8_t rpp_hbr_control(double cmd)
92 {
93     if(!enabled) {
94         return -1;
95     }
96
97     // Check range of the command
98     if((cmd < -1.0) || (cmd > 1.0)) {
99         return -2;
100     }
101
102     #if rppCONFIG_DRV == 1
103     // Great, now scale and return to sanity world of ints :D
104     int32_t scaled = (int32_t)(cmd * 100.0);
105
106     // Shutdown if required
107     if(scaled == 0) {
108         rpp_hdr_reset();
109         return SUCCESS;
110     }
111
112     /// Enabled, configure
113     // Set direction
114     drv_hbr_set_dir(scaled > 0);
115
116     // Set PWM duty cycle
117     drv_hbr_pwm_set_duty(abs(scaled));
118     drv_hbr_pwm_start();
119
120     // Enable H-Bridge
121     drv_hbr_set_en(HIGH);
122     #endif
123
124     return SUCCESS;
125 }
126
127
128 int8_t rpp_hbr_disable()
129 {
130     if(!enabled) {
131         return FAILURE;
132     }
133
134     #if rppCONFIG_DRV == 1
135     // We ignore is watchdog could not be stopped, because is harmless.
136     // It would be worse if we just could not stop the H-Bridge just because
137     // the watchdog could not be stopped.
138     drv_hbr_wdg_stop();
139     #endif
140
141     rpp_hdr_reset();
142     enabled = FALSE;
143
144     return SUCCESS;
145 }
146
147
148
149 #endif /* rppCONFIG_INCLUDE_HBR */