PServo v0.2.1
|
#include <PServo.h>
Public Member Functions | |
PServo (unsigned long *const timer) | |
PServo (unsigned long *const timer, bool const is_resetable) | |
PServo (unsigned long *const timer, unsigned char const min, unsigned char const max) | |
PServo (unsigned long *const timer, unsigned char const min, unsigned char const max, bool const is_resetable) | |
PServo * | begin (void) |
PServo * | move (unsigned char const next_pos) |
PServo * | move (unsigned char const next_pos, unsigned short const delay) |
Props const | props (void) const |
State const | get_state (void) const |
bool | is_state (State s) const |
unsigned char | pos (void) const |
void | reset (void) |
Main class of the library, represents the state machine of an asyncronous servo motor object. Which means that this can hold the position (in deg) inside it, so you can get use that information to wirte in a real servo motor component.
Since it uses an asyncronous logic, you need to pass the reference of an counter, and this counter should be be updated every loop iteration (see the example).
The logic to make it work is quite simple, start with the PServo::being()
call, so it knows where set of movements needs to stop and start other for each loop, then put your sequence of movements that you want to perform one after another. This machine is smart enough to figure out which movement should be executed and which should be just ignored.
For an example:
|
inline |
The constructor need, at least, a pointer to a timer variable. This variable, normally, is a global one, that multiple state machines can read to calculate the delay asynchronously.
For an example, at the top of your main Arduino sketch file, you may have a unsigned long timer = 0;
line, then, inside the loop()
function, you'll update that variable value everytime with the millis()
function – which will return the time duration since your Arduino board was booted up, in milisseconds.
For an example:
This choice was made because every pointer in C++ uses 16 bits of memory all the time, and an unsigned long
uses 64 bits, which is much havier, mainly when dealing with multiple object instances of this class.
timer | Pointer to a timer variable, normally related to the millis() function. |
|
inline |
The constructor need, at least, a pointer to a timer variable. This variable, normally, is a global one, that multiple state machines can read to calculate the delay asynchronously.
You can also pass the reset setting, a bolean type parameter, to the constructor. By default, the machine will not reset after the last action completion.
timer | Pointer to a timer variable, normally related to the millis() function. |
is_resetable | Configure the machine to reset after it's halted. |
|
inline |
The constructor need, at least, a pointer to a timer variable. This variable, normally, is a global one, that multiple state machines can read to calculate the delay asynchronously.
If needed, you can also configure the max and min position that this machine can assume. It's quite useful when your building a robot, as an example, that some movements can break the structure of the robot phisically.
timer | Pointer to a timer variable, normally related to the millis() function. |
min | Minimul position value that this machine can be. |
max | Maximum position value that this machine can be. |
|
inline |
The constructor need, at least, a pointer to a timer variable. This variable, normally, is a global one, that multiple state machines can read to calculate the delay asynchronously.
And for more advanced configuration, or verbosity, you can combine the other constructors into just this one. Where you pass the timer pointer, the min-max values and the seeting to define if this machine should reset to the first movement after done or not!
timer | Pointer to a timer variable, normally related to the millis() function. |
min | Minimul position value that this machine can be. |
max | Maximum position value that this machine can be. |
is_resetable | Configure the machine to reset after it's halted. |
ps::PServo * ps::PServo::begin | ( | void | ) |
This function is the most important one, it should be used everytime at the beginning of a move set collection, that's because it will handle most of the possible states of this machine – such as count the number of actions it should take on the first call or check if the machine should halt or start over again.
The reason this function, and the others, returns a pointer reference of the this same object class is to help you write sets of movements without so much code repetition. It is a syntax sugar, if you will.
For example, instead of doing this:
You can simply do:
Since this function is asynchronous, it should be called every time in the loop()
function! That's because it uses the _timer
value – defined in this class constructor – to calculate how when the current servo position should be updated.
->
syntax to write a stream of actions that this state machine will perform. ps::State const ps::PServo::get_state | ( | void | ) | const |
Used to know which state the machine is currently in. Used to catch state and handle state errors or build complex systems that behaves differently based on the state of this machine.
bool ps::PServo::is_state | ( | ps::State | s | ) | const |
Check if the state machine is in the specified state. Which is better than getting the state manually and then comparing it by hand.
ps::PServo * ps::PServo::move | ( | unsigned char const | next_pos | ) |
This function just calls it self again, but passing the ps::Default::DELAY
as a seccodary parameter to move (or, in this context, update the value of the _pos
private attribute) is a specific speed.
This function overload can be helpful because, in some cases, is necessary to just move the servo motors in an asynchronous way. So it helps to write less code and be more descriptve of what you want to do and not how.
Check for more details the explaination of the full version of this function call, the one that you need to specify the movement speed too.
next_pos | Next position that it needs to move to. |
->
syntax to write a stream of actions that this state machine will perform. ps::PServo * ps::PServo::move | ( | unsigned char const | next_pos, |
unsigned short const | delay ) |
This method will start the movement action, it only works if the machine is in the ps::State::IN_ACTION
, otherwise, it will perform nothing.
Note: In
ps::State::INITIALIZED
mode, it will, actually, count the movement. This is because the first run of the move set is for determining how much moves it should do before halt/reset.
The delay
paramter is not the overall duration of the movement! It is, actually, the delay between each degree from the the current position to the next one. For an example, if you're at 0
and want to go to 180
with 10
for delay, the overall duration will be 1.8
seconds.
Since this function is asynchronous, it should be called every time in the loop()
function! That's because it uses the _timer
value – defined in this class constructor – to calculate how when the current servo position should be updated.
next_pos | Next position that it needs to move to. |
delay | Delay between each position increment, until it's done. |
->
syntax to write a stream of actions that this state machine will perform. unsigned char ps::PServo::pos | ( | void | ) | const |
Used to get the current servo position, in order to mirror this value to a real servo, which will write that value position every time on the loop()
function.
_min
and _max
values, setted on the constructor of the class. ps::Props const ps::PServo::props | ( | void | ) | const |
This method allows the user to inspect all the private attributes of the object. It's quite useful for loggin or monitoring sketches, or maybe to build more complex sketches that behaves differently depending of the state of this object.
ps::Props
struct with all the private members values of the instantiated object. void ps::PServo::reset | ( | void | ) |
Resets the machine state back to the SANTDBY, the counter of actions is also reset to 0. It's useful when you want to use a scene based moveset pattern. For an example: