This is an article from the Animatronic WorkBench Documentation
In more complex animatronic projects, it can be attractive to write custom code instead of configuring the behavior directly from the Animatronic WorkBench (AWB). This can be, for example, the timeline-independent control of servos or the implementation of overarching logic.
This code is written to the 'CustomCode\CustomCode.cpp' file, which is located in the 'src' folder of the project. The associated header file is 'CustomCode.h'. These files can be edited in Visual Studio Code after exporting the PlatformID project.
Custom code should always be written between the comments 'cc-start' and 'cc-end' so that it is not overwritten in upcoming updates. The AWB will update these files as needed, but the custom code will remain. In case something goes wrong, a backup copy of the files is automatically created in the project folder in the subfolder 'custom_code_backup' every time you export.
Custom code example for the project "Animatronic Nix"
Here, the tail servo is searched for in the setup and stored in the variable '_tailServo'. In the loop, a random position between the minimum and maximum of the servo is then set every 4 seconds. This animates the tail of the Animatronic Nix independently of timelines.
#include <Arduino.h> #include "CustomCode.h" #include "../AwbDataImport/ProjectData.h" #include <Actuators/NeopixelManager.h> /* Enter your custom code in this cpp file and the corresponding header file. Only write code beween the cc-start and cc-end comments, otherwise it will be overwritten and lost. */ /* cc-start-include - insert your include code here before the end-protected comment: */ /* cc-end-include - insert your include code here before the end-protected comment: */ void CustomCode::setup() { /* cc-start-setup - insert your setup code here before the end-setup comment: */ // take the servo with Id 10 as tail servo for (int i = 0; i < this->_projectData->stsServos->size(); i++) { // get a pointer to the current servo StsScsServo *servo = &this->_projectData->stsServos->at(i); if (servo->title == "Tail") { _tailServo = servo; break; } } if (_tailServo == nullptr) { _errorOccured("No tail servo found with Id 10. Please check your project data."); } /* cc-end-setup */ } void CustomCode::loop(String actualTimelineName, int actualTimelineStateId) { /* cc-start-loop - insert your loop code here before the end-loop comment: */ auto _msTillLastTailUpdate = millis() - _lastTailUpdate; if (_msTillLastTailUpdate > 4000) { auto max = _tailServo->maxValue; auto min = _tailServo->minValue; // get a random value between min and max auto randomValue = random(min, max); // set the target value of the tail servo to the random value _stSerialServoManager->writePosition(_tailServo->channel, randomValue); _lastTailUpdate = millis(); } /* cc-end-loop */ } /* cc-start-functions - insert your functions here before the end-functions comment: */ /* cc-end-functions */
More files/classes in the 'CustomCode' folder
The PIP Droid project is a more complex example of using custom code in the Animatronic WorkBench. There, the touch buttons and the Neopixel control of the PIP Droid are used to perform various actions. The code implements simple logic to control the PIP Droid, including responding to keystrokes and playing sound effects.
What is special about this project "PIP Droid" is that it integrates additional custom code files that are located in the folder 'CustomCode'. These files can be created to implement further functions. This allows the code to be extended in a modular way without making the main file 'CustomCode.cpp' too confusing. In this project, the files of the following C++ classes are also included as files in the 'CustomCode' folder: