Serial Wombat Arduino Library

Loading...
Searching...
No Matches
SerialWombatServo.h
Go to the documentation of this file.
1#pragma once
2/*
3 Copyright 2020-2025 Broadwell Consulting Inc.
4
5"Serial Wombat" is a registered trademark of Broadwell Consulting Inc. in
6 the United States. See SerialWombat.com for usage guidance.
7
8 Permission is hereby granted, free of charge, to any person obtaining a
9 copy of this software and associated documentation files (the "Software"),
10 to deal in the Software without restriction, including without limitation
11 the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 and/or sell copies of the Software, and to permit persons to whom the
13 Software is furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 OTHER DEALINGS IN THE SOFTWARE.
25*/
26
27#include "SerialWombat.h"
28
29
32
68 class SerialWombatServo : public SerialWombatPin
69{
70 public:
76 SerialWombatServo(SerialWombatChip& sw):SerialWombatPin(sw)
77 {
78 _pinMode = PIN_MODE_SERVO;
79 }
80
92 void attach(uint8_t pin, bool reverse)
93 {
94 _pin = pin;
95 _pinMode = PIN_MODE_SERVO;
96 _reverse = reverse;
97 initializeServo();
98 }
99
111 void attach(uint8_t pin, uint16_t min = 544, uint16_t max = 2400, bool reverse = false)
112 {
113 _pin = pin;
114 _min = min;
115 _max = max;
116 _reverse = reverse;
117 _pinMode = PIN_MODE_SERVO;
118 initializeServo();
119 }
120
129 void write(uint8_t angle)
130 {
131 if (angle < 180)
132 {
133 write16bit((uint16_t)(65536uL * angle / 180));
134 }
135 else
136 {
137 write16bit(65535uL);
138 }
139
140 }
141
149 void write16bit(uint16_t position)
150 {
151 _position = position;
152 _sw.writePublicData(_pin, _position);
153 }
154
160 uint8_t read(void)
161 {
162 uint32_t returnval = _position;
163 returnval *= 180;
164 return (uint8_t)(returnval >> 16);
165 }
166
167 protected:
168
169 uint16_t _position = 0;
170 uint16_t _min = 544; // Default for Arduino Servo library
171 uint16_t _max = 2400; // Default for Arduino Servo Library
172 bool _reverse = false;
173 void initializeServo()
174 {
175 uint8_t tx[] = { 200,_pin,_pinMode,_pin,SW_LE16(_position), _reverse,0x55 };
176 uint8_t rx[8];
177 _sw.sendPacket(tx, rx);
178 uint8_t tx2[] = { 201,_pin,_pinMode,SW_LE16(_min), SW_LE16(_max-_min),0x55, 0x55 };
179 _sw.sendPacket(tx2, rx);
180
181 }
182};
183
208 class SerialWombatServo_18AB : public SerialWombatServo, public SerialWombatAbstractScaledOutput
209{
210 public:
211 SerialWombatServo_18AB(SerialWombatChip& serialWombat): SerialWombatServo(serialWombat),SerialWombatAbstractScaledOutput(serialWombat)
212 {
213 }
214 uint8_t pin() {return SerialWombatPin::_pin;}
215 uint8_t swPinModeNumber() { return SerialWombatPin::_pinMode;}
216};
217
253 class SerialWombatHighFrequencyServo : public SerialWombatServo_18AB
254{
255
256 public:
257 SerialWombatHighFrequencyServo(SerialWombatChip& serialWombat) :SerialWombatServo_18AB(serialWombat) {}
264 void attach(uint8_t pin, bool reverse);
265
266
279 void attach(uint8_t pin, uint16_t min , uint16_t max , bool reverse = false)
280 {
281 _pin = pin;
282 _min = min;
283 _max = max;
284 _reverse = reverse;
285 _pinMode = PIN_MODE_HS_SERVO;
286 initializeServo();
287 }
288
289 int16_t writeFrequency_Hz(uint16_t frequency_hZ)
290 {
291 return writePeriod_uS((uint16_t)(1000000ul / frequency_hZ));
292 }
293 int16_t writePeriod_uS(uint16_t period_uS)
294 {
295 uint8_t tx[] = { 203,_pin,_pinMode,SW_LE16(period_uS), 0x55,0x55,0x55 };
296 uint8_t rx[8];
297 return _sw.sendPacket(tx, rx);
298 }
299};
SerialWombat.h
SW_LE16
#define SW_LE16(_a)
Convert a uint16_t to two bytes in little endian format for array initialization.
Definition SerialWombat.h:41
PIN_MODE_HS_SERVO
@ PIN_MODE_HS_SERVO
(26)
Definition SerialWombat.h:274
PIN_MODE_SERVO
@ PIN_MODE_SERVO
(3)
Definition SerialWombat.h:254
SerialWombatAbstractScaledOutput::SerialWombatAbstractScaledOutput
SerialWombatAbstractScaledOutput(SerialWombatChip &sw)
Constructor for the SerialWombatAbstractScaledOutput Class.
Definition SerialWombatAbstractScaledOutput.h:70
SerialWombatChip
Class for a Serial Wombat chip. Each Serial Wombat chip on a project should have its own instance.
Definition SerialWombat.h:297
SerialWombatHighFrequencyServo::SerialWombatHighFrequencyServo
SerialWombatHighFrequencyServo(SerialWombatChip &serialWombat)
Definition SerialWombatServo.h:257
SerialWombatHighFrequencyServo::writeFrequency_Hz
int16_t writeFrequency_Hz(uint16_t frequency_hZ)
Definition SerialWombatServo.h:289
SerialWombatHighFrequencyServo::writePeriod_uS
int16_t writePeriod_uS(uint16_t period_uS)
Definition SerialWombatServo.h:293
SerialWombatHighFrequencyServo::attach
void attach(uint8_t pin, uint16_t min, uint16_t max, bool reverse=false)
Initialize a high Speed servo on the specified pin.
Definition SerialWombatServo.h:279
SerialWombatHighFrequencyServo::attach
void attach(uint8_t pin, bool reverse)
Do not use this interface for High Frequency Servos.
SerialWombatPin::_sw
SerialWombatChip & _sw
Definition SerialWombatPin.h:194
SerialWombatPin::SerialWombatPin
SerialWombatPin(SerialWombatChip &serialWombatChip)
Instantiates a Serial Wombat Pin.
Definition SerialWombatPin.h:45
SerialWombatPin::_pin
uint8_t _pin
Definition SerialWombatPin.h:193
SerialWombatPin::pin
uint8_t pin()
Returns the current SW pin number. Used primarily for virtual calls by derived classes.
Definition SerialWombatPin.h:121
SerialWombatPin::_pinMode
uint8_t _pinMode
Definition SerialWombatPin.h:195
SerialWombatServo_18AB::SerialWombatServo_18AB
SerialWombatServo_18AB(SerialWombatChip &serialWombat)
Definition SerialWombatServo.h:211
SerialWombatServo_18AB::pin
uint8_t pin()
Facilitates inheritance.
Definition SerialWombatServo.h:214
SerialWombatServo_18AB::swPinModeNumber
uint8_t swPinModeNumber()
Facilitates inheritance.
Definition SerialWombatServo.h:215
SerialWombatServo::_max
uint16_t _max
Definition SerialWombatServo.h:171
SerialWombatServo::write16bit
void write16bit(uint16_t position)
Writes a 16 bit value to the servo.
Definition SerialWombatServo.h:149
SerialWombatServo::_min
uint16_t _min
Definition SerialWombatServo.h:170
SerialWombatServo::_reverse
bool _reverse
Definition SerialWombatServo.h:172
SerialWombatServo::read
uint8_t read(void)
returns the last position of the servo scaled to a number from 0 to 180.
Definition SerialWombatServo.h:160
SerialWombatServo::attach
void attach(uint8_t pin, uint16_t min=544, uint16_t max=2400, bool reverse=false)
Initialize a servo on the specified pin.
Definition SerialWombatServo.h:111
SerialWombatServo::initializeServo
void initializeServo()
Definition SerialWombatServo.h:173
SerialWombatServo::write
void write(uint8_t angle)
Writes a value to the servo.
Definition SerialWombatServo.h:129
SerialWombatServo::attach
void attach(uint8_t pin, bool reverse)
Initialize a servo on the specified pin.
Definition SerialWombatServo.h:92
SerialWombatServo::_position
uint16_t _position
Definition SerialWombatServo.h:169
SerialWombatServo::SerialWombatServo
SerialWombatServo(SerialWombatChip &sw)
Constructor for the SerialWombatServo Class.
Definition SerialWombatServo.h:76