|
| 1 | +# Servo Library |
| 2 | + |
| 3 | +This library can control a great number of servos. It makes careful use |
| 4 | +of timers: the library can control 12 servos using only 1 timer. |
| 5 | +Derived from the Arduino Servo library v1.8.0. |
| 6 | + |
| 7 | + |
| 8 | +This library has a slightly diffent user interface than the usual singleton |
| 9 | +libraries. This allows for handling more than one servo per Sketch but it |
| 10 | +requires some more attention when porting an existing sketch from C++ to C. |
| 11 | + |
| 12 | +Each Servo instance is identified by a channel-ID. In order to match the C++ |
| 13 | +class constructor syntax it is defined as a type `Servo`. |
| 14 | + |
| 15 | +This channel-ID is used as a file-descriptor-like value and need to be |
| 16 | +passed to all API functions except for `Servo_attach()`. This function |
| 17 | +claimes the next free channel number. |
| 18 | + |
| 19 | +## Example |
| 20 | + |
| 21 | +Read a potentiometer on analog input 0 and set a servo pulse length between |
| 22 | +1000us and 2023us: |
| 23 | +```c |
| 24 | +#include <Arduino.h> |
| 25 | +#include <Servo.h> |
| 26 | + |
| 27 | +Servo myservo; // just a simple unsigned char to hold the channel-ID |
| 28 | + |
| 29 | +int val; // variable to read the value from the analog pin |
| 30 | + |
| 31 | +void setup() { |
| 32 | + myservo = Servo_attach(9); // attaches the servo on pin 9, returns channel-ID |
| 33 | +} |
| 34 | + |
| 35 | +void loop() { |
| 36 | + val = analogRead(0); // reads the value of the potentiometer |
| 37 | + Servo_write(myservo, val+1000);// sets the servo position |
| 38 | + delay(15); |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | + |
| 43 | +Original Arduino C++-Sytax: |
| 44 | +```c |
| 45 | +#include <Servo.h> |
| 46 | + |
| 47 | +Servo myservo; // create servo object to control a servo |
| 48 | + |
| 49 | +int val; // variable to read the value from the analog pin |
| 50 | + |
| 51 | +void setup() { |
| 52 | + myservo.attach(9); // attaches the servo on pin 9 to the servo object |
| 53 | +} |
| 54 | + |
| 55 | +void loop() { |
| 56 | + val = analogRead(0); // reads the value of the potentiometer |
| 57 | + myservo.write(val+1000); // sets the servo position |
| 58 | + delay(15); |
| 59 | +} |
| 60 | + |
| 61 | +``` |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | +## API |
| 66 | + |
| 67 | + |
| 68 | +data type `Servo`: A type definition for a simple unsigned char to hold the |
| 69 | +channel number returned my `Servo_attach()`. Needed for every servo. Syntax |
| 70 | +identical to the Arduino class constructor. |
| 71 | + |
| 72 | +Arduino syntax |sduino syntax |
| 73 | +-------------------- |--------------------- |
| 74 | +`Servo myservo;` |`Servo myservo;` |
| 75 | +`myservo.attach(pin);` |`myservo = Servo_attach(pin);` |
| 76 | +`myservo.attach(pin,min,max);` |`Servo_attach_minmax(pin,min,max);` |
| 77 | +`myservo.detach();` |`Servo_detach(byte channel);` |
| 78 | +`myservo.write(val);` |`Servo_write(myservo, val);` |
| 79 | +`myservo.writeMicroseconds(val);`|`Servo_writeMicroseconds(myservo, val);` |
| 80 | +`val = myservo.read();` |`val = Servo_read(myservo);` |
| 81 | +`val = myservo.readMicroseconds();`|`val = Servo_readMicroseconds(myservo);` |
| 82 | +`myservo.attached()` |`Servo_attached(myservo);` |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +`uint8_t Servo_attach(int pin);` |
| 87 | +attach the given pin to the next free channel, sets pinMode, returns channel |
| 88 | +number or 0 if failure. |
| 89 | + |
| 90 | +`uint8_t Servo_attach_minmax(int pin, int min, int max);` |
| 91 | +as above but also sets min and max values for writes. |
| 92 | + |
| 93 | +`void Servo_detach(byte channel);` |
| 94 | + |
| 95 | +`void Servo_write(byte channel, int value);` if value is < 200 it is treated |
| 96 | +as an angle and scaled according the minimum and maximum pulsewidth defined |
| 97 | +using the attach() function earlier, otherwise as pulse width in |
| 98 | +microseconds, unscaled. |
| 99 | + |
| 100 | +`void Servo_writeMicroseconds(byte channel, int value);` |
| 101 | +Write pulse width in microseconds, unscaled. |
| 102 | + |
| 103 | +`int Servo_read(byte channel);` |
| 104 | +returns current pulse width as an angle between 0 and 180 degrees. |
| 105 | + |
| 106 | +`int Servo_readMicroseconds(byte channel);` |
| 107 | +returns current pulse width in microseconds for this servo (was read_us() in |
| 108 | +first Arduino release). |
| 109 | + |
| 110 | +`bool Servo_attached(byte channel);` |
| 111 | +return true if this servo is attached, otherwise false . |
| 112 | + |
| 113 | + |
| 114 | +## Relationship between PWM/analog output and Servo output |
| 115 | + |
| 116 | +It is not possible to use a timer for PWM and the Servo Library at the same |
| 117 | +time. Since this library currently uses timer1, the PWM function |
| 118 | +(`analogWrite()`) is disabled for the pins connected to timer1 (for the |
| 119 | +STM8S103 this is pin PC3 and PC4 or digital pin 5 and 6). |
| 120 | + |
| 121 | +Pins connected to timer2 (PA3, PD3, PD4, digital pin 2,12,13) are still |
| 122 | +usable for PWM output. |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | +## Possible improvements |
| 128 | + |
| 129 | + |
| 130 | +### A more sophisticated pseudo-OO API |
| 131 | + |
| 132 | +Define a set of preprocessor macros that more OO-like definitions like this |
| 133 | +become possible: |
| 134 | + |
| 135 | +Current syntax |OO-like syntax |Arduino syntax |
| 136 | +-------------------- |--------------------- |-------------- |
| 137 | +myservo = Servo_attach(pin); |myservo_attach(pin); |myservo.attach(pin); |
| 138 | +Servo_write(myservo, val); |myservo_write(val); |myservo_write(val); |
| 139 | +val = Servo_read(myservo); |val = myservo_read(); |val = myservo_read(); |
| 140 | + |
| 141 | + |
| 142 | +### Optimizing the handle_interrupts() function |
| 143 | + |
| 144 | +SDCC compiles this function very inefficiently. The code size is around 500 |
| 145 | +Bytes, and it is executed as part of the CC interrupt routine. Expected CPU |
| 146 | +load for a full servo group of 12 servos is approx. 2%. |
| 147 | + |
| 148 | +(Calculated for 16MHz CPU clock, 13 interrupts every 20ms = 650 |
| 149 | +interrupts/sec, approx. 500 clock cycles each) |
| 150 | + |
| 151 | + |
| 152 | +### Using more than one CC channel per timer |
| 153 | + |
| 154 | +it might be possible to use all capture+compare (CC) channels of one timer |
| 155 | +at the same time, attaching one servo group to each CC-channel. This way it |
| 156 | +would be possible to serve up to 48 servos using the four CC-channels of |
| 157 | +timer TIM1. Monitoring the repetion period might become a little complex, as |
| 158 | +it must be ensured that all servos on all channels have finshed before. |
| 159 | + |
| 160 | + |
0 commit comments