Pico-Arduino
PicoPWMServo.h
1 #pragma once
2 
3 #include "PicoPWM.h"
4 
5 // the shortest pulse sent to a servo
6 #ifndef MIN_PULSE_WIDTH
7 #define MIN_PULSE_WIDTH 544
8 #endif
9 // the longest pulse sent to a servo
10 #ifndef MAX_PULSE_WIDTH
11 #define MAX_PULSE_WIDTH 2400
12 #endif
13 // default pulse width when servo is attached
14 #ifndef DEFAULT_PULSE_WIDTH
15 #define DEFAULT_PULSE_WIDTH 1500
16 #endif
17 // minumim time to refresh servos in microseconds
18 #ifndef REFRESH_INTERVAL
19 #define REFRESH_INTERVAL 20000
20 #endif
21 // minimum in degrees
22 #define MIN_DEGREES 0
23 // maximum in degrees
24 #define MAX_DEGREES 180l
25 
26 namespace pico_arduino {
27 
34 class Servo {
35 public:
36  Servo() {
37  }
38 
39  ~Servo(){
40  if (pwm!=nullptr) delete pwm;
41  }
42 
44  void attach(int pin) {
45  if (pwm==nullptr){
46  pwm = new PicoPWMNano(REFRESH_INTERVAL * 1000);
47  }
48  if (pwm!=nullptr)
49  pwm->begin(pin);
50  this->is_attached = true;
51  this->pin = pin;
52  }
53 
55  void attach(int pin, int min, int max) {
56  this->min = min;
57  this->max = max;
58  attach(pin);
59  }
60 
62  void detach() {
63  is_attached = false;
64  if (pwm!=nullptr)
65  pwm->end(pin);
66  }
67 
69  void write(int value) {
70  int pulse_width_ms = value;
71  if (value<200){
72  pulse_width_ms = map(value,MIN_DEGREES, MAX_DEGREES, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
73  }
74  writeMicroseconds(pulse_width_ms);
75  }
76 
78  void writeMicroseconds(int value) {
79  pwm_value = value;
80  if (pwm!=nullptr) {
81  // convert value to nanoseconds
82  pwm->setDutyCycle(pin, value * 1000);
83  }
84  }
85 
87  int read() {
88  return map(pwm_value, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH, MIN_DEGREES,MAX_DEGREES);
89  }
90 
93  return pwm_value;
94  }
95 
97  bool attached() {
98  return is_attached;
99  }
100 
101 private:
102  PicoPWMNano *pwm;
103  int8_t min = MIN_DEGREES;
104  int8_t max = MAX_DEGREES;
105  bool is_attached = false;
106  int pwm_value;
107  int pin;
108 
109 };
110 
111 }
Basic PWM API based on the input and output in nano seconds The Raspberry Pico has 8 controllable PWM...
Definition: PicoPWM.h:362
void begin(pin_size_t gpio, uint64_t initialDutyCyleNanoSeconds=0, PinMode pinMode=OUTPUT)
setup the pin mode only if necessary
Definition: PicoPWM.h:378
void end(pin_size_t gpio)
sets the output pins to low
Definition: PicoPWM.h:392
void setDutyCycle(pin_size_t gpio, uint64_t dutyCyleNanoSeconds)
Defines the active period is nanoseconds.
Definition: PicoPWM.h:398
We provide an alternative Pico implementation for the Servo class which is compatible with the Arduin...
Definition: PicoPWMServo.h:34
int read()
returns current pulse width as an angle between 0 and 180 degrees
Definition: PicoPWMServo.h:87
void detach()
stops the generation of signals
Definition: PicoPWMServo.h:62
void write(int value)
if value is < 200 its treated as an angle, otherwise as pulse width in microseconds
Definition: PicoPWMServo.h:69
bool attached()
return true if this servo is attached, otherwise false
Definition: PicoPWMServo.h:97
void attach(int pin)
attach the given pin to the next free channel, sets pinMode, returns channel number or 0 if failure
Definition: PicoPWMServo.h:44
void attach(int pin, int min, int max)
as above but also sets min and max values for writes.
Definition: PicoPWMServo.h:55
void writeMicroseconds(int value)
Write pulse width in microseconds.
Definition: PicoPWMServo.h:78
int readMicroseconds()
returns current pulse width in microseconds for this servo (was read_us() in first release)
Definition: PicoPWMServo.h:92
Pico Arduino Framework.
Definition: Arduino.cpp:26