Introduction: Arduino Otto Robot With State Machine

Project Overview

In this project, I want to show you a way of programming the Otto Robot, which is an Arduino based DIY robot. Using YAKINDU Statechart Tools (free for non-commercial) we can easily use state machines to graphically model the behavior of the Otto Robot and generate C/C++ code. We will use one of their examples to extend the behavior to our liking.

For the people that don't know what a state machine is and don't want to rummage through the complicated Wikipedia article, here is a small explanation:

A state machine is just nodes and paths between those nodes. You have a starting node and can take the paths to other nodes depending on their guards, which can be represented by events. These events are raised from either the state machine itself or from the outside(like a function, etc.).

The tool itself uses a drag&drop interface and a domain-specific language. I will go over it for you, so you don't have to dig through their documentation to get your Otto up and running. Setting up the IDE isn't too hard, because all the plug-ins, etc. should get installed automatically.

Supplies

Otto Robot or Zowi Robot

Both of these robots do essentially the same and use the same API. The Otto Robot is a DIY robot, with its parts online, ready to be printed with a 3D printer if you happen to have one. The alternative is the Zowi Robot, which can be purchased online and ready for use.

YAKINDU Statechart Tools

The tool that we will be using to model the state machine. You can start with a 30 days trial and get a free license for non-commercial use afterward.

Eclipse C++ IDE for Arduino Plugin

We don't have to download it manually, because the IDE does it for us. I still thought it would be nice to list it here.

Step 1: Setting Everything Up

After installing the IDE, run it and set up a workspace anywhere on your pc (the setup is identical to using Eclipse for the first time). When the program has started fully, click away the welcome page and click on 'File -> New -> Example...' and then select 'YAKINDU Statechart Examples', wait for a bit and search for the "Embedded Systems -> Zowi (C++)" example.

IMPORTANT: Click on the top right button called 'Install Dependencies...'! This installs everything for you, so you don't have to worry about Libraries, Plug-Ins and the like. Download the example, follow the instructions in the "Embedded Systems -> Zowi (C++)" example and then continue with the next step.

Step 2: Understanding How to Interface the Otto

Go into the ".sct" file and edit the state machine to your liking. On the right is a menu with all the items available. We are only interested in the states and transitions.

In the picture, you can see, that I wrote some stuff onto the transitions; the "after X s" is pretty self-explanatory and the "always" just means, that it goes there right after finishing the code from the State. The "entry /" means, that the code should be executed right after entering the state.

The IDE compiles the state machine to C++, which is Arduino compliant. To use the features of the Otto, we have to do a bit of work ourselves accessing the interface.

The following keywords can be used to define stuff for the state machine to use:

    • constants, which hold values and can't be changed
    • variables, which hold values and can be changed
    • operations, which will be generated to virtual C++ methods for implementation
      interface:
      const PIN_YL : integer = 2
      const PIN_YR : integer = 3
      const PIN_RL : integer = 4
      const PIN_RR : integer = 5
      const sound : integer = 2
      const mouth_heart : integer = 13
      const mouth_happyOpen : integer = 11
      
      operation zowi_init(YL : integer, YR : integer, RL : integer, RR : integer)
      operation zowi_home()
      operation zowi_putMouth(mouthType : integer)
      operation zowi_sing(songName : integer)
      operation zowi_walk(steps : real, T : integer, dir : integer)
      operation zowi_shakeLeg()

      Pro Tip:if you don't know what to enter at someplace or there seems to be an error, press "ctrl+space" to get some hints on what you can enter.

      Additionally, you should look into the examples, they have some code in there as well! You can also use them as a structure to just edit the model, which is the only part we are interested in as of now.

      Step 3: Filling the Gaps

      After changing things in the model you can right-click on the "zowiSCT.sgen -> Generate Code Artifacts". This generates the virtual functions in C++, which are declared in the state machine to the folder "src-gen", which we then implement using normal C++.

      Just create these two files in the "src" folder to get the functionality we want from Otto.

      First the Impl.h

      #ifndef SRC_IMPL_H_
      #define SRC_IMPL_H_
      #include "../src-gen/ZowiSCT.h"
      class Impl : public ZowiSCT::DefaultSCI_OCB{
      public:
      	Impl();
      	virtual ~Impl();
      	void zowi_init(sc_integer YL, sc_integer YR, sc_integer RL, sc_integer RR);
      	void zowi_home();
      	void zowi_putMouth(sc_integer mouthType);
      	void zowi_sing(sc_integer songName);
      	void zowi_walk(sc_real steps, sc_integer T, sc_integer dir);
      	void zowi_shakeLeg();
      };
      #endif /* SRC_IMPL_H_ */

      Then the Impl.cpp

      #include "Impl.h"
      #include "../Zowi/Zowi.h"
      Zowi zowi = new Zowi();
      Impl::Impl() {}
      Impl::~Impl() {}
      void Impl::zowi_home() {
      	zowi.home();
      }
      void Impl::zowi_init(sc_integer YL, sc_integer YR, sc_integer RL, sc_integer RR) {
      	zowi.init(YL, YR, RL, RR);
      }
      void Impl::zowi_putMouth(sc_integer mouthType) {
      	zowi.putMouth(mouthType);
      }
      void Impl::zowi_sing(sc_integer songName) {
      	zowi.sing(songName);
      }
      void Impl::zowi_walk(sc_real steps, sc_integer T, sc_integer dir) {
      	zowi.walk(steps, T, dir);
      }
      void Impl::zowi_shakeLeg() {
      	zowi.shakeLeg();
      }

      Step 4: Making the Otto Dance

      When you are happy with your product, click on the hammer at the top left and wait for the process to finish. Then click on the green arrow to the right of the hammer and see your Otto dancing!

      If you want, you can check out some other examples: YAKINDU Statechart Tools