Drivers description and usage

1. Hbridge / ESC

HbridgeSpeed()

This function sets both the speed and direction of the car's motors. It takes two inputs: speed1 and speed2, one for each motor. Each value can range from -100 to +100. The sign determines the direction the motor spins, where positive values make the motor spin forward,while negative values make it spins backwards. The higher the number the faster the motor spins.

HbridgeBrake()

This function stops the motors by setting their PWM duty cycle to 0%, which means no power is sent to them. It also sets the direction control pins to a safe state, ensuring the motors remain stopped.

2. Servo

Steer()

This function tells the steering servo which way to turn. It takes an angle value between -100 and +100, where -100 means full turn in one direction, +100 means full turn in the opposite direction and 0 keeps the wheels straight.

Based on this value, it calculates the PWM duty cycle needed to position the servo as requested. Then it reads the current timer period (periodTicks) and uses it in order to calculate the number of timer ticks (pulseTicks) that will produce the correct duty cycle.

In the end, it updates the timer's register with this value so the servo received the new command.

3. Pixy Camera

The Pixy camera communicates with the board using the I2C protocol. When the board needs information from the camera, such as detected lines, the process is:

  1. Build a request packet: a sequence of bytes starting with a special header, followed by a command number, the message length and any extra data.

  2. Send the packet to the Pixy.

  3. Receive the response packet from the Pixy.

  4. Interpret the received data (eg. converting a series of bytes into coordinates for the detected lines).

In this communication, DMA (Direct Memory Access) acts as a helper inside the microcontroller that transfers data between peripherals and memory without the CPU handling each byte. This allows the CPU to perform other tasks while the transfer is in progress, making communication faster and more efficient.

4. Main function

Here the main logic of the car is implemented.

Steps:

1. Start-up and setup

The main components, such as the motors, steering servo and Pixy camera, are initialized. Then the Pixy camera's LED is turned to red to show it is ready.

2. Main loop

The main loop reads vectors from the Pixy camera, calculates a steering angle from their slopes, applies proportional scaling and limits, adjusts the servo, and controls motor speed.

Last updated

Was this helpful?