Академический Документы
Профессиональный Документы
Культура Документы
M
1
+
M
2
+
M
3
+
M
4
, where
M
i
is the
reaction torque of motor i due to shaft
acceleration and the blades drag.
Using Newtons second law and
neglecting shaft friction, we have
I
M
i
= b
2
i
+
M
i
,
where I
M
is the angular momentum of
the ith motor and b > 0 is a constant.
A quad rotor moves forward by
pitching. This motion is obtained by
increasing the speed of the rear motor
M
3
while reducing the speed of the
front motor M
1
. Likewise, roll motion
Figure 1. The quad-rotor control inputs. The mini rotorcraft is controlled by varying
the speeds of four electric motors. Each motor produces a thrust f
i
, and these thrusts
combine to generate the main thrust u =
4
i=1
f
i
. The difference between the front
rotor blade speed and the rear rotor blade speed produces a pitch torque. The roll
torque is produced similarly. The yaw torque is the sum of the torques of each motor
[see (1) and (2)].
M
4
M
1
M
2
M
3
f
4
f
3
l
u
f
1
f
2
M
2
M
3
M
1
M
4
Applying modern nonlinear control
theory can improve the performance
of the controller and enable tracking
of aggressive trajectories.
December 2005 47 IEEE Control Systems Magazine
is obtained using the lateral motors. Yaw motion is
obtained by increasing the torque of the front and rear
motors,
M
1
and
M
3
, respectively, while decreasing the
torque of the lateral motors,
M
2
and
M
4
. These motions
can be accomplished while keeping the total thrust
u = f
1
+ f
2
+ f
3
+ f
4
constant. In steady state, that is, when
i
= 0, the yaw torque is
= b
2
1
+
2
3
2
2
2
4
, (1)
and the main thrust is
u =k
4
i=1
2
i
. (2)
In view of its configuration, the quad rotor shares some
similarities with the PVTOL aircraft. Indeed, if the roll or
pitch and yaw angles are set to zero, the quad rotor
reduces to a PVTOL and can be viewed as two PVTOLs
connected with orthogonal axes.
In this article, we first model the dynamics of the
quad rotor. Then we propose a control strategy by view-
ing the aircraft as the interconnection of two PVTOLs.
The control algorithm is based on the nested saturation
control strategy introduced in [5]. Using computer simu-
lations, we compare the performance of the nonlinear
control algorithm with an linear qua-
dratic regulator (LQR) control law.
The controller is implemented on a
PC, and we present the results of lab-
oratory experiments. Finally, we pre-
sent some conclusions.
Dynamical Model
In this section, we derive a dynamical
model of the quad rotor. This model
is obtained by representing the air-
craft as a rigid body evolving in a 3-D
space due to the main thrust and
three torques. The main thrust u is
shown in Figure 1. The dynamics of
the four electric motors are fast and,
thus, are neglected.
The generalized coordinates of the
rotorcraft are
q = (x, y, z, , , ) R
6
,
where = (x, y, z) R
3
denotes the
position of the center of mass of the
helicopter relative to a fixed inertial
frame I, and = (, , ) R
3
are
the Euler angles, is the yaw angle around the z-axis, is
the pitch angle around the modified y-axis, and is the roll
angle around the modified x-axis [6][8], which represent
the orientation of the rotorcraft.
Define the Lagrangian
L(q, q) = T
trans
+ T
rot
U,
where T
trans
= (m/2)
,
where
W
sin 0 1
cos sin cos 0
cos cos sin 0
.
Figure 2. Simulation of the LQR control law applied to the linear system (22).
The initial conditions are altitude y(0) = 70 m and roll (0) = 0
. In this case, y
and converge to zero as expected.
2
1
0
1
2
3
4
5
d
/
d
t
(
/
s
)
0 5 10
Time (s)
15 20
(d)
0.5
0
0.5
1
1.5
2
)
Time (s)
(c)
0 5 10 15 20
0
5
10
15
20
d
y
/
d
t
(
m
/
s
)
0 5 10 15 20
Time (s)
(b)
70
60
50
40
30
20
10
0
y
(
m
)
0 5 10 15 20
Time (s)
(a)
December 2005 48 IEEE Control Systems Magazine
Define
J = J() = W
T
I W
,
so that
T
rot
=
1
2
T
J .
Thus, the matrix
J = J() acts as the
inertia matrix for the
full rotational kinetic
energy of the helicopter
expressed in terms of
the generalized coordi-
nates .
The model of the full
rotorcraft dynamics is
obtained from Euler-
Lagrange equations with
external generalized
forces
d
dt
L
q
L
q
=
,
where F
= R
F R
3
is
the translational force
applied to the rotorcraft
due to main thrust,
R
3
represents the yaw,
pitch, and roll moments,
and R denotes the rota-
tional matrix R(, , ) SO(3) representing the orienta-
tion of the aircraft relative to a fixed inertial frame. From
Figure 1, it follows that
F =
0
0
u
,
where u is the main thrust directed out the top of the air-
craft expressed as
u =
4
i=1
f
i
,
and, for i = 1, . . . , 4, f
i
is the force produced by motor M
i
,
as shown in Figure 1. Typically, f
i
=k
2
i
, where k is a con-
stant and
i
is the angular speed of the i th motor. We
assume that the center of gravity is located at the intersec-
tion of the line joining motors M
1
and M
3
and the line join-
ing motors M
2
and M
4
(see Figure 1). The generalized
torques are thus
Figure 3. Simulation of the LQR control law applied to the nonlinear subsystem (10) and (15).
The initial conditions are y(0) = 70 m and (0) = 0
/
d
t
(
/
s
)
Time (s)
0 5 10 15 20
(d)
140
120
100
80
60
40
20
0
20
)
0 5 10
Time (s)
(c)
15 20
Control parameter Value
a
z
1
0.001
a
z
2
0.002
a
1
2.374
a
2
0.08
b
1
2
b
2
1
b
3
0.2
b
4
0.1
b
1
2
b
2
1
b
3
0.2
b
4
0.1
T 17 ms
ms
Table 1. Parameter values used in the nonlinear control
laws (16), (17), (20), and (21). These parameters are
manually tuned to improve the performance of the
closed-loop system.
December 2005 49 IEEE Control Systems Magazine
=
4
i=1
M
i
(f
2
f
4
)
(f
3
f
1
)
,
where is the distance between the motors and the center
of gravity, and
M
i
is the moment produced by motor M
i
,
i = 1, . . . , 4, around the center of gravity of the aircraft.
Thus, we obtain
m
0
0
mg
=F
= R
F , (3)
J +C(, ) =, (4)
where
C(, ) =
J
1
2
(
T
J)
is the Coriolis term, which contains the gyroscopic and
centrifugal terms associated with the dependence of J.
Finally, we obtain from (3) and (4)
m x = usin, (5)
m y =ucos sin, (6)
m z =ucos cos mg, (7)
, (8)
, (9)
, (10)
where x and y are coordinates in the horizontal plane, z is
the vertical position, and
, and
by
=
= J
1
( C(, ) ).
Control Strategy
In this section, we develop a control strategy for stabilizing
the quad rotor at hover. The controller synthesis procedure
regulates each state sequentially using a priority rule as fol-
lows. We first use the main thrust u to stabilize the altitude
Figure 4. Simulation of the nonlinear control law (20) with the nonlinear subsystem (10) and (15). The initial conditions
are y(0) = 5 m, y(0) = 0 m/s,
(0) = 0
. The states y, y, ,
)
0 20 40
Time (s)
60 80 100
(c)
120
100
80
60
40
20
0
y
(
m
)
0 20 40 60 80 100
Time (s)
(a)
50
0
50
100
150
200
d
y
/
d
t
(
m
/
s
)
0 20 40 60 80 100
Time (s)
(b)
1
0
1
2
3
4
d
/
d
t
(
/
s
)
0 20 40
Time (s)
(d)
60 80 100
December 2005 50 IEEE Control Systems Magazine
of the rotorcraft. Next, we stabilize the yaw angle. We then
control the roll angle and the y displacement using a con-
troller designed for the PVTOL model [2], [10]. Finally, the
pitch angle and the x displacement are controlled.
The proposed control strategy is simple to implement
and easy to tune. The experimental setup is such that
the four control inputs can independently operate in
either manual or automatic modes. For flight safety rea-
sons, this feature is helpful for implementing the control
strategy. The quad-rotor helicopter can be operated in
semi-automatic mode, in which the remote pilot com-
mands only the altitude and the desired position, leaving
the orientation stabilization task to the control law.
Control of Altitude and Yaw
The vertical displacement z in (7) is controlled by forcing the
altitude to satisfy the dynamics of a linear system. Thus, we set
u = (r
1
+mg)
1
cos cos
, (11)
where r
1
is given by the proportional derivative (PD)
controller
r
1
a
z
1
z a
z
2
(z z
d
), (12)
where a
z
1
and a
z
2
are positive constants and z
d
is a posi-
tive constant representing the desired altitude. To control
yaw angle, we set
= a
1
a
2
(
d
). (13)
Introducing (11)(13) into (5)(8) and assuming
cos cos = 0, that is, , (/2, /2), we obtain
m x =(r
1
+mg)
tan
cos
, (14)
m y =(r
1
+mg) tan, (15)
z =
1
m
a
z
1
z a
z
2
(z z
d
)
, (16)
=a
1
a
2
(
d
). (17)
The control gains a
1
, a
2
, a
z
1
,and a
z
2
are positive constants
chosen to ensure stable, well-damped response of the quad
rotor. From (16) and (17) it follows that, if
d
and z
d
are con-
stants, then and z converge. Therefore,
and
0,
which, using (17), implies that
d
. Similarly, z z
d
.
Control of Lateral Position and Roll
We now determine the input
| 2 V,
|
| 2 V,
|
| 2 V,
we use the control strategy developed
in [5]. The nested saturation tech-
nique developed in [5] can exponen-
tially stabilize a chain of integrators
with bounded input. The amplitudes
of the saturation functions can be
chosen in such a way that, after a
finite time T
, the roll angle lies in the
interval 1 rad 1 rad. There-
fore, for t > T
| tan | < 0.54.
Thus, after sufficient time, r
1
is small
and the (y, ) subsystem reduces to
Figure 5. Simulation of the nonlinear control law with the nonlinear subsystem
(10) and (15). The initial conditions are y(0) = 200 m, y(0) = 0 m/s,
(0) = 0
/s,
and (0) = 40
) con-
verge to zero when the initial roll angular displacement and y position are very far
from the origin. Notice that the control strategy first brings the roll angle close to
zero and then carries the y position to the origin.
40
30
20
10
0
10
Time (s)
50 0
)
150 100
(c)
250
200
150
100
50
50
0
0
50
150 100
y
(
m
)
Time (s)
(a)
50 0 150 100
50
0
50
100
150
200
250
d
y
/
d
t
(
m
/
s
)
Time (s)
(b)
0.5
0
0.5
1.5
1
2
2.5
3
50
Time (s)
(d)
0 150 100
d
/
d
t
(
/
s
)
December 2005 51 IEEE Control Systems Magazine
y =g, (18)
, (19)
which represents four integrators in cascade.
For (18)(19), the nested saturation controller has the form
1
_
2
_
+
+
3
_
2 +
+
y
g
+
4
_
+3 +3
y
g
+
y
g
____
, (20)
where
a
is a saturation function of the form
a
(s) =
_
_
_
a s < a,
s a s a,
a s > a.
The closed loop is asymptotically stable (see [5]), and
therefore ,
.
Using a procedure similar to the one proposed for the roll
control, we obtain
1
_
2
_
+
+
3
_
2 +
x
g
+
4
_
+3 3
x
g
x
g
____
, (21)
and thus ,
x = A x + B u, (22)
where
A =
_
_
_
_
0 1 0 0
0 0 g 0
0 0 0 1
0 0 0 0
_
_
, B =
_
_
_
_
0
0
0
1
_
_
, u =
.
A state feedback control input is given by
u = K x, (23)
where K = R
1
B
T
P, and P is the unique, positive-
semidefinite solution to the algebraic Riccati equation.
Using the control input (23) into (22), we obtain
Figure 7. Experimental setup. The position/orientation sensor on board
the quad rotor is connected to a PC for feedback control. The PC generates
control inputs, which are sent to the helicopter through the radio link.
PC
Radio
RS-232
Antenna
Polhemus
Measuring Device
Manual/Automatic
Control Switches
Position / Orientation
Sensor
Figure 6. Real-time quad-rotor control plat-
form in autonomous hover. The control inputs
are sent to the helicopter through a radio link.
The wires attached to the rotorcraft provide
connections to the power supply and the atti-
tude sensor.
December 2005 52 IEEE Control Systems Magazine
x = ( A BK) x. (24)
Choosing
Q =
1 2 4 6
2 4 8 12
4 8 16 24
6 12 24 36
0. We
observed that the speed of convergence increases as the
amplitudes of the saturation functions increase. This trend is
due to the fact that larger control inputs are allowed. Figure
5 shows similar results when the initial conditions are far
from the desired position.
Simulation results show that, contrary to the LQR con-
troller, the nonlinear controller in (20) stabilizes the equi-
librium of subsystem (y, ) around the origin for initial
conditions far away from
the desired position.
Experimental
Results
Fi gure 6 shows the
quad-rotor platform in
autonomous hover. The
experimental platform
i s composed of a
Draganflyer helicopter, a
Futaba 72-MHz radio, a
Pentium II PC, and a 3-D
tracker system (Polhe-
mus) [11] for measuring
the position (x, y, z) and
orientation (, , ) of
the quad rotor. The Pol-
hemus is connected
through an RS232 link to
the PC (see Figure 7). The
remote control system
consists of a four-channel
Futaba FM hobby radio.
An electronic circuit
board in the helicopter
contains three gyros, four
pulsewidth modulation
(PWM) speed controllers,
a safety switch, and a
microprocessor that
mixes the pilots com-
mands to obtain the
appropriate rotor control
Figure 8. Unstable response of the quad rotor for the LQR control law applied to the (, y) sub-
system. (a) The dotted lines represent the desired trajectory for the initial conditions y(0) = 12
cm and (0) = 0
. The oscillations in the roll angle prevent the helicopter from taking off. In
this experiment, we use the same controller parameters as in the simulation presented in Figure
2. (b) The LQR gains are manually adjusted to improve the performance of the mini rotorcraft,
although the performance is inadequate for hovering.
(b)
25
20
15
10
5
0
5
10
15
20
25
y
(
c
m
)
10 15 20 25 30 35 40
Time (s)
)
20
15
10
5
0
5
10
10 15 20 25 30 35 40
Time (s)
(a)
15
10
5
0
y
(
c
m
)
5
10
15
20
15 20 25 30 35 40 45 50
Time (s)
20
15
10
5
)
0
5
10
Time (s)
15 20 25 30 35 40 45 50
December 2005 53 IEEE Control Systems Magazine
inputs. The radio and the PC are connected using Advantech
PCL-818HG and PCL-726 data-acquisition cards. To simplify
the experiments, each control input is independently switched
between automatic and manual control modes (see Figure 7).
The gyro stabilization introduces damping into the sys-
tem and enables the quad rotor to be controlled manually.
Without this gyro stabilization, it is almost impossible for a
pilot to control the quad rotor manually [12], [13]. However,
gyro stabilization, which represents only an angular speed
feedback, is not sufficient for autonomous hover; for hover
the quad rotor requires an attitude sensor, such as the Pol-
hemus sensor, and a control law based on angular dis-
placement feedback.
The control law requires the derivatives of the position
(x, y, z) and the orientation (, , ). These derivatives are
obtained numerically using the first-order approximation
q(t) (q(t) q(t T))/T, where T is the sampling period.
In all the experiments the position and orientation are pro-
vided by a Polhemus measuring device (see Figure 7).
LQR Control
Real-time experiments using the LQR control law are car-
ried out with manual altitude control, that is, u is given by
a pilot. To stabilize the system, we first implemented the
LQR gains from the simulation results.
Figure 8(a) shows the lateral position and roll orienta-
tion of the quad rotor. As can be seen, the roll angle of the
aircraft oscillates considerably, so that the helicopter can-
not hover. To reduce the oscillations, we modify the gains
to improve the performance. After numerous trials we sig-
nificantly reduced the oscillations, as shown in Figure 8(b).
Nevertheless, the obtained performance is not adequate to
perform autonomous hovering.
Nonlinear Control Scheme
To apply the nonlinear control algorithm (20) to the rotor-
craft, we place the aircraft in an arbitrary position, which
is (x, y, z) = (9, 12, 0) cm. The control objective is to make
the rotorcraft hover at an altitude of 20 cm, that is, we
Figure 9. Response of the quad rotor with position disturbances. The dotted lines represent the desired trajectory. The initial
conditions are (x, y, z) = (8.7, 12, 0) cm and (, , ) = (0
, 0
, 0
)
2.5
2
1.5
1
0.5
0
0.5
1
1.5
Time (s)
20 40 60 80 100 120 140
1
0.5
0
0.5
1
1.5
2
2.5
)
Time (s)
(d) (e) (f)
3
2
1
0
1
2
3
4
20 40 60 80 100 120 140
)
10
5
Time (s)
20 40 60 80 100 120 140
0
x
(
c
m
)
5
10
10
20
25
30
Disturbances
Time (s)
20 40 60 80 100 120 140
15
10
5
y
(
c
m
)
0
5
10
15
20
25
Time (s)
(a) (b) (c)
20 40 60 80 100 120 140
30
25
20
z
(
c
m
)
15
10
5
0
December 2005 54 IEEE Control Systems Magazine
wish to reach the position (x, y, z) = (0, 0, 20) cm while
(, , ) = (0
, 0
, 0
1
so that the roll angular velocity
2
in
such a way that the quad-rotor roll angle is sufficiently
small. In both cases, we avoid choosing high amplitude,
which normally leads to oscillations. The amplitude of
3
is chosen so that the effect of a small disturbance in
the horizontal speed y is soon compensated. Finally, the
amplitude of
4
is chosen such that y is kept close to
the desired position.
Figure 9 shows the performance obtained when we
introduce a disturbance manually on the x-axis of 25 cm
at time 125 s, five disturbances of 20 cm on the y-axis at
times 25 s, 80 s, 90 s, 120 s, and 130 s, two disturbances of
10 cm on the z-axis at times 80 s and 115 s, and a distur-
bance of +10 cm on the z-axis at time 130 s.
We also study the system response to aggressive per-
turbations of the roll angle. In this experiment, we first
apply a force manually to reach a roll angle of +10
. At 95 s,
we perturb the roll angle by 30
. As show in Figures 10
and 11, the response remains bounded.
Conclusions
We have presented a stabilization nonlinear control algo-
rithm for a mini rotorcraft with four rotors. The dynamic
model of the rotorcraft was obtained using a Lagrange
approach. The proposed control algorithm is based on a
Figure 10. Response of the quad rotor with roll disturbances. The initial conditions are (x, y, z) = (8, 12, 0) cm and
(, , ) = (0
, 0
, 0
). The dotted lines represent the desired setpoints. The roll angle is manually perturbed by +10
and 30
during the experiment. This experiment shows that the nonlinear control law can recover from large orientation perturbations.
x
(
c
m
)
20 40 60 80 100
Time (s)
14
12
10
8
6
4
2
0
2
4
(a)
Time (s)
50
45
z
(
c
m
)
40
35
30
25
20
15
10
5
0
20 40 60 80 100
(c)
20 40 60 80 100
Time (s)
(b)
y
(
c
m
)
30
20
10
0
10
20
30
40
20 40 60 80 100
Time (s)
4
3
)
2
1
0
1
2
(d)
20 40 60 80 100
Time (s)
1
0.5
0
0.5
1
1.5
2
2.5
3
)
(e)
20 40 60 80 100
Time (s)
(f)
30
15
10
5
0
5
15
10
20
25
)
Disturbances
December 2005 55 IEEE Control Systems Magazine
nested saturation control strategy, which is such that the
amplitude constraints on the control input are satisfied.
The control strategy was applied to the mini rotorcraft,
and the experimental results show that the controller per-
forms satisfactorily even when significant disturbances are
introduced into the system. Furthermore, experimental
results show that the proposed nonlinear controller per-
forms better than an LQR linear controller.
Acknowledgments
We are grateful for the financial support of the French Picardie
Region Council, DGA, and LAFMAA (CNRS-CONACYT).
References
[1] V. Gavrilets, I. Martinos, B. Mettler, and E. Feron, Control logic for
automated aerobatic flight of miniature helicopter, in Proc. AIAA,
Monterey, CA, Aug. 58, 2002, p. 4834.
[2] P. Castillo, R. Lozano, and A. Dzul, Modelling and Control of Mini-Fly-
ing Machines (Springer-Verlag Series in Advances in Industrial Con-
trol). New York: Springer-Verlag, 2005.
[3] I. Fantoni and R. Lozano, Control of Nonlinear Mechanical Underactu-
ated Systems (Communications and Control Engineering Series). New
York: Springer-Verlag, 2001.
[4] L. Marconi, A. Isidori, and A. Serrani, Autonomous vertical landing
on an oscillating platform: An internal-model based approach, Auto-
matica, vol. 38, no. 1, pp. 2132, 2002.
[5] A.R. Teel, Global stabilization and restricted tracking for multiple
integrators with bounded controls, Syst. Contr. Lett., vol. 18, no. 3, pp.
165171, 1992.
[6] T.S. Alderete, Simulator aero model implementation [Online],
NASA Ames Research Center, Moffett Field, CA. Available:
http://www.simlabs.arc.nasa.gov/library_docs/rt_sim_docs/Toms.pdf
[7] B. Etkin and L. Duff Reid, Dynamics of Flight. New York: Wiley, 1959.
[8] B.W. McCormick, Aerodynamics Aeronautics and Flight Mechanics.
New York: Wiley, 1995.
[9] H. Goldstein, Classical Mechanics, 2nd ed. (Addison Wesley Series
in Physics). Reading, MA: Addison-Wesley, 1980.
[10] J. Hauser, S. Sastry, and G. Meyer, Nonlinear control design for
slightly nonminimum phase systems: Application to V/STOL aircraft,
Automatica, vol. 28, no. 4, pp. 665679, 1992.
[11] Fastrack 3Space Polhemus Users Manual, Polhemus, Colchester,
VT, 2001.
[12] P. Wayner, Gyroscopes that dont spin make it easy to hover, NY
Times, Aug. 8, 2002 [Online]. Available: http://www.nytimes.com
[13] N. Sacco, How the Draganflyer flies, Rotory Mag., 2002 [Online].
Available: http://www.rctoys.com/pdf/draganflyer3_rotorymagazine. pdf
Pedro Castillo (castillo@hds.utc.fr) obtained the B.S.
degree in electromechanical engineering in 1997 from the
Instituto Tecnolgico de Zacatepec, Morelos, Mexico. He
received an M.Sc. degree in electrical engineering from the
Centro de Investigacin y de Estudios Avanzados (CINVES-
TAV), Mexico, in 2000, and a Ph.D. in automatic control from
the University of Technology of Compigne, France, in 2004.
His research interests include real-time control applications,
nonlinear dynamics and control, aerospace vehicles, vision,
and underactuated mechanical systems. He can be contact-
ed at Heudiasyc-UTC, UMR CNRS 6599, B.P. 20529, 60205
Compigne, France.
Rogelio Lozano received the B.S. degree in electronic engi-
neering from the National Polytechnic Institute of Mexico in
1975, the M.S. degree in electrical engineering from the Cen-
tro de Investigacin y de Estudios Avanzados (CINVESTAV),
Mexico, in 1977, and the Ph.D. in automatic control from Labo-
ratoire dAutomatique de Grenoble, France, in 1981. He joined
the Department of Electrical Engineering at the CINVESTAV,
Mexico, in 1981, where he worked until 1989. He was head of
the Section of Automatic Control from June 1985 to August
1987. He has held visiting positions at the University of New-
castle, Australia, NASA Langley Research Center, and Labora-
toire dAutomatique de Grenoble, France. Since 1990, he has
been CNRS research director at the University of Technology
of Compigne, France, and, since 1995, head of the CNRS
Laboratory Heudiasyc (Heuristique et Diagnostic des Sys-
tmes Complexes). His research interests are in adaptive con-
trol, passivity, nonlinear systems, underactuated mechanical
systems, and autonomous helicopters.
Alejandro Dzul received his B.S. degree in electronic engi-
neering in 1993 and his M.S. degree in electrical engineering
in 1997, both from Instituto Tecnolgico de La Laguna, Mex-
ico. He received the Ph.D. in automatic control from Univer-
sit de Technologie de Compigne, France, in 2002. Since
2003, he has been a research professor in the Electrical and
Electronic Engineering Department at Instituto Tecnolgico
de La Laguna. His current research interests are in nonlin-
ear dynamics and control and real-time control with appli-
cations to aerospace vehicles.
Figure 11. System response to a roll angle perturbation.
This closeup view corresponds to the roll angle in Figure 10.
With the nonlinear control law, the subsystem (10) and (15)
recovers from a roll perturbation of 30
.
5
90 91 92 93 94
Time (s)
95 96 97 98 99
0
5
10
)
15
25
20
B Programkod
Se nsta sida
25
accel.h
vol at i l e ui nt 16_t TX1cal , TX2cal , TY1cal , TY2cal , TX1, TX2, TY1, TY2;
vol at i l e ui nt 16_t Pr evi ous_Dut y_Cycl eX, Dut ycal X, Tact ual X;
vol at i l e ui nt 16_t Pr evi ous_Dut y_Cycl eY, Dut ycal Y, Tact ual Y;
vol at i l e ui nt 8_t accst at us;
vol at i l e ui nt 16_t accel , Ta, Tb, Tc, Td, Te, K;
ui nt 16_t get Xaccel ( ) ;
i nt get Yaccel ( ) ;
voi d cal i br at e( ) ;
voi d del ay( ) ;
voi d i ni t _accel ( ) ;
accel.c
#i ncl ude <avr / i o. h>
#i ncl ude <avr / si gnal . h>
#i ncl ude <avr / i nt er r upt . h>
#i ncl ude " accel . h"
/ *Funkt i on f r ber kni ng av accel er at i on i X l ed*/
ui nt 16_t get Xaccel ( ) {
accel =1;
Tact ual X = ( ui nt 16_t ) ( ( ( ui nt 32_t ) ( TX1cal *TX2) ) / TX2cal ) ;
Pr evi ous_Dut y_Cycl eX=( ( ui nt 32_t ) 65536*( TX1- Tact ual X) ) / TX2;
r et ur n accel ; / / Bl ev al dr i g r t t p ber kni ng av accel er at i on,
/ / dr f r anvnds Pr evi ous_Dut y_Cycl e i st l l et .
}
i nt get Yaccel ( ) {
accel =2;
Tact ual Y = ( ui nt 16_t ) ( ( ( ui nt 32_t ) ( TY1cal *TY2) ) / TY2cal ) ;
Pr evi ous_Dut y_Cycl eY=( ( ui nt 32_t ) 65536*( TY1- Tact ual Y) ) / TY2;
r et ur n accel ;
}
I SR( TI MER1_CAPT_vect ) {
cl i ( ) ;
i f ( accst at us==0) { / / Ti den Ta,
TCNT1 =0; / / Nol l st l l r knar en
TCCR1B ^= _BV( I CES1) ; / / ndr a avbr ot t st r i ggni ng
TI FR1 | = _BV( I CF1) ; / / Mst e r ensa ef t er man ndr at t r i gg
accst at us = 1;
}el se i f ( accst at us==1) {
TX1 = I CR1; / / Ta Tb vr det , vi l ket pga at t Ta r nol l
bl i r T1
TCCR1B ^= _BV( I CES1) ;
TI FR1 | = _BV( I CF1) ;
accst at us = 2;
}el se i f ( accst at us==2) {
TX2 = I CR1; / / Ta Tc vr det
TI FR1 | = _BV( I CF1) ;
PORTD | = _BV( PD7) ; / / st yr mux s man f r i n y- axel
accst at us = 3;
}el se i f ( accst at us==3) { / / Uppr epa samma pr oci dur
TCNT1 = 0;
TCCR1B ^= _BV( I CES1) ;
TI FR1 | = _BV( I CF1) ;
accst at us = 4;
}el se i f ( accst at us==4) {
TY1 = I CR1;
TCCR1B ^= _BV( I CES1) ;
TI FR1 | = _BV( I CF1) ;
accst at us = 5;
}el se i f ( accst at us==5) {
TY2 = I CR1;
TI FR1 | = _BV( I CF1) ;
PORTD &= ~_BV( PD7) ;
accst at us = 0;
}el se{
accst at us =0;
/ / Fel
}
sei ( ) ;
}
/ *Tar kal i br er i ngsvr de nr QuadCopt er n st r p mar ken ( i nnan
mot or er na st t s i gng) */
voi d cal i br at e( ) {
f or ( i nt n=0; n<10; n++) {
del ay( 0xFF) ;
}
get Xaccel ( ) ;
get Yaccel ( ) ;
TY1cal =TY1;
TY2cal =TY2;
TX1cal =TX1;
TX2cal =TX2;
Dut ycal X=Pr evi ous_Dut y_Cycl eX;
Dut ycal Y=Pr evi ous_Dut y_Cycl eY;
K = ( ui nt 16_t ) ( ( ui nt 32_t ) 4*( TX2cal *180) / TX2cal ) ;
}
/ *Godt yckl i gt l ng del ayf unkt i on*/
voi d del ay( i nt n) {
f or ( i nt i = 0; i < n; i ++) {
f or ( i nt q = 0; q < 10; q++)
asmvol at i l e( " nop" ) ;
}
}
/ *I ni t i er a accel er omet er */
voi d i ni t _accel ( ) {
DDRB &= ~_BV( PB0) ; / / X- accel i ngng
DDRD | = _BV( PD7) ; / / St yr ut gng
TCCR1B | = _BV( CS10) | _BV( I CNC1) ; / / I ni t i er a t i mer , 1x
pr escal er , Enabl e Noi se Cancel er , Capt ur e on Ri si ng- edge
TI MSK1 | = _BV( I CI E1) ; / / | _BV( OCI E1A) ; / / I nput Capt ur e
I nt er r upt Enabl e on I CP1
}
servo.h
vol at i l e ui nt 16_t ser vo1, ser vo2, ser vo3, ser vo4; / / Mi n= 0x4E ( 78) Max=
0x9C ( 156)
voi d i ni t _Ser vopul s( ) ;
servo.c
#i ncl ude <avr / i o. h>
#i ncl ude <avr / si gnal . h>
#i ncl ude <avr / i nt er r upt . h>
#i ncl ude " ser vo. h"
ui nt 8_t next ;
/ *I ni t i er a Ser vopul s*/
voi d i ni t _Ser vopul s( ) {
DDRC | = _BV( PC0) | _BV( PC1) | _BV( PC2) | _BV( PC3) ; / / Ser vout gngar ,
PC0. . 3
TCCR0B | = _BV( CS02) ; / / | _BV( CS00) ; / / Ti mer 0, 256x pr escal er
TI MSK0 | = _BV( OCI E0A) ; / / Enabl e t i mer 0 compar e i nt er r upt
TI FR0 | = _BV( OCF0A) ; / / Count er out put compar e A mat ch
next =1;
PORTC &= ~_BV( PC0) ;
ser vo1 =10; / / Mi n= 0x4E ( 78) Max= 0x9C ( 156)
ser vo2 =10;
ser vo3 =10;
ser vo4 =10;
sei ( ) ; / / Enabl e gl obal i nt er r upt
}
/ *Ser vopul s i nt er r upt r ut i n f r at t byt a ut gngar */
I SR( TI MER0_COMPA_vect ) {
cl i ( ) ;
swi t ch( next ) {
case 1: / / St t PC0 hg t i l l s nst a compar e p vr de
ser vo1
OCR0A = ser vo1;
PORTC | = _BV( PC0) ;
TCNT0=0;
br eak;
case 2: / / St t PC0 l g, PC1 hg t i l l s nst a compar e p
vr de ser vo2
OCR0A = ser vo2;
PORTC &= ~_BV( PC0) ;
PORTC | = _BV( PC1) ;
TCNT0=0;
br eak;
case 3: / / Uppr epa f r PC2
OCR0A = ser vo3;
PORTC &= ~_BV( PC1) ;
PORTC | = _BV( PC2) ;
TCNT0=0;
br eak;
case 4: / / Uppr epa f r PC3
OCR0A = ser vo4;
PORTC &= ~_BV( PC2) ;
PORTC | = _BV( PC3) ;
TCNT0=0;
br eak;
case 5: / / Al l a ut gngar l ga
OCR0A = 0xFF;
PORTC &= ~_BV( PC3) ;
TCNT0=0;
br eak;
def aul t :
OCR0A = 0xFF;
i f ( next >= 10) { / / Al l a ut gngar l ga i 5x255 count s
sen st ar t a om
next = 0;
}
TCNT0=0;
br eak;
}
next +=1;
sei ( ) ;
}
IR.h
vol at i l e ui nt 8_t i r ;
voi d i ni t _I R( ) ;
voi d get I R( ) ;
IR.c
#i ncl ude <avr / i o. h>
#i ncl ude <avr / si gnal . h>
#i ncl ude <avr / i nt er r upt . h>
#i ncl ude " I R. h"
#i ncl ude " accel . h"
/ *I ni t i er ar AD- omvandl ar en p PD0 i Fr ee Runni ng Mode*/
voi d i ni t _I R( ) {
DDRD &= ~_BV( PD0) ;
ADMUX | = _BV( REFS0) ;
ADCSRA | = _BV( ADEN) | _BV( ADSC) | _BV( ADATE) | 0x03;
/ / Pr escal er x128
}
/ *Lser av AD- r egi st r et ( 10 bi t ar ) */
voi d get I R( ) {
ui nt 16_t t emp=ADCL;
t emp| =ADCH<<8;
i r =t emp;
}
SPI.h
vol at i l e ui nt 16_t next SPI ; / / Sl ave var i abl es
vol at i l e ui nt 16_t degam, degbm, i r m, x, y, sent SPI ; / / Mast er var i abl es
voi d i ni t _SPI _sl ave( ) ;
voi d i ni t _SPI _mast er ( ) ;
voi d st ar t _t r ans( ) ;
SPI.c
#i ncl ude <avr / i o. h>
#i ncl ude <avr / si gnal . h>
#i ncl ude <avr / i nt er r upt . h>
#i ncl ude " SPI . h"
#i ncl ude " I R. h"
#i ncl ude " accel . h"
#i ncl ude " t wi . h"
/ / I ni t i at e Sl ave
voi d i ni t _SPI _sl ave( ) {
ui nt 8_t f aker ead;
DDRB | = _BV( PB4) ; / / MI SO out put
SPCR | = _BV( SPE) | _BV( SPI E) ; / / Enabl e SPI , Enabl e i nt er r upt
SPCR &= ~_BV( MSTR) ; / / Sl ave
f aker ead=SPSR;
f aker ead=SPDR;
next SPI =0;
SPDR=0x1;
}
/ / I ni t i at e Mast er
voi d i ni t _SPI _mast er ( ) {
ui nt 8_t f aker ead;
DDRB | = _BV( PB3) | _BV( PB5) | _BV( PB2) ; / / MOSI , SCK, SS out put
PORTB | = _BV( PB2) ; / / no t r ansmi ssi on, must be done f i r st
SPCR | = _BV( SPE) | _BV( MSTR) ; / / Enabl e SPI , set mast er
SPCR | = _BV( SPR0) | _BV( SPI E) ; / / 16x pr escal er , Enabl e
i nt er r upt
f aker ead=SPSR;
f aker ead=SPDR;
sent SPI =0;
}
/ *St ar t t r ansf er , st ar t ar ver f r i ng av al l mt dat a*/
voi d st ar t _t r ans( ) {
i f ( sent SPI ==0) {
PORTB &= ~_BV( PB2) ;
sent SPI =1;
SPDR=1;
}
}
/ *Avbr ot t shant er ar e f r at t l adda SPI - r egi st r et med ny i nf or mat i on*/
I SR( SPI _STC_vect ) {
cl i ( ) ;
i f ( 0x10&SPCR) { / / Mast er handl er
ui nt 8_t t emp = SPDR; / / Ladda i n mot t aget vr de
i f ( sent SPI ==1) {
SPDR=2;
sent SPI =2;
}el se i f ( sent SPI ==2) {
SPDR=3;
degam=t emp<<8;
sent SPI =3;
}el se i f ( sent SPI ==3) {
SPDR=4;
degam| =t emp;
sent SPI =4;
}el se i f ( sent SPI ==4) {
SPDR=5;
degbm=t emp<<8;
sent SPI =5;
}el se i f ( sent SPI ==5) {
SPDR=6;
degbm| =t emp;
sent SPI =6;
}el se i f ( sent SPI ==6) {
SPDR=7;
x=t emp<<8;
sent SPI =7;
}el se i f ( sent SPI ==7) {
SPDR=8;
x| =t emp;
sent SPI =8;
}el se i f ( sent SPI ==8) {
SPDR=9;
y=t emp<<8;
sent SPI =9;
}el se i f ( sent SPI ==9) {
y| =t emp;
sent SPI =0;
PORTB | = _BV( PB2) ;
}el se
t emp=SPDR; / / j ust do a f ake r ead;
}el se{ / / Sl ave handl er
next SPI =SPDR; / / Ladda i n val av nst a vr de ( f r
synkni ng)
i f ( next SPI ==1) {
SPDR = dega>>8;
}el se i f ( next SPI ==2) {
SPDR = 0xf f °a;
}el se i f ( next SPI ==3) {
SPDR = degb>>8;
}el se i f ( next SPI ==4) {
SPDR = 0xf f °b;
}el se i f ( next SPI ==5) {
SPDR = Pr evi ous_Dut y_Cycl eX>>8;
}el se i f ( next SPI ==6) {
SPDR = 0xf f &Pr evi ous_Dut y_Cycl eX;
}el se i f ( next SPI ==7) {
SPDR = Pr evi ous_Dut y_Cycl eY>>8;
}el se i f ( next SPI ==8) {
SPDR = 0xf f &Pr evi ous_Dut y_Cycl eY;
}el se i f ( next SPI ==9) {
SPDR=0x1;
}
}
sei ( ) ;
}
TWI.h
vol at i l e ui nt 16_t aof f set ;
vol at i l e ui nt 16_t bof f set ;
vol at i l e ui nt 16_t r ot a;
vol at i l e ui nt 16_t r ot b;
vol at i l e ui nt 8_t st at us, next TWCR, mem, t est ;
vol at i l e ui nt 16_t ol db, ol da, dega;
vol at i l e i nt degb;
voi d Get Rot ( ) ;
voi d Get Rot B( ) ;
voi d FakeWr i t e( ui nt 8_t adr , ui nt 8_t mem) ;
voi d Wr i t e8( ui nt 8_t adr , ui nt 8_t mem1, ui nt 8_t dat a) ;
ui nt 8_t Read8( ui nt 8_t adr , ui nt 8_t mem1) ;
voi d I ni t TWI ( voi d) ;
voi d Power UpGyr o( voi d) ;
voi d St ar t UpGyr o( voi d) ;
I SR( TWI _vect ) ;
TWI.c
#i ncl ude <avr / i o. h>
#i ncl ude <ut i l / del ay. h>
#i ncl ude <mat h. h>
#i ncl ude <avr / i nt er r upt . h>
#i ncl ude " t wi . h"
/ / Ber knar bda r ot at i onsr i kt ni ngar na
voi d Cal cRot ( ) {
dega=( ( ( r ot a>>1) +( ol da>>1) ) - aof f set ) ;
degb=( ( ( r ot b>>1) +( ol db>>1) ) - bof f set ) ;
}
/ / Ber kanar endast en r ot ai onsr i kt ni ng
voi d Cal cRot B( ) {
degb=( ( ( r ot b>>1) +( ol db>>1) ) - bof f set ) ;
}
/ / St ar t ar avl sni ng f r n gyr ot r est en avbr ot t shant er at . Vxl ar mel l an
at t hmt a f r n A el l er B
voi d Get Rot ( ) {
i f ( st at us == 0) {
i f ( mem== 0x00) {
mem= 0x02;
}el se{
mem = 0x00;
}
st at us = 1;
TWCR = ( 1<<TWEN) | ( 1<<TWSTA) | ( 1<<TWI NT) | ( 1<<TWI E) ;
}
}
/ / St ar t ar avl sni ng f r n gyr ot , men endast f r n r i kt ni ng B
voi d Get Rot B( ) {
i f ( st at us == 0) {
st at us = 1;
mem= 0x02;
TWCR = ( 1<<TWEN) | ( 1<<TWSTA) | ( 1<<TWI NT) | ( 1<<TWI E) ;
}
}
/ / Gr en l t sas skr i vni ng t i l l mi nnet f r at t f mi nnenspekar en p
r t t st l l e
voi d FakeWr i t e( ui nt 8_t adr , ui nt 8_t mem) {
TWCR = ( 1<<TWI NT) | ( 1<<TWSTA) | ( 1<<TWEN) ;
whi l e( ! ( TWCR & ( 1<<TWI NT) ) ) ;
TWDR = adr & 0xf e;
TWCR = ( 1<<TWI NT) | ( 1<<TWEN) ;
whi l e( ! ( TWCR & ( 1<<TWI NT) ) ) ;
TWDR = mem;
TWCR = ( 1<<TWI NT) | ( 1<<TWEN) ;
whi l e( ! ( TWCR & ( 1<<TWI NT) ) ) ;
}
/ / Skr i ver en byt e
voi d Wr i t e8( ui nt 8_t adr , ui nt 8_t mem1, ui nt 8_t dat a) {
FakeWr i t e( adr , mem1) ;
TWDR = dat a;
TWCR = ( 1<<TWI NT) | ( 1<<TWEN) ;
whi l e( ! ( TWCR & ( 1<<TWI NT) ) ) ;
TWCR = ( 1<<TWI NT) | ( 1<<TWSTO) | ( 1<<TWEN) ;
}
/ / Lser en byt e
ui nt 8_t Read8( ui nt 8_t adr , ui nt 8_t mem1) {
ui nt 8_t t emp;
FakeWr i t e( adr , mem1) ;
TWCR = ( 1<<TWI NT) | ( 1<<TWSTA) | ( 1<<TWEN) ;
whi l e( ! ( TWCR & ( 1<<TWI NT) ) ) ;
TWDR = adr | 0x01;
TWCR = ( 1<<TWI NT) | ( 1<<TWEN) ;
whi l e( ! ( TWCR & ( 1<<TWI NT) ) ) ;
TWCR = ( 1<<TWI NT) | ( 1<<TWEN) ;
whi l e( ! ( TWCR & ( 1<<TWI NT) ) ) ;
t emp = TWDR;
TWCR = ( 1<<TWI NT) | ( 1<<TWSTO) | ( 1<<TW, N) ;
r et ur n t emp;
}
/ / St ar t ar TWI bussen
voi d I ni t TWI ( voi d) {
/ / St t er TWI t i l l 100 kHz vi d AVR 20 MHz
TWBR = 0x17;
st at us = 0;
TWSR = ( 1<<TWPS0) ;
}
/ / Uppst ar t ssekvens f r gyr ot nr st r mmen ansl ut s
voi d Power UpGyr o( voi d) {
f or ( ui nt 8_t i = 0x00; i <0x10; i ++) {
Wr i t e8( 0xaf , i , ( Read8( 0xa1, i ) ) ) ;
}
Wr i t e8( 0xaf , 0x0f , 0x40) ;
}
/ / Uppst ar t och kal l i br er i ng av gyr ot ef t er Power Up
voi d St ar t UpGyr o( voi d) {
aof f set = 0xf f f f ;
bof f set = 0xf f f f ;
Wr i t e8( 0xaf , 0x0f , 0x41) ;
/ / Lt er gyr ot l ugna ner si g
_del ay_ms( 200) ;
_del ay_ms( 200) ;
whi l e( ! ( ( Read8( 0xaf , 0x0f ) ) & 0x02) ) ; / / Pol l ar gyr ot i f al l det
r r edo
/ / Kont r ol l er ar i f al l den f r r a whi l e l open gj or de r t t
whi l e( aof f set == 0xFFFF | bof f set == 0xFFFF) {
aof f set = ( Read8( 0xaf , 0x00) ) <<8;
aof f set | = Read8( 0xaf , 0x01) ;
bof f set = ( Read8( 0xaf , 0x02) ) <<8;
bof f set | = Read8( 0xaf , 0x03) ;
}
/ / kar hast i ghet en p bussen
TWBR = 0x13;
TWSR &= ~_BV( TWPS0) ;
}
/ / Avbr ot t shant er ar en somhant er ar avbr ot t et f r n TWI n
I SR( TWI _vect ) {
cl i ( ) ;
swi t ch( st at us) {
case 1: {/ / Ski ckar adr essen med skr i vi nst r ukt i onen t i l l
gyr ot
TWDR = 0xae;
st at us = 2;
next TWCR = 0;
}br eak;
case 2: {/ / Ski ckar var vi vi l l skr i va, det t a f r at t f
pekar en i mi nnet r t t nr vi ska l sa
TWDR = mem;
next TWCR = 0;
st at us = 3;
}br eak;
case 3: {/ / St ar t ar i gen
next TWCR = ( 1<<TWSTA) ;
st at us = 4;
}br eak;
case 4: {/ / Ski ckar adr essen med l si nst r ukt i onen t i l l
gyr ot
TWDR = 0xaf ;
next TWCR = 0;
st at us = 5;
}br eak;
case 5: {/ / Bekr f t ar acken och vnt ar p dat a
st at us = 6;
next TWCR = ( 1<<TWEA) ;
}br eak;
case 6: {/ / Lser f r st a byt en
ol da = r ot a;
ol db = r ot b;
i f ( mem== 0x00) {
r ot a = TWDR<<8;
}el se{
r ot b = TWDR<<8;
}
next TWCR = 0;
st at us = 7;
}br eak;
case 7: {/ / Lser andr a byt en
i f ( mem== 0x00) {
r ot a | = TWDR;
}el se{
r ot b | =TWDR;
}
next TWCR = ( 1<<TWSTO) ;
st at us = 0;
}br eak;
}
/ / Ski ckar nst a i nst r ukt i on
TWCR = ( 1<<TWI NT) | ( 1<<TWEN) | ( 1<<TWI E) | next TWCR;
sei ( ) ;
}
main.c (slave)
/ ****************/
/ *Sl ave Code */
/ * f or */
/ *QuadCopt er */
/ ****************/
#i ncl ude <avr / i o. h>
#i ncl ude <avr / si gnal . h>
#i ncl ude <avr / i nt er r upt . h>
#i ncl ude <st di o. h>
#i ncl ude " . . / l i b/ uar t . h"
#i ncl ude " . . / l i b/ accel . h"
#i ncl ude " . . / l i b/ I R. h"
#i ncl ude " . . / l i b/ ser vo. h"
#i ncl ude " . . / l i b/ SPI . h"
#i ncl ude " . . / l i b/ t wi . h"
/ * def i ne CPU f r equency i n Mhz her e i f not def i ned i n Makef i l e */
#i f ndef F_CPU
#def i ne F_CPU 20000000UL
#endi f
i nt mai n ( voi d) {
/ / I ni t i er i ng
/ / Uppst ar t f r al l t somanvnds av sl aven
i ni t _accel ( ) ;
i ni t _SPI _sl ave( ) ;
i ni t _I R( ) ;
I ni t TWI ( ) ;
/ / Uppst ar t av gyr o
Power UpGyr o( ) ;
del ay( 0xFFFF) ;
del ay( 0xFFFF) ;
St ar t UpGyr o( ) ;
sei ( ) ;
cal i br at e( ) ;
cl i ( ) ;
/ / Kal i br er i ng av gyr o
f or ( i nt i = 0; i <100; i ++) {
del ay( 0xf f f f ) ;
St ar t UpGyr o( ) ;
}
sei ( ) ;
cal i br at e( ) ;
uar t _put s( " Di gi pr oj & Regl er pr oj " ) ;
i nt i = 0;
i nt h = 0;
/ / Huvudpr ogr ammet
whi l e( 1) {
Get Rot ( ) ;
Cal cRot ( ) ;
i = get Yaccel ( ) ;
h = get Xaccel ( ) ;
get I R( ) ;
}
r et ur n 0;
}
main.c (master)
/ ****************/
/ *Mast er Code */
/ * f or */
/ *QuadCopt er */
/ ****************/
#i ncl ude <avr / i o. h>
#i ncl ude <avr / si gnal . h>
#i ncl ude <avr / i nt er r upt . h>
#i ncl ude " . . / l i b/ uar t . h"
#i ncl ude " . . / l i b/ SPI . h"
#i ncl ude " . . / l i b/ ser vo. h"
#i ncl ude " . . / l i b/ t wi . h"
vol at i l e ui nt 16_t xcal , xol d;
vol at i l e i nt cal c;
/ * def i ne CPU f r equency i n Mhz her e i f not def i ned i n Makef i l e */
#i f ndef F_CPU
#def i ne F_CPU 20000000UL
#endi f
/ * 57600 baud */
#def i ne UART_BAUD_RATE 57600
/ *
voi d uar t _hex( unsi gned char dat a) ;
voi d uar t _hexi nt ( i nt dat a) ;
voi d uar t _newl i ne( ) ;
*/
/ *
SPI t ar emot p:
Accel er met er var i abl er :
x, y
I R var i abl er :
i r m
Gyr ovar i abl er :
r ot am
r ot bm
r ot b
*/
/ / vol at i l e ui nt 16_t xcal ;
/ *Godt yckl i gt l ng del ayf unkt i on*/
voi d del ay( i nt n) {
f or ( i nt i = 0; i < n; i ++) {
f or ( i nt q = 0; q < 10; q++)
asmvol at i l e( " nop" ) ;
}
}
i nt mai n( voi d) {
uar t _i ni t ( UART_BAUD_SELECT( UART_BAUD_RATE, F_CPU) ) ;
/ / Akt i ver ar f unkt i oner
i ni t _SPI _mast er ( ) ;
i ni t _Ser vopul s( ) ;
I ni t TWI ( ) ;
Power UpGyr o( ) ;
/ / Del ay f r at t gyr ot ska l ugna si g
del ay( 0xFFFF) ;
del ay( 0xFFFF) ;
St ar t UpGyr o( ) ;
/ / Mer del ay f r gyr ot
f or ( i nt i = 0; i <100; i ++) {
del ay( 0xf f f f ) ;
St ar t UpGyr o( ) ;
}
/ / var i abl er f r at t f mot or st yr ni ngen at t st ar t a
i nt f i r st =1;
i nt second=1;
sei ( ) ;
uar t _put c( 12) ;
uar t _put s( " SPI - TEST" ) ;
uar t _newl i ne( ) ;
/ / Kont r ol l er ar s accel er omet er n l i gger i nomaccept abl a vr den
whi l e( x == 0xf f f f | x <= 0x5000) {
st ar t _t r ans( ) ;
uar t _hexi nt ( x) ;
uar t _newl i ne( ) ;
}
del ay( 0xf f f f ) ;
xcal = x;
/ / Pr ogr aml open
whi l e( 1) {
/ / Ber knar ser vopul sen t i l l mot or er na
i nt gyr o = degb>>8;
cal c = ( i nt ) ( xcal - x) >>6;
i nt u = cal c- gyr o;
/ / St t er pul sen t i l l 2 av mot or er na
ser vo3 = 90- u;
ser vo1 = 90+u;
Get Rot B( ) ;
Cal cRot B( ) ;
st ar t _t r ans( ) ;
/ / Skr i ver ut vr dena t i l l en PC
uar t _put s( " cal c: " ) ;
uar t _hexi nt ( cal c) ;
uar t _put s( " u: " ) ;
uar t _hexi nt ( u) ;
uar t _put s( " Gyr o: " ) ;
uar t _hexi nt ( gyr o) ;
uar t _put s( " Ser vo1: " ) ;
uar t _hexi nt ( ser vo1) ;
uar t _put s( " Ser vo3: " ) ;
uar t _hexi nt ( ser vo3) ;
uar t _newl i ne( ) ;
/ / St ar t up sekvens f r mot or er na
i f ( f i r st ) {
f or ( i nt i =30; i <150; i ++) {
ser vo1=i ;
ser vo3=i ;
f or ( i nt n=0; n<9000; n++) {
del ay( 0xf f f f ) ;
}
}
f i r st =0;
}
i f ( second) {
f or ( i nt i =150; i >30; i - - ) {
ser vo1=i ;
ser vo3=i ;
f or ( i nt n=0; n<9000; n++) {
del ay( 0xf f f f ) ;
}
}
f i r st =1;
second=0;
}
}
r et ur n 0;
}
/ *Uar t f unkt i oner */
voi d uar t _hex( unsi gned char dat a) {
unsi gned char aHex[ ] =
{' 0' , ' 1' , ' 2' , ' 3' , ' 4' , ' 5' , ' 6' , ' 7' , ' 8' , ' 9' , ' A' , ' B' , ' C' , ' D' , ' E' , ' F' };
uar t _put c( aHex[ ( ( dat a>>4) &0x0F) ] ) ;
uar t _put c( aHex[ ( dat a&0x0F) ] ) ;
}
voi d uar t _hexi nt ( i nt dat a) {
unsi gned char aHex[ ] =
{' 0' , ' 1' , ' 2' , ' 3' , ' 4' , ' 5' , ' 6' , ' 7' , ' 8' , ' 9' , ' A' , ' B' , ' C' , ' D' , ' E' , ' F' };
uar t _put c( aHex[ ( ( dat a>>12) &0x0F) ] ) ;
uar t _put c( aHex[ ( ( dat a>>8) &0x0F) ] ) ;
uar t _put c( aHex[ ( ( dat a>>4) &0x0F) ] ) ;
uar t _put c( aHex[ ( dat a&0x0F) ] ) ;
}
voi d uar t _newl i ne( ) {
uar t _put c( 10) ;
uar t _put c( 13) ;
}
C Fldesschema
Se nsta sida
41