INF2004-Project v0.1
 
Loading...
Searching...
No Matches
motor_control.h File Reference

Header file for motor control drivers. More...

#include <stdbool.h>
#include "pico/stdlib.h"
#include "pico/types.h"
Include dependency graph for motor_control.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  motor_pins
 Contains the GPIO pins for the motor. More...
 

Macros

#define MOTOR_DEFAULT_DIFF_RATIO   1.05f
 Default ratio for motor speed difference.
 
#define MOTOR_LEFT_PIN_ANTICLKWISE   17
 GPIO pin for left motor anti-clockwise.
 
#define MOTOR_LEFT_PIN_CLKWISE   16
 GPIO pin for left motor clockwise.
 
#define MOTOR_PWM_CLKDIV   100
 PWM clock divider.
 
#define MOTOR_PWM_PIN_LEFT   10
 GPIO pin for left motor PWM.
 
#define MOTOR_PWM_PIN_RIGHT   11
 GPIO pin for right motor PWM.
 
#define MOTOR_PWM_WRAP   62500
 PWM wrap value.
 
#define MOTOR_RIGHT_PIN_ANTICLKWISE   14
 GPIO pin for right motor anti-clockwise.
 
#define MOTOR_RIGHT_PIN_CLKWISE   15
 GPIO pin for right motor clockwise.
 

Typedefs

typedef struct motor_pins motor_pins_t
 Contains the GPIO pins for the motor.
 

Functions

void motor_move_forward (void)
 A function to move the car forward. More...
 
void motor_reverse (void)
 A function to reverse the car. More...
 
void motor_start (uint clkwise_gpio, uint anti_clkwise_gpio, uint pwm_gpio)
 Default initialization of a single motor. More...
 
void motor_stop (void)
 A function to stop the car. More...
 
void motor_turn_left (bool reverse_turn)
 A function to turn the car left. More...
 
void motor_turn_right (bool reverse_turn)
 A function to turn the car right. More...
 
void motor_update_direction (uint clkwise_gpio, uint anti_clkwise_gpio, uint clkwise, uint anti_clkwise)
 A function to update the direction of the motor's rotation. More...
 
void motor_update_pwm (uint pwm_gpio, uint pwm_wrap, float duty_cycle)
 A function to update the PWM duty cycle and wrap value. More...
 
void motor_update_ratio (float new_ratio)
 Update the ratio of the left motor's duty cycle to the right motor's duty cycle. More...
 

Detailed Description

Header file for motor control drivers.

Author
Bryan Seah
Version
0.1
Date
2023-10-29

Function Documentation

◆ motor_move_forward()

void motor_move_forward ( void  )

A function to move the car forward.

This function moves the car forward by setting the duty cycle of both motors to be equal, as well as setting a clockwise rotation for both motors.

References MOTOR_LEFT_PIN_ANTICLKWISE, MOTOR_LEFT_PIN_CLKWISE, MOTOR_PWM_PIN_LEFT, MOTOR_PWM_PIN_RIGHT, MOTOR_PWM_WRAP, MOTOR_RIGHT_PIN_ANTICLKWISE, MOTOR_RIGHT_PIN_CLKWISE, motor_update_direction(), and motor_update_pwm().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ motor_reverse()

void motor_reverse ( void  )

A function to reverse the car.

This function reverses the car by setting the duty cycle of both motors to be equal, as well as setting an anti-clockwise rotation for both motors.

References MOTOR_LEFT_PIN_ANTICLKWISE, MOTOR_LEFT_PIN_CLKWISE, MOTOR_PWM_PIN_LEFT, MOTOR_PWM_PIN_RIGHT, MOTOR_PWM_WRAP, MOTOR_RIGHT_PIN_ANTICLKWISE, MOTOR_RIGHT_PIN_CLKWISE, motor_update_direction(), and motor_update_pwm().

Here is the call graph for this function:

◆ motor_start()

void motor_start ( uint  clkwise_gpio,
uint  anti_clkwise_gpio,
uint  pwm_gpio 
)

Default initialization of a single motor.

The function initializes the PWM pin and the two GPIO pins for the
motor. It also sets the clock divider to 100 and enables the PWM slice.
Parameters
[in]clkwise_gpioGPIO pin number for clockwise rotation.
[in]anti_clkwise_gpioGPIO pin number for anticlockwise rotation.
[in]pwm_gpioGPIO pin number for PWM output to control motor speed.

References MOTOR_PWM_CLKDIV.

◆ motor_stop()

void motor_stop ( void  )

A function to stop the car.

This function works by setting the duty cycle of both motors to 0.

References MOTOR_PWM_PIN_LEFT, MOTOR_PWM_PIN_RIGHT, MOTOR_PWM_WRAP, and motor_update_pwm().

Here is the call graph for this function:

◆ motor_turn_left()

void motor_turn_left ( bool  reverse_turn)

A function to turn the car left.

This function turns the car left by setting the duty cycle of the left motor to be lower than the right motor, which enables the car to turn left. In addition, the left motor is set to rotate in the opposite direction of the right motor.

Parameters
[in]reverse_turnA boolean value to indicate that the car is turning in reverse.

References MOTOR_LEFT_PIN_ANTICLKWISE, MOTOR_LEFT_PIN_CLKWISE, MOTOR_PWM_PIN_LEFT, MOTOR_PWM_PIN_RIGHT, MOTOR_PWM_WRAP, MOTOR_RIGHT_PIN_ANTICLKWISE, MOTOR_RIGHT_PIN_CLKWISE, motor_update_direction(), and motor_update_pwm().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ motor_turn_right()

void motor_turn_right ( bool  reverse_turn)

A function to turn the car right.

This function turns the car right by setting the duty cycle of the right motor to be lower than the left motor, which enables the car to turn left. In addition, the right motor is set to rotate in the opposite direction of the right motor.

Parameters
reverse_turnA boolean value to indicate that the car is turning in reverse.

References MOTOR_LEFT_PIN_ANTICLKWISE, MOTOR_LEFT_PIN_CLKWISE, MOTOR_PWM_PIN_LEFT, MOTOR_PWM_PIN_RIGHT, MOTOR_PWM_WRAP, MOTOR_RIGHT_PIN_ANTICLKWISE, MOTOR_RIGHT_PIN_CLKWISE, motor_update_direction(), and motor_update_pwm().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ motor_update_direction()

void motor_update_direction ( uint  clkwise_gpio,
uint  anti_clkwise_gpio,
uint  clkwise,
uint  anti_clkwise 
)

A function to update the direction of the motor's rotation.

Parameters
[in]clkwise_gpioThe GPIO pin number for clockwise rotation.
[in]anti_clkwise_gpioThe GPIO pin number for anticlockwise rotation.
[in]clkwiseThe digital value to set for clockwise rotation.
[in]anti_clkwiseThe digital value to set for anticlockwise rotation.
Here is the caller graph for this function:

◆ motor_update_pwm()

void motor_update_pwm ( uint  pwm_gpio,
uint  pwm_wrap,
float  duty_cycle 
)

A function to update the PWM duty cycle and wrap value.

This function updates the PWM duty cycle and wrap value of a PWM slice.
This is used to update / modify the speed of the motor's rotation.
Parameters
[in]pwm_gpioThe GPIO pin number for the PWM output.
[in]pwm_wrapThe wrap value of the PWM slice.
[in]duty_cycleThe duty cycle of the PWM slice.
Here is the caller graph for this function:

◆ motor_update_ratio()

void motor_update_ratio ( float  new_ratio)

Update the ratio of the left motor's duty cycle to the right motor's duty cycle.

Parameters
[in]new_ratioNew ratio of the left motor's duty cycle to the right motor's duty cycle.

References g_ratio.

Here is the caller graph for this function: