NXPCUP-libary
Library for car's control board on NXPCUP competition based on the Mbed framework.
Servo.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "mbed.h"
4 
5 #include "util.h"
6 
7 namespace nxpcup {
8 
9 class Servo {
10 public:
11  struct Config {
12  PinName pin;
13  int8_t correctionAngle = 0;
14  uint8_t servoMinMaxAngle = 90;
15  int8_t defaultCenterAngle = 0;
16  bool isReverseSignal = false;
20  uint32_t minUs = 900;
21  uint32_t maxUs = 2100;
23  static constexpr uint16_t PERIOD_US = 20000; // 50 Hz
24  static constexpr uint16_t CENTER_US = 1500;
25  };
26 
32  Servo(Config config)
33  : Servo(config.pin, config.minUs, config.maxUs)
34  {
35  m_isReverseSignal = config.isReverseSignal;
39  }
40 
48  Servo(PinName pin, uint16_t minUs = 1000, uint16_t maxUs = 2000)
49  : m_minUs(minUs)
50  , m_maxUs(maxUs)
51  {
52 #if defined SERVO_HARDWARE_PWM
53  servo = new PwmOut(pin);
54 #elif defined SERVO_SOFTWARE_PWM
55  servo = new SoftPWM(pin);
56 #endif
57 
58  servo->period_us(Config::PERIOD_US);
59  servo->pulsewidth_us(Config::CENTER_US);
60  }
61 
67  void setAngleCorrection(int8_t angle) { m_correctionAngle = angle; }
68 
75  void setAngleMinMax(uint8_t min, uint8_t max)
76  {
77  m_minAngle = min + m_correctionAngle;
78  m_maxAngle = max + m_correctionAngle;
79  }
80 
87  void setAngleMinMaxCenter(uint8_t diffAngle)
88  {
89  m_minAngle = 90 - diffAngle + m_correctionAngle;
90  m_maxAngle = 90 + diffAngle + m_correctionAngle;
91  }
92 
98  void setAngle(uint8_t degree)
99  {
100  m_lastAngle = nxpcup::clamp<uint8_t>(degree + m_correctionAngle, m_minAngle, m_maxAngle);
101  uint16_t positionInMicrosecond = 0;
102  if (m_isReverseSignal) {
103  positionInMicrosecond = m_maxUs - (m_lastAngle * usDiff) / 180; // 180 degree = max range
104  } else {
105  positionInMicrosecond = m_minUs + (m_lastAngle * usDiff) / 180; // 180 degree = max range
106  }
107  setMicrosecond(positionInMicrosecond);
108  }
109 
116  void setAngleCenter(int8_t degree, bool reverse = false)
117  {
118  setAngle(90 + ((reverse ? -1 : 1) * degree));
119  }
120 
124  uint8_t angle() const { return m_lastAngle; }
125 
129  int8_t centerAngle() const { return angle() - 90; }
130 
136  int getMinAngle() const { return m_minAngle; }
137 
143  int getMaxAngle() const { return m_maxAngle; }
144 
145 private:
149  void setMicrosecond(uint16_t microsecond)
150  {
151  servo->pulsewidth_us(microsecond);
152  }
153 
154 #if defined SERVO_HARDWARE_PWM
155  PwmOut* servo;
156 #elif defined SERVO_SOFTWARE_PWM
157  SoftPWM* servo;
158 #endif
159 
160  uint8_t m_minAngle = 0;
161  uint8_t m_maxAngle = 180;
162  int8_t m_correctionAngle = 0;
163  uint8_t m_lastAngle = 0;
164  bool m_isReverseSignal = false;
165 
166  const uint32_t m_minUs;
167  const uint32_t m_maxUs;
168  const uint32_t usDiff = m_maxUs - m_minUs;
169 };
170 
171 } // namespace nxpcup
static constexpr uint16_t CENTER_US
Definition: Servo.h:24
uint8_t angle() const
Definition: Servo.h:124
PinName pin
Definition: Servo.h:12
bool isReverseSignal
Definition: Servo.h:16
int8_t correctionAngle
Definition: Servo.h:13
Definition: BorderDetector.h:6
uint32_t minUs
Definition: Servo.h:20
Definition: SoftPWM.h:24
Definition: Servo.h:9
int getMaxAngle() const
Definition: Servo.h:143
uint8_t servoMinMaxAngle
Definition: Servo.h:14
Definition: Servo.h:11
Servo(PinName pin, uint16_t minUs=1000, uint16_t maxUs=2000)
Definition: Servo.h:48
uint32_t maxUs
Definition: Servo.h:21
void setAngleCenter(int8_t degree, bool reverse=false)
Definition: Servo.h:116
void setAngleMinMax(uint8_t min, uint8_t max)
Definition: Servo.h:75
void setAngleMinMaxCenter(uint8_t diffAngle)
Definition: Servo.h:87
int8_t defaultCenterAngle
Definition: Servo.h:15
void setAngleCorrection(int8_t angle)
Definition: Servo.h:67
static constexpr uint16_t PERIOD_US
Definition: Servo.h:23
int8_t centerAngle() const
Definition: Servo.h:129
Servo(Config config)
Definition: Servo.h:32
void setAngle(uint8_t degree)
Definition: Servo.h:98
int getMinAngle() const
Definition: Servo.h:136