PServo v0.2.1
Loading...
Searching...
No Matches
Public Member Functions | List of all members
ps::PServo Class Reference

#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)
 
PServobegin (void)
 
PServomove (unsigned char const next_pos)
 
PServomove (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)
 

Detailed Description

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:

unsigned long timer = 0;
ps::PServo myservo_machine(&timer);
Servo myservo;
void setup() {
Servo.attach(8);
}
void loop() {
timer = millis();
myservo.write(myservo_machine.props().pos);
myservo_machine.begin()
->move(90, 10)
->move(180, 25)
->move(0, 5);
}
Definition PServo.h:143
See also
ps::Props
ps::State

Constructor & Destructor Documentation

◆ PServo() [1/4]

ps::PServo::PServo ( unsigned long *const timer)
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:

unsigned long timer = 0;
ps::PServo myservo_machine(&timer);
void setup() {
// Your setup code...
}
void loop() {
timer = millis();
// Your loop code...
}

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.

Parameters
timerPointer to a timer variable, normally related to the millis() function.
See also
ps::PServo

◆ PServo() [2/4]

ps::PServo::PServo ( unsigned long *const timer,
bool const is_resetable )
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.

Parameters
timerPointer to a timer variable, normally related to the millis() function.
is_resetableConfigure the machine to reset after it's halted.

◆ PServo() [3/4]

ps::PServo::PServo ( unsigned long *const timer,
unsigned char const min,
unsigned char const max )
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.

Parameters
timerPointer to a timer variable, normally related to the millis() function.
minMinimul position value that this machine can be.
maxMaximum position value that this machine can be.
See also
ps::Default

◆ PServo() [4/4]

ps::PServo::PServo ( unsigned long *const timer,
unsigned char const min,
unsigned char const max,
bool const is_resetable )
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!

Parameters
timerPointer to a timer variable, normally related to the millis() function.
minMinimul position value that this machine can be.
maxMaximum position value that this machine can be.
is_resetableConfigure the machine to reset after it's halted.
See also
ps::Default

Member Function Documentation

◆ begin()

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:

myservo_machine.begin();
myservo_machine.move(90, 10);
myservo_machine.move(180, 50);
myservo_machine.move(0, 30);

You can simply do:

myservo_machine.begin()
->move(90, 10)
->move(180, 50)
->move(0, 30);

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.

Returns
A pointer to this same object, allowing the use of the -> syntax to write a stream of actions that this state machine will perform.

◆ get_state()

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.

Returns
The current state of the machine.

◆ is_state()

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.

Returns
A boolean that tells if the machine state is in the state specified or not.

◆ move() [1/2]

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.

Parameters
next_posNext position that it needs to move to.
Returns
A pointer to this same object, allowing the use of the -> syntax to write a stream of actions that this state machine will perform.

◆ move() [2/2]

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.

Parameters
next_posNext position that it needs to move to.
delayDelay between each position increment, until it's done.
Returns
A pointer to this same object, allowing the use of the -> syntax to write a stream of actions that this state machine will perform.

◆ pos()

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.

Returns
The current registered postion, this value whould be between the _min and _max values, setted on the constructor of the class.

◆ props()

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.

Returns
A ps::Props struct with all the private members values of the instantiated object.

◆ reset()

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:

switch (curr_scene) {
case 0:
machine.begin()->move(180, 100);
if (machine.is_state(ps::State::HALT)) {
machine.reset();
curr_scene = 1;
}
break;
case 1:
machine.begin()->move(0, 25);
if (machine.is_state(ps::State::HALT)) {
machine.reset();
curr_scene = 0;
}
break;
}
@ HALT
No operation, the final action was completed (NOOP).

The documentation for this class was generated from the following files: