As easy as 1,2,3 – Robot Zowi learns Statecharts
You know Zowi? No? May I introduce to you: Zowi is a robot for kids to teach them programming reactive systems that physically interact with their environment. Zowi walks on two feets, has an ultrasonic distance sensor as eyes, an array of LEDs as mouth, makes sound, and detects noise. It's the perfect match to program it with statecharts.
Very young kids of less than eight years can start playing around with Zowi via a smartphone app. They steer it or execute predefined motion and tone sequences. They can even program it by creating new sequences of the given motion and tone sequences.
When they are getting older, kids may program more complex interactions for Zowi by using a web based visual block language or program it directly with C/C++. Since Zowi is based on the well known Arduino Uno microprocesser ATmega328, it might also be programmed with the Arduino tool chain.
Anyway, all of these programming concepts are based on classical imperative programming techniques. Reactive systems like robots are in fact a perfect match to be modelled with state machines. A state machine defines possible states of a system and changes the system's current state by reacting on events from the outside or by a timing schedule.
Let Zowi learn to walk
Let's consider a typical, simple scenario for a robot. Zowi should walk straight ahead until it detects an obstacle. In this case Zowi turns left to avoid the obstacle and goes on with walking. After some time, Zowi gets tired and needs to rest. When sleeping, Zowi wakes up if it hears some noise and begins to walk again.
Programming this scenario in pure C or C++ is no fun. Using a lot of IFs and ELSEs causes the code to get lengthy and unreadable. There are various techniques like encapsulation to mitigate this but it's still uncomfortable programming. It lets you loose your focus on modelling the robot's behaviour.
Why not just model the state machine by a visually appealing statechart and let the computer do the rest? Let the computer generate the code for the state machine and never touch it again. Concentrate on modelling the system's behaviour.
YAKINDU Statechart Tools (YAKINDU SCT) brings to you a visual DSL (Domain Specific Language) for state machines and code generators for various languages like C, C++, or Java. In my last blog post I wrote about how to program an Arduino Uno with YAKINDU Statechart Tools. I introduced YAKINDU Statechart Tools for Arduino that adds a special C++ source code generator to YAKINDU SCT for various well known and widely used microprocessors like ATmega328, ATmega2560, ATmega32u4, ESP8266, and their derivates.
Have a look at this short video to see how simple it is to program Zowi based on statecharts with YAKINDU Statechart Tools. I modelled exactly the scenario described above with a statechart and generated the code. After programming some glue code to link the hardware with the state machine and vice versa, I compiled the code and uploaded it to Zowi. Finally, Zowi acts like i planned it. It's that easy! If you like it, use the toolkit for your own robot or IoT projects. You can download YAKINDU Statechart Tools and YAKINDU Statechart Tools for Arduino for free!