summaryrefslogtreecommitdiffhomepage
path: root/digital/asserv/src/asserv/motor_model.host.c
blob: f87e7db6ea1e6f127074bd663e328c7b881bf51b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* motor_model.c - DC motor model. */
/* asserv - Position & speed motor control on AVR. {{{
 *
 * Copyright (C) 2006 Nicolas Schodet
 *
 * APBTeam:
 *        Web: http://apbteam.org/
 *      Email: team AT apbteam DOT org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */
#include "common.h"
#include "motor_model.host.h"

/**
 * Switching to french for all those non english speaking people.
 *
 * Ce fichier permet de modéliser un moteur à courant continue. Il y a deux
 * parties, la modélisation électrique et la modélisation mécanique.
 *
 * On peut trouver de l'aide sur :
 *  - le site de maxon : http://www.maxonmotor.com/
 *  - ici : http://www.mathworks.com/access/helpdesk/help/toolbox/control/getstart/buildmo4.html
 *  - là : http://iai.eivd.ch/users/mee/Regulation_automatique_anal.htm
 *
 * Voici ce qui en résulte, des belles équations différentielles :
 * u(t) = R i(t) + L di(t)/dt + 1/Ke o(t)
 * J do(t)/dt = Kt i(t) - Rf o(t)
 * dth(t)/dt = o(t)
 *
 * Les variables sont décrites dans la structure motor_t.
 *
 * À cela, il faut ajouter un coef pour le réducteur, je vous laisse ça en
 * exercice.
 *
 * On va résoudre ces belles équadiff numériquement par la méthode d'Euler (il
 * est partout celui là). Si vous voulez plus de détail, mailez moi. On arrive
 * à :
 *
 * i(t+h) = i(t) + h (1/L u(t) - R/L i(t) - 1/(Ke L) o(t))
 * o(t+h) = o(t) + h (i_G^2 ro_G)/J (Kt i(t) - Rf o(t))
 * th(t+h) = th(t) + h o(t)
 *
 * C'est consternant de simplicité non ?
 */

/** Make a simulation step. */
void motor_model_step (struct motor_t *m)
{
    double i_, o_, th_; /* New computed values. */
    double h; /* Infinitesimal step...  Well, not so infinite. */
    int d;
    d = m->d;
    h = m->h / d;
    /* Make several small steps to increase precision. */
    for (; d; d--)
      {
	/* Ah, the mystical power of computation. */
	i_ = m->i
	    + h * (1.0 / m->m.L * m->u
		   - m->m.R / m->m.L * m->i
		   - 1.0 / m->m.Ke / m->m.L * m->o);
	o_ = m->o
	    + h * m->m.i_G * m->m.i_G * m->m.ro_G / m->m.J
	    * (m->m.Kt * m->i - m->m.Rf * m->o);
	th_ = m->th + h * m->o;
	/* Test for limits overflow, I have no proof it works right for the
	 * moment, only suspicions. */
	if (th_ < m->m.th_min)
	  {
	    th_ = m->m.th_min;
	    o_ = 0.0;
	  }
	else if (th_ > m->m.th_max)
	  {
	    th_ = m->m.th_max;
	    o_ = 0.0;
	  }
	/* Ok, now store this step. */
	m->i = i_;
	m->o = o_;
	m->th = th_;
      }
    /* Damn!  It's finished yet! */
    m->t += m->h;
}