LRAUV  revA
OffshoreEnvelope_Test.h
Go to the documentation of this file.
1 
9 #ifndef _OFFSHOREENVELOPE_TEST_H_
10 #define _OFFSHOREENVELOPE_TEST_H_
11 
12 #include "OffshoreEnvelope.h"
13 #include "OffshoreEnvelopeIF.h"
14 
19 #include "data/Slate.h"
21 #include "io/FileOutStream.h"
22 #include "io/NavChartDb.h"
23 #include "logger/LogEngine.h"
24 #include "logger/LoggerRules.h"
25 #include "logger/TextLogWriter.h"
28 #include "module/Config.h"
29 #include "units/Units.h"
30 
31 #include <cxxtest/TestSuite.h>
32 
38 class OffshoreEnvelope_Test : public CxxTest::TestSuite
39 {
40 public:
41 
43 
44  // Ensures the syslog is empty at the end of the run.
45  virtual void tearDown( void )
46  {
47 
48  FileOutStream stdOutStream( stdout );
49  syslogEngine_.addAssociation( new TextLogWriter( stdOutStream ),
51  syslogEngine_.processQueue( Timespan::ZERO_TIMESPAN );
52  }
53 
54  // run test
55  // We start with the vehicle close to shore, heading towards shore.
56  // We expect the behavior to turn the vehicle around.
57  // We run 100 cycles, or about 40 seconds.
58  // At the end, we expect the vehicle to be further wes than where it started.
59  virtual void testRun( void )
60  {
61  // Make sure NavChart is set up...
62  NavChartDb navChartDb;
63  // Dump the critical errors from setting up navchartDb with no real config variables
64  syslogEngine_.processQueue( Timespan::ZERO_TIMESPAN );
65  // Fake the chartsCfg
66  Str chartsCfg( "US1WC07M,US2WC11M,US3CA52M,US4CA60M,US5CA50M,US5CA61M,US5CA62M,US5CA83M" );
67  navChartDb.setChartsCfg( chartsCfg );
68  navChartDb.initialize();
69  int maxLoops = 1000000;
70  while( !navChartDb.ready() && maxLoops > 0 )
71  {
72  navChartDb.execute();
73  --maxLoops;
74  }
75 
76  TS_ASSERT_LESS_THAN( 0, maxLoops );
77 
78  // Fake a mission...
80  BehaviorRegistry::Insert( OffshoreEnvelopeIF::NAME, &OffshoreEnvelope::CreateBehavior, NULL );
81  TestAsyncComponent testComponent( "OffshoreEnvelope_TestComponent" );
82 
83  // Build up the behavior as if it were expressed in XML
84  MissionNode offshoreEnvelopeNode( "Guidance:OffshoreEnvelope" );
85  offshoreEnvelopeNode.setAttribute( "RunIn", "parallel" );
86  MissionNode* minOffshoreSettingNode( offshoreEnvelopeNode.appendChild( "Setting" ) );
87  minOffshoreSettingNode->appendChild( "Guidance:OffshoreEnvelope.minOffshore" );
88  minOffshoreSettingNode->appendChild( "Units:meter" );
89  minOffshoreSettingNode->appendChild( "Value" )->appendChildText( "1000" ); // 1 km
90  MissionNode* maxOffshoreSettingNode( offshoreEnvelopeNode.appendChild( "Setting" ) );
91  maxOffshoreSettingNode->appendChild( "Guidance:OffshoreEnvelope.maxOffshore" );
92  maxOffshoreSettingNode->appendChild( "Units:meter" );
93  maxOffshoreSettingNode->appendChild( "Value" )->appendChildText( "100000" ); // 100 km
94 
95  // Convert the virtual XML to a behavior
96  MissionItem* offshoreEnvelopeMissionItem( MissionItem::Instance( NULL, &offshoreEnvelopeNode, 0, testComponent.getLogger() ) );
97  OffshoreEnvelope* offshoreEnvelopePtr = ( OffshoreEnvelope* )( offshoreEnvelopeMissionItem->getComponent() );
98  OffshoreEnvelope& offshoreEnvelope( *offshoreEnvelopePtr );
99 
100  // Initialize the behavior
101  offshoreEnvelope.initialize();
102 
104  DataReader* readerControlHeadingMode = testComponent.newDataReader( HorizontalControlIF::HORIZONTAL_MODE );
105  DataReader* readerControlHeading = testComponent.newDataReader( HorizontalControlIF::HEADING_CMD );
106  DataReader* readerControlSpeed = testComponent.newDataReader( SpeedControlIF::SPEED_CMD );
107 
108  // Universals
109  UniversalDataWriter* writerLatitudeFix = testComponent.newUniversalWriter( UniversalURI::LATITUDE_FIX, Units::DEGREE, 1.0 );
110  UniversalDataWriter* writerLatitude = testComponent.newUniversalWriter( UniversalURI::LATITUDE, Units::DEGREE, 1.0 );
111  UniversalDataWriter* writerLongitude = testComponent.newUniversalWriter( UniversalURI::LONGITUDE, Units::DEGREE, 1.0 );
113 
114  // Universals...
115  // This should send the vehicle offshore
116  double latitude = D2R( 36.8 );
117  double longitude = D2R( -121.8 );
118  float orientation = M_PI_2;
119 
120  // Interrogate nav chart
121  writerLatitude->write( Units::RADIAN, latitude );
122  writerLongitude->write( Units::RADIAN, longitude );
123  NavChart navChart( NULL );
124  navChart.initialize();
125  writerLatitudeFix->write( Units::RADIAN, latitude );
126  for( int i = 0; i < 1000; ++i )
127  {
128  navChart.execute();
129  navChartDb.execute();
130  }
131 
132  // Let's run a loop
133  float period = 0.4;
134  float yaw_step = M_PI / 36 * period;
135  int horizontalMode( HorizontalControlIF::NONE );
136  float speed( 0.0 );
137  float heading( M_PI_2 );
138 
139  for( int i = 0; i < 100; ++i )
140  {
141  writerLatitude->write( Units::RADIAN, latitude );
142  writerLongitude->write( Units::RADIAN, longitude );
143  writerOrientation->write( Units::RADIAN, orientation );
144 
145  navChart.execute();
146  offshoreEnvelope.execute();
147 
148  bool readOk = readerControlHeadingMode->read( Units::ENUM, horizontalMode );
149  TS_ASSERT( readOk );
150  TS_ASSERT_EQUALS( horizontalMode, ( int )HorizontalControlIF::HEADING );
151 
152  readOk = readerControlSpeed->read( Units::METER_PER_SECOND, speed );
153  TS_ASSERT( readOk );
154  TS_ASSERT_LESS_THAN( 0.0, speed );
155 
156  readOk = readerControlHeading->read( Units::RADIAN, heading );
157  TS_ASSERT( readOk );
158  TS_ASSERT_DIFFERS( heading, M_PI_2 );
159 
160  if( heading != orientation )
161  {
162  float diff = fabs( heading - orientation );
163  if( diff > yaw_step )
164  {
165  float step = diff / ( heading - orientation ) * yaw_step;
166  orientation += step;
167  }
168  else
169  {
170  orientation = heading;
171  }
172  }
173  Location::AtBearing( orientation, speed * period, latitude, longitude );
174  }
175 
176  // cleanup
177  offshoreEnvelope.uninitialize();
178  delete offshoreEnvelopeMissionItem;
180  // the test!
181  TS_ASSERT_LESS_THAN( longitude, D2R( -121.80 ) );
182 
183  navChart.uninitialize();
184  navChartDb.uninitialize();
185  }
186 
187 };
188 
189 #endif // _OFFSHOREENVELOPE_TEST_H
MissionNode * appendChildText(const char *text)
Definition: MissionNode.cpp:227
static const Timespan ZERO_TIMESPAN
Constant value to represent instant of time.
Definition: Timestamp.h:255
Contains the AsyncComponent class definition.
static MissionItem * Instance(MissionItem *parent, MissionNode *node, unsigned int tablePos, Logger &logger)
Returns an instance, or NULL if an instance can't be returned.
Definition: MissionItem.cpp:69
Units tests for OffshoreEnvelope class.
Definition: OffshoreEnvelope_Test.h:38
void initialize(void)
Initialize function.
Definition: NavChart.cpp:38
bool setAttribute(const char *name, const char *value)
Definition: MissionNode.cpp:168
UniversalDataWriter * newUniversalWriter(const UniversalURI &universalURI, const Unit &accuracyUnit=Units::NONE, const float accuracy=DataElement::NO_ACCURACY, Logger::TimePrecisionType timePrecision=Logger::TIME_PRECISION_MILLIS)
Definition: Component.cpp:656
Wraps a Behavior or Aggregate with mission-specific settings pertaining to that item.
Definition: MissionItem.h:31
virtual void execute(void)
Definition: Component.cpp:124
static const UniversalURI LATITUDE_FIX
latitude_fix, as internally defined as a location measurement obtained by an absolute positioning sen...
Definition: UniversalURI.h:153
static const UniversalURI PLATFORM_ORIENTATION
platform_orientation, as defined by CF-metadata The platform orientation is the direction in which th...
Definition: UniversalURI.h:278
void initialize(void)
Initialize function.
Definition: OffshoreEnvelope.cpp:56
virtual bool write(const Unit &unit, const unsigned char value, const Timestamp &timestamp=Timestamp::NOT_SET_TIME)
Sets the Component's DataValue, with an optional timestamp.
Definition: DataWriter.cpp:71
Contains the BehaviorRegistry class declaration.
void setChartsCfg(const Str chartsCfg)
Definition: NavChartDb.cpp:172
A DataAccessor that reads values from the Slate Contains a default dataValue that must not be NULL...
Definition: DataReader.h:30
Contains the various /ref logging Rule class definitions that are used by LogEngine to determine whic...
#define D2R(x)
A macro for converting degrees to radians.
Definition: AuvMath.h:40
A DataAccessor that writes values to the Slate via its associated DataElement.
Definition: UniversalDataWriter.h:24
virtual void uninitialize()
Provide a space for uninitialization.
Definition: NavChartDb.cpp:668
Contains the MissionItem class declaration.
Contains the TextLogWriter class definition.
LogEngine syslogEngine_
Definition: OffshoreEnvelope_Test.h:42
Contains the Config class definition.
Wraps a c FILE* handle with an OutStream class.
Definition: FileOutStream.h:26
DataReader * newDataReader(const DataURI &dataURI)
Definition: Component.cpp:592
Definition: HorizontalControlIF.h:74
Reads a standard electronic navigation chart (ENC) in S-57 format, stores it in a database...
Definition: NavChartDb.h:30
static const BaseUnit METER_PER_SECOND
Definition: Units.h:86
static void Insert(const Str &baseName, BehaviorCreator behaviorCreator, const Module *module)
Add the base name and BehaviorCreator to the map.
Definition: BehaviorRegistry.cpp:31
Definition: LogEntry.h:44
virtual void initialize()
Provide a space for initialization before being run.
Definition: NavChartDb.cpp:177
static const BaseUnit RADIAN
Definition: Units.h:60
static void Uninitialize()
Uninitializes the map.
Definition: BehaviorRegistry.cpp:21
A logging Rule which matches against a given LogEntry::Type.
Definition: LoggerRules.h:147
virtual void tearDown(void)
Definition: OffshoreEnvelope_Test.h:45
static Behavior * CreateBehavior(const Str &prefix, const Module *module)
Mission Component factory interface.
Definition: OffshoreEnvelope.cpp:259
Wraps a TiXmlNode.
Definition: MissionNode.h:20
Replacement for standard template class string.
Definition: Str.h:12
Component responsible for handling the contents of the LogQueue.
Definition: LogEngine.h:63
Contains the OffshoreEnvelope class declaration.
Contains the LogEngine class definition.
Specifies the interface details for the SpeedControl component in the ControlModule.
bool read(const Unit &unit, unsigned char &readTo)
Get functions.
Definition: DataReader.cpp:82
virtual void testRun(void)
Definition: OffshoreEnvelope_Test.h:59
static const UniversalURI LONGITUDE
longitude is positive eastward; its units of degree_east (or equivalent) indicate this explicitly...
Definition: UniversalURI.h:169
virtual LogAssociation * addAssociation(LogWriter *writer, Rule *rule)
Add an entry to the LoggerAssociations table.
Definition: LogEngine.cpp:125
void uninitialize(void)
Uninit function.
Definition: NavChart.cpp:106
Contains the Units class declaration.
Contains the MissionNode class definition.
Definition: HorizontalControlIF.h:76
MissionNode * appendChild(const char *name)
Definition: MissionNode.cpp:219
Over simplified NavChart component.
Definition: NavChart.h:32
void processQueue(const Timespan &timeout)
Pop log entries from the queue and deal with them as appropriate.
Definition: LogEngine.cpp:59
Contains the OffshoreEnvelope Behavior/Command.
Definition: OffshoreEnvelope.h:35
Contains the DataWriter class definition.
static const BaseUnit ENUM
Definition: Units.h:229
Logger & getLogger(void)
Definition: Component.h:389
static void AtBearing(const float bearing, const float distance, double &lat, double &lon)
Definition: Location.cpp:348
static void Initialize()
Initializes the map.
Definition: BehaviorRegistry.cpp:15
static const Unit DEGREE
Definition: Units.h:132
Dummy implementation of AsyncComponent for testing purposes.
Definition: TestAsyncComponent.h:19
static const UniversalURI LATITUDE
latitude is positive northward; its units of degree_north (or equivalent) indicate this explicitly...
Definition: UniversalURI.h:147
bool ready()
Definition: NavChartDb.h:66
Contains the FileOutStream class declaration.
Defines a simple LogWriter that takes LogEntries and serializes them as text values to a stream...
Definition: TextLogWriter.h:25
Behavior * getComponent() const
Returns the mission component.
Definition: MissionItem.h:49
Specifies the interface details for the OffshoreEnvelope component in the GuidanceModule.
void uninitialize(void)
Uninit function.
Definition: OffshoreEnvelope.cpp:251
Contains the Slate class definition.