Posts

Showing posts from May, 2021

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

Why do we need feedback in a control system?? (And basic functioning of an integrator)

Image
 I still remember my introductory course on control systems. It was "not very well explained" to say the least. It was something which I learned as I needed to use these concepts in various advanced courses and projects. Just trying to share some of that. So why do we need a feedback in a control system ?? A plain simple answer might be to sense how much deviation the system has from the desired response. Which is 100% correct. But how does this error help the "controller" actually control the output ?? I will take the example of a simple integrating controller (modeled by 1/s) to try and explain the function of feedback.   Let us say I give my system some input reference (ref) and the system now gives me some output (out).  Now ideally the output should be equal to the reference however due to  a multitude of reasons(system non-ideality, noise, or load applied to the system) in most real world scenarios the input will not match the output. That is why need a fe...

Simulating serial communication in ngspice

Image
 Most systems today involve both analog and digital components, and digital components in a lot of cases would be communicating with each other via some form of serial communication interface (I2C, UART, SPI or any other). In such a scenario it might be useful to have a way to simulate these communications.  In this post I will be explaining an example of an ADC over SPI interface. For reference I will be taking the example of ADC081S021 which is an 8-bit analog to digital converter by Texas instruments. For simulation I will be using ngspice as a circuit simulator. Ngspice is an open source circuit simulation which allows you to build custom models of a device using the XSPICE extension. (for more details on ngspice and xspice checkout the ngspice page and user manual ).  Functional details of the ADC.  In this section I will be describing the details of the ADC and its communication protocol. For reference look at the figure below, it describes the timing of adc...