A Virtual Ride on an Electric Scooter using a Digital Twin

A tiny chip controls your scooter

You unlock the scooter, gently push the throttle and the scooter promptly shoots forward and off you go.

Behind this simple mechanic, there is a considerable amount of software that is running inside the scooter on an electronic device. It is responsible for many different tasks.

The most visible aspect is the display, that shows information about the speed of the scooter, the state of the battery, and other important messages.

The scooter firmware also controls the speed of the scooter. In some cities, there are zones where the scooter cannot enter or must drive slowly. In that case the scooter will automatically slow down in a safe way. In other cases, the scooter controller must prevent that the rider parks it in a forbidden location. The following map shows these different zones for Washington DC.

Besides that, the scooter also communicates with an external server, to exchange information about the GPS location, the battery charge, and mechanical problems.

Why a digital twin speeds up development

Developing the software for such a system is complicated by the physical nature of smart devices and Internet-of-things. Before the code can be developed or tested, first the hardware board must be developed and produced, and the mechanical aspects must mostly be finished.

"Firmware is late"

"Firmware is late" is often quoted in companies to signal that the delivery date needs to be pushed back. However, in many cases, the embedded software team has been set back by delays in earlier development stages.

With a digital twin, the complete system including software and physics is simulated on the development machine of the programmer. This way, most of the application can be developed without touching any hardware.

As a consequence, the firmware department can perform the software development in parallel to the hardware design. This results is less coupling between the departments, thereby the software team becomes more independent.

First modeling, then coding

A digital twin is a virtual representation of a physical system. Functionalities of the real system are replicated in a simulation tool. The image below shows a digital twin of the electric scooter.

By dragging and dropping components from a library a digital twin is created. Each component replicates the functionality of a real-world object. The scooter for example has a button to lock or unlock, a knob for the throttle, an LCD display to show some info, and some status LEDs.

It is not important how the virtual model looks like, it only has to represent the functionality of the actual system. So the steering wheel is also represented here by a knob.

With the same principle, the bottom right part of the model computes the charge of the battery. It starts at 100% and goes down when the scooter moves.

The bottom left part of the model is more complex and computes the location of the scooter based on its speed and steering angle. It is visualized on a map. With the GPS component, the coordinates are sent to the scooter controller, which is the big blue component in the middle.

By modeling the application first, you think more about "What" the system should do rather than "How" it should be implemented. Moreover, iterating on the design is easy in a virtual world, so a better designed is obtained upfront.

Programming the controller

The controller (big blue component) contains the code of the scooter. It receives signals from the virtual sensors and other inputs. It also produces virtual output signals.

The application is written in C. It is written like any other embedded program in your preferred code editor or IDE. Studio Technix provides a C API, so you can connect the application to the virtual sensors, actuators and communication devices of the simulator.

Scooter Demo Code

This API resembles the Hardware Abstraction Layer (HAL) of most embedded frameworks, so once you are satisfied with the program in the virtual environment, porting to real hardware is easy.

Testing usage scenarios with simulation

With a push of the "Play" button, the simulation is started. The application is loaded and starts receiving signals from the virtual sensors, and any output signals that its produces are visualized, either on one of the gauges, text display, map, or graphs.

The simulation is interactive, and by pressing the lock button, or turning the throttle and steering knobs, you can make the scooter move as shown by the indicator on the map.

If you move, the scooter uses energy. This is reflected by the battery state plot. The faster you move, the faster the battery drains.

Try to stop and park in a "No Parking zone". You'll notice that the embedded application produces an error message on the display and the scooter remains unlocked.

Project files

Download the project files to try out the scooter example.