Posts

Embedded system simulations using tessim

Image
 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. >>...

Simulating arduino and atmega

Image
 This is a simple update of the project I'm working. I am attempting to build a simulation model for an entire atmega mcu, that can be inegrated with ngspice (an open source circuit simulator). The idea to take the binary file in the .hex format , parse it to get the instructions and then execute the program instruction by instruction to get a response (similar to a processor emulator). In this post I am giving a demo of the work that has been done till now. I will in this post, demonstrate how I am able to get a pwm pulse from the MCU and use it to drive the motor. The system being simulated is as :  The c program used is as follows :  define F_CPU 1000000UL #include <xc.h> #include <avr/io.h> #include <util/delay.h> int main (){ DDRD = 0xFF ; TCCR0A = 0x81 ; TCCR0B = 0x01 ; OCR0A = 0xFE ; return 0 ; } From this I get a hex file using the xc8 compiler for avr. I use that file to get the set of instructions in the program a...

Counting pulses in Arduino (without interrupts or loops)

Image
 In this post, I will talk about how to count pulses in Arduino without using any interrupts or loops. This method can come in handy when the controller is handling a lot of task which can either cause it to miss some counts or introduce some unnecessary delays if some checks are included.  Even when using interrupts, it will have to interrupt a program in execution in order to increment a count. This will obviously cause delays for the rest of the program.  In this blog, I will be using the built in timer/counter module of Arduino for the purpose. More details about this module can be found in the atmega328p datasheet ( link ) (in chapter 14,15 and 16).  I will provide a basic introduction, mainly the three registers (TCCR0A, TCCR0B and TCNT0) that will be of use to us.  Introduction  A basic function of the timer counter is to (as the name suggests) "count". It increments the counter (the value in the TCNT0 register) whenever it encounters a clock. The cl...

Simulating a motor in ngspice

Image
 Motors and actuators are an integral component of any robotics project.  In this post I will describe how a simple pmdc motor can be simulated using xspice and ngspice.  Modelling of a PMDC motor A simple model of a pmdc motor is given below :  Model f a PMDC motor  The electrical characteristics (current voltage and back emf) can be modeled by the following equation :-   where Ra is the armature resistance, La is the inductance of the coil, Vs the applied voltage and Vb is the back emf of the motor.  The mechanical characteristics of the motor are :-    Jm is the moment of inertia, Bm is the friction constant , Tm is the torque applied by the motor and TL is the external load torque.    In the above two equations we can substitute :    Vb = k w(t) Tm = k i(t) where w(t) is the angular velocity and i is the armature current of the motor. In order to model these in xspice code model these equations were discretized a...

Simulating embedded systems response, similar to tht of an MCU

Image
 A feedback controller in most cases would be implemented digitally inside a micro-controller. In a lot of cases it can be hard to take into account the effects of ADC sampling and delays caused by the processing inside the micro-controller.  In this blog  I demonstrate how these effects can be effectively simulated. I take the example of ATmega328p (the MCU present in Arduino uno) and simulate the response when it is using ADC readings to drive a PWM output(ADC reading determines the duty cycle o the PWM). I will not be simulating all the registers and load a binary (something which soft-wares like micro-chip studio do). I will be creating a simulation which gives the response similar to the one one would have when implemented in the MCU).  I will be using ngspice as the software for simulations and an Xspice code model will be created for the purpose of simulation.  First I show the interface specification file of the model. NAME_TABLE : C_Function_name : ...