Skip to content

Commit c9cdb30

Browse files
committed
pbio/dcmotor: Document module.
1 parent 635ae0d commit c9cdb30

File tree

2 files changed

+101
-15
lines changed

2 files changed

+101
-15
lines changed

lib/pbio/include/pbio/dcmotor.h

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,42 @@
1919
#include <pbio/parent.h>
2020
#include <pbio/port.h>
2121

22+
/**
23+
* Direction a motor turns in when a positive voltage is applied.
24+
*/
2225
typedef enum {
23-
PBIO_DIRECTION_CLOCKWISE, /**< Positive means clockwise */
24-
PBIO_DIRECTION_COUNTERCLOCKWISE, /**< Positive means counterclockwise */
26+
PBIO_DIRECTION_CLOCKWISE, /**< Positive means clockwise. */
27+
PBIO_DIRECTION_COUNTERCLOCKWISE, /**< Positive means counterclockwise. */
2528
} pbio_direction_t;
2629

30+
/**
31+
* Actuation types that can be applied by a dc motor.
32+
*/
2733
typedef enum {
28-
PBIO_DCMOTOR_ACTUATION_COAST, /**< Coast the motor */
29-
PBIO_DCMOTOR_ACTUATION_BRAKE, /**< Brake the motor */
30-
PBIO_DCMOTOR_ACTUATION_VOLTAGE, /**< Apply a voltage */
31-
PBIO_DCMOTOR_ACTUATION_TORQUE, /**< Apply a torque */
34+
PBIO_DCMOTOR_ACTUATION_COAST, /**< Coast the motor. */
35+
PBIO_DCMOTOR_ACTUATION_BRAKE, /**< Brake the motor. */
36+
PBIO_DCMOTOR_ACTUATION_VOLTAGE, /**< Apply a voltage. */
37+
PBIO_DCMOTOR_ACTUATION_TORQUE, /**< Apply a torque. */
3238
} pbio_dcmotor_actuation_t;
3339

40+
/**
41+
* DC Motor instance.
42+
*/
3443
typedef struct _pbio_dcmotor_t {
35-
pbio_port_id_t port; /**< Port to which this motor is attached */
36-
pbio_direction_t direction; /**< Direction for positive speeds. */
37-
pbio_dcmotor_actuation_t actuation_now; /**< Currently active actuation type. */
38-
int32_t voltage_now; /**< Currently applied voltage. */
39-
int32_t max_voltage; /**< Maximum allowed voltage. */
40-
pbio_parent_t parent; /**< Parent object (like a servo) that uses this motor. */
41-
pbdrv_motor_driver_dev_t *motor_driver; /**< Low level motor driver */
44+
/** Port to which this motor is attached. */
45+
pbio_port_id_t port;
46+
/** Direction for positive speeds. */
47+
pbio_direction_t direction;
48+
/** Currently active actuation type. */
49+
pbio_dcmotor_actuation_t actuation_now;
50+
/** Currently applied voltage. */
51+
int32_t voltage_now;
52+
/** Maximum allowed voltage. */
53+
int32_t max_voltage;
54+
/** Parent object (like a servo) that uses this motor. */
55+
pbio_parent_t parent;
56+
/** Low level motor driver. */
57+
pbdrv_motor_driver_dev_t *motor_driver;
4258
} pbio_dcmotor_t;
4359

4460
#if PBIO_CONFIG_DCMOTOR

lib/pbio/src/dcmotor.c

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@
2222

2323
static pbio_dcmotor_t dcmotors[PBDRV_CONFIG_NUM_MOTOR_CONTROLLER];
2424

25-
// Stop all motors and their parent objects.
25+
/**
26+
* Stops all motors and all higher level parent objects that use them.
27+
*
28+
* @param [in] clear_parents Whether to not only stop the parent object
29+
* physically, but also clear the dcmotor's
30+
* knowledge about that object. Choosing true frees
31+
* up all motors to be used again by new parent
32+
* objects.
33+
*/
2634
void pbio_dcmotor_stop_all(bool clear_parents) {
2735

2836
// Go through all ports.
@@ -119,25 +127,63 @@ pbio_error_t pbio_dcmotor_get_dcmotor(pbio_port_id_t port, pbio_dcmotor_t **dcmo
119127
return PBIO_SUCCESS;
120128
}
121129

130+
/**
131+
* Gets the actuation type that is currently being applied to the motor.
132+
*
133+
* @param [in] dcmotor The dcmotor instance.
134+
* @param [out] actuation Actuation type, ::PBIO_DCMOTOR_ACTUATION_COAST
135+
* or ::PBIO_DCMOTOR_ACTUATION_VOLTAGE.
136+
* @param [out] voltage_now Voltage in mV if @ actuation is voltage. Else 0.
137+
*/
122138
void pbio_dcmotor_get_state(pbio_dcmotor_t *dcmotor, pbio_dcmotor_actuation_t *actuation, int32_t *voltage_now) {
123139
*actuation = dcmotor->actuation_now;
124140
*voltage_now = dcmotor->voltage_now;
125141
}
126142

143+
/**
144+
* Gets the maximum allowed voltage for a motor.
145+
*
146+
* @param [in] id Device type id.
147+
* @return Maximum voltage (mV) for the given motor type.
148+
*/
127149
int32_t pbio_dcmotor_get_max_voltage(pbio_iodev_type_id_t id) {
128150
if (id == PBIO_IODEV_TYPE_ID_SPIKE_S_MOTOR) {
129151
return 6000;
130152
}
131153
return 9000;
132154
}
133155

156+
/**
157+
* Coasts the dc motor.
158+
*
159+
* This just sets the physical state and does not stop parent objects. It
160+
* should only be used by higher level objects. Users should use
161+
* ::pbio_dcmotor_user_command instead.
162+
*
163+
* @param [in] dcmotor The motor instance.
164+
* @return Error code.
165+
*/
134166
pbio_error_t pbio_dcmotor_coast(pbio_dcmotor_t *dcmotor) {
135167
// Stop the motor and set the passivity state value for data logging.
136168
dcmotor->actuation_now = PBIO_DCMOTOR_ACTUATION_COAST;
137169
dcmotor->voltage_now = 0;
138170
return pbdrv_motor_driver_coast(dcmotor->motor_driver);
139171
}
140172

173+
/**
174+
* Sets the dc motor voltage.
175+
*
176+
* A positive voltage will cause a rotation in the direction configured in
177+
* ::pbio_dcmotor_setup. A negative voltage makes it turn in the other way.
178+
*
179+
* This just sets the physical state and does not stop parent objects. It
180+
* should only be used by higher level objects. Users should use
181+
* ::pbio_dcmotor_user_command instead.
182+
*
183+
* @param [in] dcmotor The motor instance.
184+
* @param [in] voltage The voltage in mV.
185+
* @return Error code.
186+
*/
141187
pbio_error_t pbio_dcmotor_set_voltage(pbio_dcmotor_t *dcmotor, int32_t voltage) {
142188
// Cap voltage at the configured limit.
143189
if (voltage > dcmotor->max_voltage) {
@@ -167,6 +213,18 @@ pbio_error_t pbio_dcmotor_set_voltage(pbio_dcmotor_t *dcmotor, int32_t voltage)
167213
return PBIO_SUCCESS;
168214
}
169215

216+
/**
217+
* Sets a voltage or coasts the motor, and stops higher level objects.
218+
*
219+
* A positive voltage will cause a rotation in the direction configured in
220+
* ::pbio_dcmotor_setup. A negative voltage makes it turn in the other way.
221+
*
222+
* @param [in] dcmotor The motor instance.
223+
* @param [in] coast If true, the motor coasts. If false, applies the
224+
* given voltage.
225+
* @param [in] voltage The voltage in mV.
226+
* @return Error code.
227+
*/
170228
pbio_error_t pbio_dcmotor_user_command(pbio_dcmotor_t *dcmotor, bool coast, int32_t voltage) {
171229
// Stop parent object that uses this motor, if any.
172230
pbio_error_t err = pbio_parent_stop(&dcmotor->parent, false);
@@ -181,11 +239,23 @@ pbio_error_t pbio_dcmotor_user_command(pbio_dcmotor_t *dcmotor, bool coast, int3
181239
return pbio_dcmotor_set_voltage(dcmotor, voltage);
182240
}
183241

184-
242+
/**
243+
* Gets the settings for this motor.
244+
*
245+
* @param [in] dcmotor The motor instance.
246+
* @param [out] max_voltage The configured maximum voltage for this motor.
247+
*/
185248
void pbio_dcmotor_get_settings(pbio_dcmotor_t *dcmotor, int32_t *max_voltage) {
186249
*max_voltage = dcmotor->max_voltage;
187250
}
188251

252+
/**
253+
* Sets the settings for this motor.
254+
*
255+
* @param [in] dcmotor The motor instance.
256+
* @param [in] max_voltage Maximum voltage (mV) for this motor.
257+
* @return Error code.
258+
*/
189259
pbio_error_t pbio_dcmotor_set_settings(pbio_dcmotor_t *dcmotor, int32_t max_voltage) {
190260

191261
// Get the device type.

0 commit comments

Comments
 (0)