Create Steering authored by Adham Beshr's avatar Adham Beshr
# 🏎️ **Steering System – Servo Motor Control**
![Steering System](INSERT_IMAGE_LOCATION_HERE)
## 📌 **Overview**
The **steering system** of our autonomous car is controlled using a **servo motor**. The servo adjusts the front wheels' angle by modifying its **PWM (Pulse Width Modulation) duty cycle**, allowing for **precise directional control**.
This page explains:
✔ How the servo motor is used for steering.
✔ The implementation using **FTM (FlexTimer Module)** for PWM control.
✔ Code snippets for setting steering angles.
---
## ⚙️ **How the Servo Motor Works**
A **servo motor** operates by receiving a **PWM signal**, where:
- A **1ms pulse width (~5% duty cycle)** steers **left**.
- A **1.5ms pulse width (~7.5% duty cycle)** keeps the wheels **centered**.
- A **2ms pulse width (~10% duty cycle)** steers **right**.
### **PWM Signal Representation**
📌 _These signals are generated using **FTM3 Channel 6**._
![PWM Signal Explanation](INSERT_IMAGE_LOCATION_HERE)
---
## 🔩 **Implementation – Controlling the Servo Motor**
The **FlexTimer Module (FTM)** is used to generate the required PWM signals. Below is an overview of how the servo control is implemented.
### **Pin Configuration**
| **Peripheral** | **Pin** | **Function** |
|--------------|--------|------------|
| **Servo Motor** | PTB12 | FTM3_CH6 (PWM) |
---
## 🛠️ **Code Implementation**
### **1️⃣ Setting the Servo Position**
The function `setServoPosition()` updates the **PWM duty cycle** to control the steering.
```cpp
// Function to set the servo position based on the desired duty cycle
void setServoPosition(float duty_cycle) {
// Update PWM duty cycle for SERVO0 (FTM3 CH6)
FTM_UpdatePwmDutycycle(FTM3_PERIPHERAL, FTM3_FTM_SERVO_CHANNEL, kFTM_EdgeAlignedPwm, duty_cycle);
FTM_SetSoftwareTrigger(FTM3_PERIPHERAL, true); // Apply the new duty cycle
}
// Function to turn the car left
void turnLeft() {
float duty_cycle = 5.0; // 1ms pulse width -> 5% duty cycle
setServoPosition(duty_cycle);
}
// Function to turn the car right
void turnRight() {
float duty_cycle = 10.0; // 2ms pulse width -> 10% duty cycle
setServoPosition(duty_cycle);
}
// Function to center the servo (straight position)
void centerServo() {
float duty_cycle = 7.5; // 1.5ms pulse width -> 7.5% duty cycle
setServoPosition(duty_cycle);
}