Firmware - Main
Webpage under constructionSketch
Here's how to get started with a Robot Butterfly sketch. First up, include the library and declare the object:
#include <RobotButterfly.h>
RobotButterfly robotbutterfly;
// -- function prototypes --
void setupState1();
void loopState1();
// --
Here's part 1 of what's inside setup:
Serial.begin(9600);
robotbutterfly = RobotButterfly();
robotbutterfly.init();
Here's part 2 of what's inside setup:
robotbutterfly.addState(RobotButterfly::STATE1, setupState1, loopState1);
Serial << "Welcome to Robot Butterfly!" << endl;
playSound(SOUND_ALERT_STARTUP);
robotbutterfly.changeState(RobotButterfly::STATE1);
Here's what's inside loop:
robotbutterfly.update();
Now for the state machine! Here's the setup function:
void setupState1() {
if(new_enter) {
// first entrance into setup
new_enter = false;
}
// looping code in setup before it transitions to loop
}
Now for the state machine! Here's the loop function:
void loopState1() {
if(new_update) {
// first entrance into loop
new_update = false;
}
// looping code goes here
}
Since Robot Butterfly uses an RTOS, a function that sets the priorities of each task can be called. This helps optimise the behaviour for what it is currently working on. This is best called in the first entrance of the state's setup. Each state has the priority levels defined in ParamsRTOS.h (TODO: insert link), as well, tskIDLE_PRIORITY can be used.
void state1Priorities() {
setButtonsTaskPriority(PRIORITY_BUTTONS_MID);
setIMUTaskPriority(tskIDLE_PRIORITY);
setNeoAnimationTaskPriority(PRIORITY_NEOANIM_MID);
setProximityTaskPriority(tskIDLE_PRIORITY);
setSensorsTaskPriority(tskIDLE_PRIORITY);
setServoAnimationTaskPriority(PRIORITY_SERVOANIM_HIGH);
setSoundTaskPriority(PRIORITY_SOUND_MID);
}
Here's how you can navigate through the state machine:
static void addState(uint8_t id, StateSetup setup_fn, StateLoop loop_fn);
static void changeState(uint8_t n);
static void incrementState();
static void decrementState();
static void printStateHeartbeat(uint8_t id);