Embedded system simulations using tessim
Tessim is an in-development software project to enable scalable and efficient simulations of embedded systems. The aim of this tool is to allow hardware engineers, embedded software engineers and systems engineers to work in the same environment while designing and developing a product.
In this post I will give an example of how this can be used. Currently the software is in the form of a shared library file that can be used with python. The python library file, (python_cli_interface.py) acts as a way to call the functions and objects of the library, thus acting as a user interface, that can be used in the python command line. In this blog I will demonstrate how this library can be used by taking an example of a motor being controller by an atmega328p.
to start open the python3 cli :
:~$ python3
Python 3.8.5 (default, May 27 2021, 13:30:53)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
>>>import python_cli_commands as cli
Creating a component
Next create the required components, to start with let's create a motor :
>>>mt = cli.dcMotor(0.05,0.05 , 0.5, 0.0015, 0.00025, 0.001)
This command will create a simple PMDC motor with the given parameters. The general syntax of the function is :
dcMotor(kt, ke, r, l, jm, bm)
where the parameters mean the following :
- kt torque constant
- ke electrical constant
- r : armature resistance
- l : motor inductance
- jm : motor inertia
- bm : friction coefficient.
Adding and connecting nodes
Now that we have created a motor with given parameters. We need to connect it to "nodes". These nodes can be used to provide input voltages and connect other components.
in order to create nodes use :
>>>nodes = cli.createNodes(2)
the above command will create two nodes and assign them to the pointer nodes (python does not have pointers but the shared library returns a pointer, it will be stored as an integer). The motor that we created earlier has two node pointers , which can be assigned to the nodes that were just created. This can be done by the following commands :
>>>mt.setNodes(0, 1)
This will connect the motor to nodes 0 and 1.
Simulating the Motor
>>>cli.putValue(0,12)
This command will put a value of 12 V on node 0. In order to simulate the motor now we will use the following command :
>>>cli.execAll(0.0025, 4)
The above command will simulate the entire system (right now there is only motor in the system but as we will see further, there can be multiple components) together with for 4 seconds with a time division of 2.5ms.
In order to get the final speed of the motor use :
>>>mt.getSpeed()
This should output 166.66666666666646 on the terminal (for the given parameters). this is the motor speed at the end of the simulation. In order to get the time evolution of speed the object variable speeds inside the motor object can be used. In order to plot the speed as a function of time we can use the matplotlib library in python.
>>>plt.plot(mt.time, mt.speeds
>>>plt.xlabel("time (s)")
>>>plt.ylabel("speed (rad/s)")
>>>plt.show()
this should plot the speed of the motor as a function of time.
No to this simulation environment let's add a motor driver and a micro-controller.
Adding a micro-controller
atmega = cli.mcu(32, cli.architecture_family.avr, cli.avr_mcu.atmega328p, b'compiled/single_motor_control.hex')
Connecting the components via nodes.
In total we will need 36 nodes (32 for Atmega itself). How each of these nodes are connected is described in the code below :
Now we assign voltages to the nodes as needed :
# giving a value of 5v means that the button is pressed
In the start we have assumed that the button is pressed. we simulate This for first 0.5 seconds
>>>cli.execAll(0.00000025, 0.5)
Now we release the input button (which means the voltage on node 11 or pin 12 will go to zero).
>>>cli.putValue(11,0) #button is released
>>>cli.execAll(0.00000025, 0.5)
Now we apply the button pressed again.
Now if we plot the the speed of the motor as a function of time we see :
Comments
Post a Comment