Skip to content

Commit

Permalink
Renamed to ServoLibrary.
Browse files Browse the repository at this point in the history
  • Loading branch information
ATrappmann committed Mar 10, 2021
1 parent e24fdc1 commit 7660705
Show file tree
Hide file tree
Showing 13 changed files with 516 additions and 135 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# ServoPCA9685
# TrappmannRobotics-ServoLibrary

The library **ServoPCA9685** implements an interface given by the original
[Servo Library for Arduino](https://github.com/arduino-libraries/Servo)
to the implementation of the
The **TrappmannRobotics-ServoLibrary** defines a **GenericServo** interface which is given by the original
[Servo Library for Arduino](https://github.com/arduino-libraries/Servo) and implements different types
of Servo actuation. The class **ServoPWM** is fully compatible to the original **Servo** class but has
some additional functions. The class **ServoPCA9685** implements a **Servo** which is driven by the
[Adafruit-PWM-Servo-Driver-library](https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library)
which allows us to control 16 Servos via an I2C-bus controlled PCA9685-board
as it is sold from Adafruit [here](https://www.adafruit.com/product/815).

## Copyright
**ServoPCA9685** is written by Andreas Trappmann from
**TrappmannRobotics-ServoLibrary** is written by Andreas Trappmann from
[Trappmann-Robotics.de](https://www.trappmann-robotics.de/). It
is published under the MIT license, check LICENSE for more information.
All text above must be included in any redistribution.
Expand Down
6 changes: 2 additions & 4 deletions examples/Knob/Knob.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#include <ServoPCA9685.h>

Servo myservo; // create servo object to control a servo
ServoPCA9685 myservo; // create servo object to control a servo

int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
Expand All @@ -20,8 +20,6 @@ void setup() {

void loop() {
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
myservo.writeAnalog(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}

32 changes: 0 additions & 32 deletions examples/Sweep/Sweep.ino

This file was deleted.

36 changes: 36 additions & 0 deletions examples/SweepPCA9685Servo/SweepPCA9685Servo.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// NAME: SweepPCA9685Servo.ino
//
#include "ServoPCA9685.h"

ServoPCA9685 myservo;

void setup() {
Serial.begin(9600);
while (!Serial); // wait for Leonardo

myservo.attach(9);

Serial.print("MIN_PULSE_WIDTH: "); Serial.println(MIN_PULSE_WIDTH);
myservo.writeMicroseconds(MIN_PULSE_WIDTH);
delay(5000);

Serial.print("DEFAULT_PULSE_WIDTH: "); Serial.println(DEFAULT_PULSE_WIDTH);
myservo.writeMicroseconds(DEFAULT_PULSE_WIDTH);
delay(5000);

Serial.print("MAX_PULSE_WIDTH: "); Serial.println(MAX_PULSE_WIDTH);
myservo.writeMicroseconds(MAX_PULSE_WIDTH);
delay(5000);
}

void loop() {
for (int pos=0; pos<=180; pos++) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (int pos=MAX_PULSE_WIDTH; pos>=MIN_PULSE_WIDTH; pos--) { // goes from 180 degrees to 0 degrees
myservo.writeMicroseconds(pos); // tell servo to go to position in variable 'pos'
// delay(15); // waits 15ms for the servo to reach the position
}
}
35 changes: 35 additions & 0 deletions examples/SweepPWMServo/SweepPWMServo.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// NAME: SweepPWMServo.ino
//
#include "ServoPWM.h"

ServoPWM myservo;

void setup() {
Serial.begin(9600);
while (!Serial); // wait for Leonardo

myservo.attach(9);

Serial.print("MIN_PULSE_WIDTH: "); Serial.println(MIN_PULSE_WIDTH);
myservo.writeMicroseconds(MIN_PULSE_WIDTH);
delay(5000);

Serial.print("DEFAULT_PULSE_WIDTH: "); Serial.println(DEFAULT_PULSE_WIDTH);
myservo.writeMicroseconds(DEFAULT_PULSE_WIDTH);
delay(5000);

Serial.print("MAX_PULSE_WIDTH: "); Serial.println(MAX_PULSE_WIDTH);
myservo.writeMicroseconds(MAX_PULSE_WIDTH);
delay(5000);
}

void loop() {
for (int pos=0; pos<=180; pos++) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (int pos=MAX_PULSE_WIDTH; pos>=MIN_PULSE_WIDTH; pos--) { // goes from 180 degrees to 0 degrees
myservo.writeMicroseconds(pos); // tell servo to go to position in variable 'pos'
}
}
44 changes: 44 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#######################################
# Syntax Coloring Map For TrappmannRobotics
#######################################
# Class
#######################################

GenericServo KEYWORD1
ServoPCA9685 KEYWORD1
ServoPWM KEYWORD1

#######################################
# Methods and Functions
#######################################

servo KEYWORD2
pin KEYWORD2
currentPulse KEYWORD2
minPulse KEYWORD2
maxPulse KEYWORD2
lowerLimit KEYWORD2
upperLimit KEYWORD2

attach KEYWORD2
detach KEYWORD2
write KEYWORD2
writeAnalog KEYWORD2
writeMicroseconds KEYWORD2
read KEYWORD2
readMicroseconds KEYWORD2
attached KEYWORD2
limit KEYWORD2
getLowerLimit KEYWORD2
getUpperLimit KEYWORD2

#######################################
# Constants
#######################################

DEFAULT_PULSE_WIDTH LITERAL1
PULSE_RANGE LITERAL1
MIN_PULSE_WIDTH LITERAL1
MAX_PULSE_WIDTH LITERAL1
INVALID_SERVO LITERAL1
SERVO_FREQ LITERAL1
10 changes: 5 additions & 5 deletions library.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"name": "ServoPCA9685",
"name": "TrappmannRobotics-ServoLibrary",
"frameworks": "Arduino",
"keywords": "Servo PCA9685",
"description": "Arduino library for PCA9685-boards which is compatible to the original Servo library.",
"keywords": "TrappmannRobotics GenericServo ServoPWM ServoPCA9685 PCA9685 Servo",
"description": "Arduino library for different types of Servo actuation throught a GenericServo interface which is compatible to the original Servo library.",
"authors":
[
{
"name": "Andreas Trappmann",
"email": "[email protected]",
"url": "https://github.com/ATrappmann/ServoPCA9685",
"url": "https://github.com/ATrappmann/TrappmannRobotics-ServoLibrary",
"maintainer": true
}
],
"repository":
{
"type": "git",
"url": "https://github.com/ATrappmann/ServoPCA9685"
"url": "https://github.com/ATrappmann/TrappmannRobotics-ServoLibrary"
}
}
8 changes: 4 additions & 4 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=ServoPCA9685
name=TrappmannRobotics-ServoLibrary
version=1.0.0
author=Andreas Trappmann
maintainer=Andreas Trappmann <[email protected]>
sentence=Arduino library for PCA9685 boards which is compatible to the original Servo library
paragraph=Arduino library for PCA9685 boards which is compatible to the original Servo library
sentence=Arduino library for different types of Servo actuation throught a GenericServo interface which is compatible to the original Servo library.
paragraph=Arduino library for different types of Servo actuation throught a GenericServo interface which is compatible to the original Servo library.
category=*
url=https://github.com/ATrappmann/ServoPCA9685
url=https://github.com/ATrappmann/TrappmannRobotics-ServoLibrary
architectures=*
122 changes: 122 additions & 0 deletions src/GenericServo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* NAME: GenericServo.h
*
* DESC: Abstract base class for managing servos with different technical
* intrerfaces. The interface is basically compatible to the original
* Servo library from Arduino but does some cleanup and also adds
* advanced features.
*
* TrappmannRobotics developed the following Servo implementations so far:
* - ServoPWM, for Servos controlled by a PWM pin on an Arduino.
* - ServoPCA9685, for Servos controlled via a PWM channel on an I2C-bus controlled PCA9685-board.
*
* SOURCE: Code is available at https://github.com/ATrappmann/ServoPCA9685
*
* MIT License
*
* Copyright (c) 2021 Andreas Trappmann
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef GENERIC_SERVO_H
#define GENERIC_SERVO_H

#include <inttypes.h>

class GenericServo {
public:
/*
* attach the given pin/channel for controlling the servo and power it up.
* @param pin: pin or channel number the servo is controlled with.
* @return servoId on success, INVALID_SERVO (255) in case of error.
*/
virtual uint8_t attach(const uint8_t pin) = 0;

/*
* as above but also sets min and max values for the PWM pulse width in microseconds.
* @param pin: pin or channel number the servo is controlled with.
* @param minPulse: the pulse width, in microseconds, corresponding to the minimum (0-degree) angle on the servo (defaults to 544)
* @param maxPulse: the pulse width, in microseconds, corresponding to the maximum (180-degree) angle on the servo (defaults to 2400)
* @return servoId on success, INVALID_SERVO (255) in case of error
*/
virtual uint8_t attach(const uint8_t pin, const uint16_t minPulse, const uint16_t maxPulse) = 0;

/*
* detach the servo from its pin/cannel and release power from the servo.
*/
virtual void detach() = 0;

/*
* write a value to the servo, controlling the shaft accordingly. On a standard servo,
* this will set the angle of the shaft (in degrees), moving the shaft to that orientation.
* On a continuous rotation servo, this will set the speed of the servo (with 0 being
* full-speed in one direction, 180 being full speed in the other, and a value near 90 being
* no movement).
* If the limitter is set, positions are clipped to the lower and upper limit.
* @param position is treated as an angle in degree (0-180)
* INCOMPATIBILTY NOTE: position > MIN_PULSE_WIDTH will not lead a direct call to writeMicroseconds
*/
virtual void write(uint8_t position) = 0;

/*
* writeAnalog is an additional write method, nonexistent in the original Servo library,
* which allows writing 10-bit values directly from an Arduino ADC to the servo.
* @param adcValue is a 10-bit value from an Arduino ADC (range: 0-1023)
*/
virtual void writeAnalog(uint16_t adcValue) = 0;

/*
* writeMicroseconds - Writes a value in microseconds (uS) to the servo, controlling
* the shaft accordingly. On a standard servo, this will set the angle of the shaft.
* On standard servos a parameter value of 1000 is fully counter-clockwise, 2000 is
* fully clockwise, and 1500 is in the middle.
* @param pulse - the value of the pulse with in microseconds
*/
virtual void writeMicroseconds(uint16_t pulse) = 0; // Write pulse width in microseconds

/*
* read the current angle of the servo.
* @return current angle between 0 and 180 degrees
*/
virtual uint8_t read() = 0;

/*
* readMicroseconds of current pulse width of the servo.
* @return current pulse width in microseconds for this servo
*/
virtual uint16_t readMicroseconds() = 0;

/*
* attached - Check whether the Servo is attached to a pin.
* @return true if this servo is attached, otherwise false
*/
virtual bool attached() const = 0;

/*
* limit - set Servo limitter and allow movement only in the specified range of degrees
* during calls to write().
* @param lowerLimit - set the lower limit of the moving range
* @param upperLimit - set the upper limit of the moving range
*/
virtual void limit(uint8_t lowerLimit, uint8_t upperLimit) = 0;
virtual uint8_t getLowerLimit() const = 0;
virtual uint8_t getUpperLimit() const = 0;
};

#endif /* GENERIC_SERVO_H */
Loading

0 comments on commit 7660705

Please sign in to comment.