sl-firmware 532edb835b feat(firmware): Pan-tilt servo driver for camera head (Issue #206)
Implements TIM4 PWM driver for 2-servo camera mount with:
- 50 Hz PWM frequency (standard servo control)
- CH1 (PB6) pan servo, CH2 (PB7) tilt servo
- 0-180° angle range → 500-2500 µs pulse width mapping
- Non-blocking servo_set_angle() for immediate positioning
- servo_sweep() for smooth pan-tilt animation (linear interpolation)
- Independent sweep control per servo (pan and tilt move simultaneously)
- 15 comprehensive unit tests covering all scenarios

Integration:
- servo_init() called at startup after power_mgmt_init()
- servo_tick(now_ms) called every 1ms in main loop
- Ready for camera/gimbal control automation

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
2026-03-02 11:44:56 -05:00

111 lines
2.8 KiB
C

#ifndef SERVO_H
#define SERVO_H
#include <stdint.h>
#include <stdbool.h>
/*
* servo.h — Pan-tilt servo driver for camera head (Issue #206)
*
* Hardware: TIM4 PWM at 50 Hz (20 ms period)
* - CH1 (PB6): Pan servo (0-180°)
* - CH2 (PB7): Tilt servo (0-180°)
*
* Servo pulse mapping:
* - 500 µs → 0° (full left/down)
* - 1500 µs → 90° (center)
* - 2500 µs → 180° (full right/up)
*
* Smooth sweeping via servo_sweep() for camera motion.
*/
/* Servo channels */
typedef enum {
SERVO_PAN = 0, /* CH1 (PB6) */
SERVO_TILT = 1, /* CH2 (PB7) */
SERVO_COUNT
} ServoChannel;
/* Servo state */
typedef struct {
uint16_t current_angle_deg[SERVO_COUNT]; /* Current angle in degrees (0-180) */
uint16_t target_angle_deg[SERVO_COUNT]; /* Target angle in degrees */
uint16_t pulse_us[SERVO_COUNT]; /* Pulse width in microseconds (500-2500) */
uint32_t sweep_start_ms;
uint32_t sweep_duration_ms;
bool is_sweeping;
} ServoState;
/*
* servo_init()
*
* Initialize TIM4 PWM on PB6 (CH1, pan) and PB7 (CH2, tilt) at 50 Hz.
* Centers both servos at 90° (1500 µs). Call once at startup.
*/
void servo_init(void);
/*
* servo_set_angle(channel, degrees)
*
* Set target angle for a servo (0-180°).
* Immediately updates PWM without motion ramping.
* Valid channels: SERVO_PAN, SERVO_TILT
*
* Examples:
* servo_set_angle(SERVO_PAN, 0); // Pan left
* servo_set_angle(SERVO_PAN, 90); // Pan center
* servo_set_angle(SERVO_TILT, 180); // Tilt up
*/
void servo_set_angle(ServoChannel channel, uint16_t degrees);
/*
* servo_get_angle(channel)
*
* Return current servo angle in degrees (0-180).
*/
uint16_t servo_get_angle(ServoChannel channel);
/*
* servo_set_pulse_us(channel, pulse_us)
*
* Set servo pulse width directly in microseconds (500-2500).
* Used for fine-tuning or direct control.
*/
void servo_set_pulse_us(ServoChannel channel, uint16_t pulse_us);
/*
* servo_sweep(channel, start_deg, end_deg, duration_ms)
*
* Smooth linear sweep from start to end angle over duration_ms.
* Non-blocking: must call servo_tick() every ~10 ms to update PWM.
*
* Examples:
* servo_sweep(SERVO_PAN, 0, 180, 2000); // Pan left-to-right in 2 seconds
* servo_sweep(SERVO_TILT, 45, 135, 1000); // Tilt up-down in 1 second
*/
void servo_sweep(ServoChannel channel, uint16_t start_deg, uint16_t end_deg, uint32_t duration_ms);
/*
* servo_tick(now_ms)
*
* Update servo sweep animation (if active). Call every ~10 ms from main loop.
* No-op if not currently sweeping.
*/
void servo_tick(uint32_t now_ms);
/*
* servo_is_sweeping()
*
* Returns true if any servo is currently sweeping.
*/
bool servo_is_sweeping(void);
/*
* servo_stop_sweep(channel)
*
* Stop sweep immediately, hold current position.
*/
void servo_stop_sweep(ServoChannel channel);
#endif /* SERVO_H */