MotorController
Macros | Enumerations | Functions
motor.h File Reference

Go to the source code of this file.

Macros

#define MIN_COMMAND   1
 
#define MAX_COMMAND   100
 

Enumerations

enum  motor_t { MOTOR_LEFT = 0U, MOTOR_RIGHT = 1U }
 Alias to select the motor to work on. More...
 
enum  motor_direction_t { FORWARD = 0U, BACKWARD = 1U }
 Alias for motor rotation sense. More...
 
enum  motor_sense_t { DIRECTION_1 = 0U, DIRECTION_2 = 1U }
 Alias for motor orientation. More...
 

Functions

void motor_init (motor_sense_t motor_left_forward_sense, motor_sense_t motor_right_forward_sense)
 Initialise the motor driver. More...
 
int motor_set_speed (motor_t motor, uint8_t speed)
 Set the rotation speed of the specified motor. More...
 
int8_t motor_get_speed (motor_t motor)
 Get the rotation speed of the specified motor. More...
 
motor_direction_t motor_get_direction (motor_t motor)
 Get the current rotation direction of the specified motor. More...
 
int motor_set_direction (motor_t motor, motor_direction_t direction)
 Set the rotation direction of the specified motor. More...
 
void motor_toggle_direction (motor_t motor)
 Revert the rotation direction of the specified motor. More...
 

Macro Definition Documentation

§ MAX_COMMAND

#define MAX_COMMAND   100

Maximum possible value for commands

Definition at line 11 of file motor.h.

§ MIN_COMMAND

#define MIN_COMMAND   1

Minimal value that makes the robot move

Definition at line 9 of file motor.h.

Enumeration Type Documentation

§ motor_direction_t

Alias for motor rotation sense.

Enumerator
FORWARD 

Motor turns so that robot moves forward

BACKWARD 

Motor turns so that robot moves backward

Definition at line 28 of file motor.h.

§ motor_sense_t

Alias for motor orientation.

Remarks
These values allow to hide mechanical differences in motor installation so that a FORWARD move makes the motor turn in the proper sense. Values are totally arbitrary.
Enumerator
DIRECTION_1 

Value 1

DIRECTION_2 

Value 2

Definition at line 40 of file motor.h.

§ motor_t

enum motor_t

Alias to select the motor to work on.

Remarks
The meaning of the value "LEFT" and "RIGHT" is not necessarily coherent with the actual mechanical organization of the robot, it depends on initial user choice.
Enumerator
MOTOR_LEFT 

Left motor

MOTOR_RIGHT 

Right motor

Definition at line 20 of file motor.h.

Function Documentation

§ motor_get_direction()

motor_direction_t motor_get_direction ( motor_t  motor)

Get the current rotation direction of the specified motor.

Parameters
[in]motorThe motor to look at.
Returns
The current rotation direction of the specified motor.
Return values
FORWARD/BACKWARD
-1Invalid 'motor' parameter.

Definition at line 114 of file motor.c.

§ motor_get_speed()

int8_t motor_get_speed ( motor_t  motor)

Get the rotation speed of the specified motor.

Parameters
[in]motorThe motor to get the speed of.
Returns
The rotation speed of the specified motor or -1 in case of error.

Definition at line 103 of file motor.c.

§ motor_init()

void motor_init ( motor_sense_t  motor_left_forward_sense,
motor_sense_t  motor_right_forward_sense 
)

Initialise the motor driver.

This function is in charge of enabling all the low-level components used to control the motors. It configures the timers used to generate the PWM and starts them. It also sets the direction pins so that the motors turn forward as defined by the parameters.

Parameters
[in]motor_left_forward_senseThe rotation direction which corresponds to a forward movement of the left motor.
[in]motor_right_forward_senseThe rotation direction which corresponds to a forward movement of the right motor.
Note
To provide a high-level interface to control the motors, this driver uses the values FORWARD and BACKWARD to describe the rotation direction of the motors. However, these directions depend on how the motors are placed in the robot and how they are wired on the H-bridge. In order to foster reusability of this driver, the mapping between pins state and rotation direction is settable. The 2 parameters of this init function are for this purpose. DIRECTION_1 corresponds to PIN_A at '1' (+3V3) and PIN_B at '0' (GND). DIRECTION_2 corresponds to PIN_A at '0' (GND) and PIN_B at '1' (+3V3).
Warning
This function must be called first to start the motor driver.

Definition at line 48 of file motor.c.

§ motor_set_direction()

int motor_set_direction ( motor_t  motor,
motor_direction_t  direction 
)

Set the rotation direction of the specified motor.

Parameters
[in]motorTo motor to set the direction of.
[in]directionThe requested rotation direction.
Returns
An int indicating success, else an error code.
Return values
0Success.
-1Invalid 'motor' parameter.
-2Invalid 'direction' parameter (out of range).

Definition at line 123 of file motor.c.

§ motor_set_speed()

int motor_set_speed ( motor_t  motor,
uint8_t  speed 
)

Set the rotation speed of the specified motor.

Parameters
[in]motorThe motor to change the speed of.
[in]speedThe requested speed. Allowed range is [0,MAX_COMMAND]. Be careful that a minimum non-zero value (MIN_COMMAND) is required for the robot to move.
Returns
An int indicating success, else an error code.
Return values
0Success.
-1Invalid 'motor' parameter.
-2Invalid 'speed' parameter (out of range).

Definition at line 77 of file motor.c.

§ motor_toggle_direction()

void motor_toggle_direction ( motor_t  motor)

Revert the rotation direction of the specified motor.

Parameters
[in]motorTo motor to change the rotation direction of.

Definition at line 145 of file motor.c.