Welcome

Firmware Tutorial: Dance - Part III

On to Part III! In this part we will add the code to detect the tilt position and make the animations.

Then, you can build on top of this and modify it! How will you make your Robot Butterfly dance?

Let's get started!


12. New Tab

We will add the movement code to a new tab. Press the three dots, and create new tab.

Name the file Movement and press enter.

Here is what is happening:

  • We are creating a new file, the compiler will know to compile this as well
  • This helps keep our code organised

 
60%

13. Implement Callback

Remember way back in part I, step 3 when we added the declarations, and there was a function prototype? Now we will implement it!

Add this to the new file - Movement.


void imuPoseChangeCallback(uint8_t p) {

}
      

Here is what is happening:

  • This is a function that will be called from the Robot Butterfly library upon a change in the IMU position
  • This is helpful because it is an event driven update rather than a polling update. It is helpful because it allows us as the robot butterfly programmers to focus on programming the animation.

 
65%

14. Active Check

Inside of the callback function, we will add code. First up is checking that the IMU is active. If it's not active, we will exit out of the function by returning. those two if-statements, we'll add a servo animation. Plus, we will set last_movement_rest to the current time in milliseconds with the millis() function.

Add this in the callback funciton, in between the two parenthesis { }, still within the callback function.


if(getIMUState() != IMU_ACTIVE) {
  Serial << "IMU not active yet" << endl;
  return;
}
      

Here is what is happening:

  • We are checking the state of the IMU. Remember way back in part 2, step 9, where we did similar with the LEDs? In this case, we will output some text to the console, and then exit out of the function.
  • Exiting out of the function is done by using return. Since the return type of the function is void, it does not need a value to return with.

 
70%

15. Switch

It's time to check for the position! We will do this using a switch statement. The callback function passes a variable p, which contains the enumerated value of the position.

Add this snippet of code after the closing parenthesis } of the above if statement. Still within the callback function.


switch(p) {
  case IMU_Pose_Tilt_L: {

  }
  break;
  case IMU_Pose_Tilt_R: {

  }
  break;
}
      

Here is what is happening:

  • A switch statement is similar to an if statement, however it only checks one condition for a match
  • The variable p is obtained from the callback function passing that to us to use
  • IMU_Pose_Tilt_L and IMU_Pose_Tilt_R are enumerated values provided to us from the Robot Butterfly library
  • Inside each of the cases is where we can add code to make an animation!

 
75%

16. Animation - Left

Now for the fun part! Time to add the animations! We will start with the left animation.

Add this snippet of code in between the parenthesis for case IMU_Pose_Tilt_L.


Serial << "tilt left!" << endl;

setServoAnim(&servo_animation_alert, SERVO_ANIM_FLUTTER, SERVO_ANIM_ALERT);
setServoAnimFlutterWings(&servo_animation_alert, SERVO_ANIM_FLUTTER_WINGS_LEFT_HOME);
setServoAnimFlutterPos(&servo_animation_alert, SERVO_ANIM_FLUTTER_POS_UP);
setServoAnimSpeed(&servo_animation_alert, 200);
setServoAnimDuration(&servo_animation_alert, 2000);
startServoAnim(&servo_animation_alert);

setNeoAnim(&neo_animation_alert, NEO_ANIM_SPRINKLE, NEO_ANIM_ALERT);
setNeoAnimColours(&neo_animation_alert, NEO_GREEN, NEO_WHITE);
setNeoAnimSpeed(&neo_animation_alert, 100);
setNeoAnimDuration(&neo_animation_alert, 2000);
startNeoAnim(&neo_animation_alert);

playSound(SOUND_SMELL_WILDFLOWER);
      

Here is what is happening:

  • Printing text to the console to let us know that the robot is tilting left
  • Setting a servo animation (alert type) to a flutter with the wings up, and specifically the left wing at the home position
  • Setting a neopixel animation (alert type) to a sprinkle animation that is green and white.
  • Playing a sound!

 
80%

17. Animation - Right

Next up we will do the same, now for the right hand side!

Add this snippet of code in between the parenthesis for case IMU_Pose_Tilt_R.


Serial << "tilt right!" << endl;

setServoAnim(&servo_animation_alert, SERVO_ANIM_FLUTTER, SERVO_ANIM_ALERT);
setServoAnimFlutterWings(&servo_animation_alert, SERVO_ANIM_FLUTTER_WINGS_RIGHT_HOME);
setServoAnimFlutterPos(&servo_animation_alert, SERVO_ANIM_FLUTTER_POS_UP);
setServoAnimSpeed(&servo_animation_alert, 200);
setServoAnimDuration(&servo_animation_alert, 2000);
startServoAnim(&servo_animation_alert);

setNeoAnim(&neo_animation_alert, NEO_ANIM_SPRINKLE, NEO_ANIM_ALERT);
setNeoAnimColours(&neo_animation_alert, NEO_BLUE, NEO_WHITE);
setNeoAnimSpeed(&neo_animation_alert, 100);
setNeoAnimDuration(&neo_animation_alert, 2000);
startNeoAnim(&neo_animation_alert);

playSound(SOUND_SMELL_DAISY);
      

Here is what is happening:

  • Printing text to the console to let us know that the robot is tilting right
  • Setting a servo animation (alert type) to a flutter with the wings up, and specifically the right wing at the home position
  • Setting a neopixel animation (alert type) to a sprinkle animation that is blue and white
  • Playing a sound!

 
85%

18. Upload

Time to upload the code!

Upload the code by pressing the right arrow button. This will compile the code and check for errors before uploading.

The output should no longer have red text and no errors, it should say something similar to:


Sketch uses 371849 bytes (28%) of program storage space. Maximum is 1310720 bytes.
Global variables use 27212 bytes (8%) of dynamic memory, leaving 300468 bytes for local variables. Maximum is 327680 bytes.
      

Then, open Serial Monitor by pressing the magnifying glass icon. Make sure the baud rate is set to 9600.

 
90%

19. Modifications

Congrats on completing this tutorial! Now you have a better understanding of how to program Robot Butterfly!

Now, it's time for you to customise the dance! Make it unique to your robot! Here are some ideas for changing the code:

Feel free to ask Erin any questions for how to get started modifying the code with your idea!

 
100%