Simulating a motor in ngspice

 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 and implemented. 

Interface specification file 

The interface of the model has two input ports. one for voltage and another for torque. External torque applied on the motor is modeled as an external applied voltage. 

There is one output port for speed of the motor. The angular velocity (in rad/s) is modeled as output voltage (in V).    

The parameters for the model include : mechanical constant, electric constant, armature resistance, inductance, moment of inertia and rotary friction coefficient. The file is shown below : 

NAME_TABLE :
C_Function_name : c_motor
Spice_Model_Name : motor
Description : "code model for motor"

PORT_TABLE :
PORT_Name : volt_in speed
Description : "input port of motor" "voltage equivalent of the speed of a motor"
Direction : in out
Default_Type : v v
Allowed_Types : [v] [v]
Vector : no no
Vector_Bounds : - -
Null_Allowed : no no

PORT_TABLE :
PORT_Name : torque
Description : "torque can be given as an input voltage"
Direction : in
Default_Type : v
Allowed_Types : [v]
Vector : no
Vector_Bounds : -
Null_Allowed : no



PARAMETER_TABLE :
Parameter_Name : electric_cons mechanical_cons resistance
Description : "Electric constant" "torque constant" "armature resistance"
Data_Type : real real real
Default_Value : 1 1 1
Limits : - - -
Vector : no no no
Vector_Bounds : - - -
Null_Allowed : no no no



PARAMETER_TABLE :
Parameter_Name : inductance inertia friction
Description : "armature inductance" "moment of inertia" "friction constant"
Data_Type : real real real
Default_Value : 1 1 1
Limits : - - -
Vector : no no no
Vector_Bounds : - - -
Null_Allowed : no no no



 The model file. the cfunc.mod file looks as below : 

void c_motor(ARGS){
static double vin, t_pre, dt, speed_cache = 0, i_cache = 0, i;
static double torque;
static double speed;
static double Kt, Ke, R, L, Jm, Bm,k;
static double A1,B1,C1,A2,B2,C2;
Kt = PARAM(mechanical_cons);
Ke = PARAM(electric_cons);
R = PARAM(resistance);
L = PARAM(inductance);
Jm = PARAM(inductance);
Bm = PARAM(friction);
k = Kt;
vin = INPUT(volt_in);
if(TIME - t_pre > 0.000001){
dt = TIME - t_pre;

i = (vin - k * speed - R * i) * (dt / L) + i;//these two lines are 
                                        //the discretized equations 
speed = (-torque + k * i_cache - Bm * speed) * (dt / Jm) + speed;
i_cache = i;
t_pre = TIME;
speed_cache = speed;
i_cache = i;
}
OUTPUT(speed) = speed;
}

 Results :

I try to run my model with a pwm input and plot the results : 
the top voltage of pwm = 10V and I vary the duty cycle : 
  
 
50% duty cycle : 
 
25 % duty cycle : 

As can be seen the speed is dependent proportionally on the duty cycle of the input signal. 

 
 
This is part of a project that I am working on in order to enable simulations of embedded systems in ngspice. If you are interested in this project or in  simulations of embedded systems or think such simulations can be useful for your project do contact me. : 
 

Comments

Popular posts from this blog

Counting pulses in Arduino (without interrupts or loops)

Embedded system simulations using tessim