change motion from analogWrite to ledc, add fluent interface for motors

This commit is contained in:
hhaupt
2024-05-12 09:56:42 +02:00
parent c8f7e95363
commit 60314ec101
3 changed files with 92 additions and 24 deletions

View File

@@ -1,8 +1,8 @@
/**
* @file Motion.h
* @author Jonathan Schulze, Nick Hübenthal
* @author Jonathan Schulze, Nick Hübenthal, Hans Haupt
* @brief This component controls the ability to rotate and change position.
* @version 0.1
* @version 0.2
* @date 2023-12-13
*
* @copyright Copyright (c) 2023
@@ -15,11 +15,31 @@
#include <Arduino.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include "driver/ledc.h"
#define LEDC_MODE LEDC_LOW_SPEED_MODE
#define TIMER LEDC_TIMER_2
#define CHANNEL_LEFT LEDC_CHANNEL_3
#define CHANNEL_RIGHT LEDC_CHANNEL_4
#define DUTY_RES LEDC_TIMER_13_BIT // Set duty resolution to 13 bits
#define FREQUENCY (5000) // Frequency in Hertz. Set frequency at 5 kHz
class Motor{
public:
Motor(uint8_t pin, ledc_timer_t timer, ledc_channel_t channel);
void begin(void);
void setSpeed(uint16_t duty);
uint16_t getSpeed(void);
protected:
uint8_t pin;
ledc_timer_t timer;
ledc_channel_t channel;
uint16_t duty;
};
class Motion{
protected:
static const uint8_t RIGHT_MOTOR_DUTY = 128;
static const uint8_t LEFT_MOTOR_DUTY = 128;
static const uint16_t RIGHT_MOTOR_DUTY = 4096;
static const uint16_t LEFT_MOTOR_DUTY = 4096;
static const int MOTOR_RIGHT_PIN = 11;
static const int MOTOR_LEFT_PIN = 12;
static void moveTask(void * args);
@@ -27,6 +47,10 @@ protected:
static void rightMotorTask(void * args);
public:
//Shared Timer to sync movement
static inline Motor left = Motor(MOTOR_LEFT_PIN,TIMER,CHANNEL_LEFT);
static inline Motor right = Motor(MOTOR_RIGHT_PIN,TIMER,CHANNEL_RIGHT);
/**
* @brief Initialize the movement component.
*
@@ -61,4 +85,6 @@ public:
static void stop(void);
};
#endif //Motion_h