Serial Wombat Arduino Library
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
SerialWombatServo Class Reference

A class to control Servos attached to the Serial Wombat chip. More...

#include <SerialWombatServo.h>

Inheritance diagram for SerialWombatServo:
SerialWombatPin SerialWombatServo_18AB SerialWombatHighFrequencyServo

Public Member Functions

 SerialWombatServo (SerialWombatChip &sw)
 Constructor for the SerialWombatServo Class. More...
 
void attach (uint8_t pin, bool reverse)
 Initialize a servo on the specified pin. More...
 
void attach (uint8_t pin, uint16_t min=544, uint16_t max=2400, bool reverse=false)
 Initialize a servo on the specified pin. More...
 
void write (uint8_t angle)
 Writes a value to the servo. More...
 
void write16bit (uint16_t position)
 Writes a 16 bit value to the servo. More...
 
uint8_t read (void)
 returns the last position of the servo scaled to a number from 0 to 180. More...
 
- Public Member Functions inherited from SerialWombatPin
 SerialWombatPin (SerialWombatChip &serialWombatChip)
 Instantiates a Serial Wombat Pin. More...
 
 SerialWombatPin (SerialWombatChip &serialWombatChip, uint8_t pin)
 Instantiates a Serial Wombat Pin. More...
 
uint16_t readPublicData ()
 Read the 16 Bit public data associated with this pin. More...
 
void pinMode (uint8_t mode, bool pullDown=false, bool openDrain=false)
 Set pin to INPUT or OUTPUT, with options for pull Ups and open Drain settings. More...
 
void digitalWrite (uint8_t val)
 Set output pin High or Low. More...
 
int digitalRead ()
 Reads the state of the Pin. More...
 
uint16_t writePublicData (uint16_t value)
 Write a 16 bit value to this pin. More...
 
uint8_t pin ()
 Returns the current SW pin number. Used primarily for virtual calls by derived classes. More...
 
uint8_t swPinModeNumber ()
 Returns the Mode number. Used primarily by derived classes to populate packet data. More...
 

Protected Member Functions

void initializeServo ()
 

Protected Attributes

uint16_t _position = 0
 
uint16_t _min = 544
 
uint16_t _max = 2400
 
bool _reverse = false
 
- Protected Attributes inherited from SerialWombatPin
uint8_t _pin = 255
 
SerialWombatChip_sw
 
uint8_t _pinMode = 0
 

Detailed Description

A class to control Servos attached to the Serial Wombat chip.

The SerialWombatServo class is used to control hobby servos attached to the Serial Wombat chip.

Any output-capable pin may be attached to an instance of the SerialWombatServo class. A separate instance should be declared for each Servo.

On the Serial Wombat 4A and Serial Wombat 4B chips servo output pulses are timed to 1uS precision using a timer interrupt.

On the Serial Wombat 18AB chip all Enhanced Digital Capability output pins (0-4,7,9-19) can be timed to sub-microsecond precision by sharing hardware timing resources (assuming resources are avaialble and have not be permanently claimed by PWM pin modes or other pin modes that claim and hold timing resources).

For SW18AB pins that are not enhanced, or if timing resources are not available the pin mode will degrade to using DMA based output with 17uS resolution. This equates to slightly better than 7 bit resolution for a 500-2500uS pulse range servo.

The SW4B_Ard_Servo example included with the Arduino library shows how to use this class.

This class uses "attach" rather than "begin" to initialize servos to be consistent with the Arduino Servo native API.

A Tutorial video is also avaialble:

https://youtu.be/WiciAtS1ng0

Constructor & Destructor Documentation

◆ SerialWombatServo()

SerialWombatServo::SerialWombatServo ( SerialWombatChip sw)
inline

Constructor for the SerialWombatServo Class.

Parameters
swA reference to a previously declared SerialWombatChip to which the Servo is connected.

Member Function Documentation

◆ attach() [1/2]

void SerialWombatServo::attach ( uint8_t  pin,
bool  reverse 
)
inline

Initialize a servo on the specified pin.

This function must be called after the SerialWombatChip instance specified in the constructor has been initialized with a begin call.

This function initializes a servo with a minimum pulse of 544 uS and a maximum pulse of 2400uS to be consistent with the native Arduino API attach function.

Parameters
pinThe Serial Wombat pin to set. Valid values for SW4A: 0-3 SW4B: 1-3
reverseIf true, the pulse widths will be reversed, with the longest pulse width corresponding to 0 position.

◆ attach() [2/2]

void SerialWombatServo::attach ( uint8_t  pin,
uint16_t  min = 544,
uint16_t  max = 2400,
bool  reverse = false 
)
inline

Initialize a servo on the specified pin.

This function must be called after the SerialWombatChip instance specified in the constructor has been initialized with a begin call.

Parameters
pinThe Serial Wombat pin to set. Valid values for SW4A: 0-3 SW4B: 1-3
minMinimum pulse width in uS. Minimum functional value is 100.
maxMaximum pulse width in uS
reverseIf true, the pulse widths will be reversed, with the longest pulse width corresponding to 0 position.

◆ initializeServo()

void SerialWombatServo::initializeServo ( )
inlineprotected

◆ read()

uint8_t SerialWombatServo::read ( void  )
inline

returns the last position of the servo scaled to a number from 0 to 180.

Returns
A position value from 0 to 180.

◆ write()

void SerialWombatServo::write ( uint8_t  angle)
inline

Writes a value to the servo.

This funciton allows setting the "angle" of a servo, although actual travel may vary. Consider using the SerialWombatServo function write16bit() for higher resolution

Parameters
angleA value from 0 to 180

◆ write16bit()

void SerialWombatServo::write16bit ( uint16_t  position)
inline

Writes a 16 bit value to the servo.

This funciton scales the pulse width of the signal being sent to the servo.

Parameters
positionServo position. 0 sends the minimum pulse width. 65535 sends the maximum pulse width (unless Reverse is enabled)

Member Data Documentation

◆ _max

uint16_t SerialWombatServo::_max = 2400
protected

◆ _min

uint16_t SerialWombatServo::_min = 544
protected

◆ _position

uint16_t SerialWombatServo::_position = 0
protected

◆ _reverse

bool SerialWombatServo::_reverse = false
protected