-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgyro.cpp
160 lines (123 loc) · 3.97 KB
/
gyro.cpp
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*******************************************************************
*
* DESCRIPTION: Gyro Navigation Avionics Atomic Model
*
* AUTHOR: Ken Edwards
*
* EMAIL: ken@kje.ca
*
* DATE: November 2009
*
*******************************************************************/
/** include files **/
#include "gyro.h"
#include "message.h" // class ExternalMessage, InternalMessage
#include "mainsimu.h" // MainSimulator::Instance().getParameter( ... )
#include <math.h>
#include <ctime>
/** public functions **/
/*******************************************************************
* Function Name: Gyro
* Description: Gyro constructor
********************************************************************/
Gyro::Gyro( const string &name )
: Atomic( name )
, port_in_power( addInputPort( "port_in_power" ) )
, port_out_accel( addOutputPort( "port_out_accel" ) )
//, gyro_time_delay( 0, 0, 0, 50 )
{
string gyro_output_period_tmp( MainSimulator::Instance().getParameter( description(), "gyro_output_period" ) ) ;
string gyro_spinup_period_tmp( MainSimulator::Instance().getParameter( description(), "gyro_spinup_period" ) ) ;
if (gyro_output_period_tmp != "")
{
gyro_output_period = gyro_output_period_tmp ;
}
if (gyro_spinup_period_tmp != "")
{
gyro_spinup_period = gyro_spinup_period_tmp ;
}
// set the initial acceleration of our Gyro.
gyro_accel_value = 0 ;
}
/*******************************************************************
* Function Name: initFunction
* Description: Do nothing when the Gyro starts, we wait for power.
********************************************************************/
Model &Gyro::initFunction()
{
this->passivate();
return *this ;
}
/*******************************************************************
* Function Name: externalFunction
* Description:
********************************************************************/
Model &Gyro::externalFunction( const ExternalMessage &msg )
{
int new_power_state = POWER_OFF ;
if( msg.port() == port_in_power )
{
new_power_state = static_cast < int > (msg.value());
if (new_power_state == POWER_ON && gyro_state == GYRO_STATE_OFF )
{
//we enter SPINUP mode
gyro_state = GYRO_STATE_SPINUP ;
// have to wait in spinup mode for a while
holdIn( active, gyro_spinup_period );
}
else if (new_power_state == POWER_OFF && ( gyro_state == GYRO_STATE_SPINUP || gyro_state == GYRO_STATE_ON) )
{
// now we are turned off
gyro_state = GYRO_STATE_OFF ;
passivate();
}
}
return *this;
}
/*******************************************************************
* Function Name: internalFunction
* Description:
********************************************************************/
Model &Gyro::internalFunction( const InternalMessage & )
//Model &Gyro::internalFunction( const InternalMessage &msg )
{
if ( GYRO_STATE_ON == gyro_state)
{
/* This is a phony way of making the Gyro update it's values
*
* NOTE THIS CODE WAS USED FOR GYRO ATOMIC MODEL TESTING
* */
//gyro_accel_value = gyro_accel_value + 1 ;
/* This is a phony way of making the Gyro update it's values
* NOTE THIS CODE WAS USED FOR WHEN GYRO WAS COUPLED INTO THE IRS MODEL
* */
if (rand()>RAND_MAX/2)
{
gyro_accel_value = -1 ;
}
else
{
gyro_accel_value = 1 ;
}
holdIn( active, gyro_output_period );
}
else if ( GYRO_STATE_SPINUP == gyro_state)
{
// we can now leave SPINUP mode for ON mode
gyro_state = GYRO_STATE_ON ;
holdIn( active, gyro_output_period );
}
return *this ;
}
/*******************************************************************
* Function Name: outputFunction
* Description:
********************************************************************/
Model &Gyro::outputFunction( const InternalMessage &msg )
{
if ( GYRO_STATE_ON == gyro_state)
{
sendOutput( msg.time(), port_out_accel, gyro_accel_value) ;
}
return *this ;
}