ADD EventQueue for static_scheduling
This commit is contained in:
		| @@ -72,6 +72,39 @@ static void test_bike_system() { | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // test_bike_system_event_queue handler function | ||||||
|  | static void test_bike_system_event_queue() { | ||||||
|  |     // create the BikeSystem instance | ||||||
|  |     static_scheduling::BikeSystem bikeSystem; | ||||||
|  |  | ||||||
|  |     // run the bike system in a separate thread | ||||||
|  |     Thread thread; | ||||||
|  |     thread.start(callback(&bikeSystem, &static_scheduling::BikeSystem::startWithEventQueue)); | ||||||
|  |  | ||||||
|  |     // let the bike system run for 20 secs | ||||||
|  |     ThisThread::sleep_for(20s); | ||||||
|  |  | ||||||
|  |     // stop the bike system | ||||||
|  |     bikeSystem.stop(); | ||||||
|  |  | ||||||
|  |     // check whether scheduling was correct | ||||||
|  |     // Order is kGearTaskIndex, kSpeedTaskIndex, kTemperatureTaskIndex, | ||||||
|  |     //          kResetTaskIndex, kDisplayTask1Index, kDisplayTask2Index | ||||||
|  |     // When we use the event queue, we do not check the computation time | ||||||
|  |     constexpr std::chrono::microseconds taskPeriods[] = { | ||||||
|  |         800000us, 400000us, 1600000us, 800000us, 1600000us, 1600000us}; | ||||||
|  |  | ||||||
|  |     // allow for 2 msecs offset (with EventQueue) | ||||||
|  |     uint64_t deltaUs = 2000; | ||||||
|  |     for (uint8_t taskIndex = 0; taskIndex < advembsof::TaskLogger::kNbrOfTasks; | ||||||
|  |          taskIndex++) { | ||||||
|  |         TEST_ASSERT_UINT64_WITHIN( | ||||||
|  |             deltaUs, | ||||||
|  |             taskPeriods[taskIndex].count(), | ||||||
|  |             bikeSystem.getTaskLogger().getPeriod(taskIndex).count()); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
| static utest::v1::status_t greentea_setup(const size_t number_of_cases) { | static utest::v1::status_t greentea_setup(const size_t number_of_cases) { | ||||||
|     // Here, we specify the timeout (60s) and the host test (a built-in host test or the |     // Here, we specify the timeout (60s) and the host test (a built-in host test or the | ||||||
|     // name of our Python file) |     // name of our Python file) | ||||||
| @@ -81,7 +114,10 @@ static utest::v1::status_t greentea_setup(const size_t number_of_cases) { | |||||||
| } | } | ||||||
|  |  | ||||||
| // List of test cases in this file | // List of test cases in this file | ||||||
| static Case cases[] = {Case("test bike system", test_bike_system)}; | static Case cases[] = { | ||||||
|  |     Case("test bike system", test_bike_system), | ||||||
|  |     Case("test bike system with event queue", test_bike_system_event_queue) | ||||||
|  | }; | ||||||
|  |  | ||||||
| static Specification specification(greentea_setup, cases); | static Specification specification(greentea_setup, cases); | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										3
									
								
								main.cpp
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								main.cpp
									
									
									
									
									
								
							| @@ -27,7 +27,8 @@ int main() { | |||||||
|     //        tr_debug("blink"); |     //        tr_debug("blink"); | ||||||
|     //    } |     //    } | ||||||
|     static_scheduling::BikeSystem bikeSystem; |     static_scheduling::BikeSystem bikeSystem; | ||||||
|     bikeSystem.start(); |     // bikeSystem.start(); | ||||||
|  |     bikeSystem.startWithEventQueue(); | ||||||
| } | } | ||||||
|  |  | ||||||
| #endif  // MBED_TEST_MODE | #endif  // MBED_TEST_MODE | ||||||
|   | |||||||
| @@ -53,6 +53,9 @@ static constexpr std::chrono::milliseconds kTemperatureTaskComputationTime	 = 10 | |||||||
| static constexpr std::chrono::milliseconds kDisplayTask2Period 				 = 1600ms; | static constexpr std::chrono::milliseconds kDisplayTask2Period 				 = 1600ms; | ||||||
| static constexpr std::chrono::milliseconds kDisplayTask2Delay 				 = 1200ms; | static constexpr std::chrono::milliseconds kDisplayTask2Delay 				 = 1200ms; | ||||||
| static constexpr std::chrono::milliseconds kDisplayTask2ComputationTime   	 = 100ms; | static constexpr std::chrono::milliseconds kDisplayTask2ComputationTime   	 = 100ms; | ||||||
|  | static constexpr std::chrono::milliseconds kCPUTaskPeriod 					 = 1600ms; | ||||||
|  | static constexpr std::chrono::milliseconds kCPUTaskDelay 					 = 0ms; | ||||||
|  | static constexpr std::chrono::milliseconds kCPUTaskComputationTime			 = 0ms; | ||||||
|  |  | ||||||
|  |  | ||||||
| BikeSystem::BikeSystem() : | BikeSystem::BikeSystem() : | ||||||
| @@ -109,6 +112,54 @@ void BikeSystem::start() { | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | void BikeSystem::startWithEventQueue() { | ||||||
|  |  | ||||||
|  |     tr_info("Starting Super-Loop with event handling"); | ||||||
|  |  | ||||||
|  |     init(); | ||||||
|  |  | ||||||
|  |     EventQueue eventQueue; | ||||||
|  |  | ||||||
|  | 	Event<void()> gearEvent(&eventQueue, callback(this, &BikeSystem::gearTask)); | ||||||
|  | 	gearEvent.delay(kGearTaskDelay); | ||||||
|  | 	gearEvent.period(kGearTaskPeriod); | ||||||
|  | 	gearEvent.post(); | ||||||
|  |  | ||||||
|  |     Event<void()> speedDistanceEvent(&eventQueue, callback(this, &BikeSystem::speedDistanceTask)); | ||||||
|  |     speedDistanceEvent.delay(kSpeedDistanceTaskDelay); | ||||||
|  |     speedDistanceEvent.period(kSpeedDistanceTaskPeriod); | ||||||
|  |     speedDistanceEvent.post(); | ||||||
|  |  | ||||||
|  |     Event<void()> display1Event(&eventQueue, callback(this, &BikeSystem::displayTask1)); | ||||||
|  |     display1Event.delay(kDisplayTask1Delay); | ||||||
|  |     display1Event.period(kDisplayTask1Period); | ||||||
|  |     display1Event.post(); | ||||||
|  |  | ||||||
|  |     Event<void()> resetEvent(&eventQueue, callback(this, &BikeSystem::resetTask)); | ||||||
|  |     resetEvent.delay(kResetTaskDelay); | ||||||
|  |     resetEvent.period(kResetTaskPeriod); | ||||||
|  |     resetEvent.post(); | ||||||
|  |  | ||||||
|  |     Event<void()> temperatureEvent(&eventQueue, callback(this, &BikeSystem::temperatureTask)); | ||||||
|  |     temperatureEvent.delay(kTemperatureTaskDelay); | ||||||
|  |     temperatureEvent.period(kTemperatureTaskPeriod); | ||||||
|  |     temperatureEvent.post(); | ||||||
|  |  | ||||||
|  |     Event<void()> display2Event(&eventQueue, callback(this, &BikeSystem::displayTask2)); | ||||||
|  |     display2Event.delay(kDisplayTask2Delay); | ||||||
|  |     display2Event.period(kDisplayTask2Period); | ||||||
|  |     display2Event.post(); | ||||||
|  |  | ||||||
|  | 	#if !defined(MBED_TEST_MODE) | ||||||
|  |   		Event<void()> cpuEvent(&eventQueue, callback(this, &BikeSystem::cpuTask)); | ||||||
|  |   		cpuEvent.delay(kCPUTaskDelay); | ||||||
|  |   		cpuEvent.period(kCPUTaskPeriod); | ||||||
|  |   		cpuEvent.post(); | ||||||
|  | 	#endif | ||||||
|  |      | ||||||
|  | 	eventQueue.dispatch_forever(); | ||||||
|  | } | ||||||
|  |  | ||||||
| void BikeSystem::stop() { core_util_atomic_store_bool(&_stopFlag, true); } | void BikeSystem::stop() { core_util_atomic_store_bool(&_stopFlag, true); } | ||||||
|  |  | ||||||
| #if defined(MBED_TEST_MODE) | #if defined(MBED_TEST_MODE) | ||||||
| @@ -250,4 +301,8 @@ void BikeSystem::displayTask2() { | |||||||
|         _timer, advembsof::TaskLogger::kDisplayTask2Index, taskStartTime); |         _timer, advembsof::TaskLogger::kDisplayTask2Index, taskStartTime); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | void BikeSystem::cpuTask() { | ||||||
|  |     _cpuLogger.printStats(); | ||||||
|  | } | ||||||
|  |  | ||||||
| }  // namespace static_scheduling | }  // namespace static_scheduling | ||||||
| @@ -52,6 +52,9 @@ class BikeSystem { | |||||||
|     // method called in main() for starting the system |     // method called in main() for starting the system | ||||||
|     void start(); |     void start(); | ||||||
|  |  | ||||||
|  |     // method called in main() for starting the sysytem with the event queue | ||||||
|  |     void startWithEventQueue(); | ||||||
|  |  | ||||||
|     // method called for stopping the system |     // method called for stopping the system | ||||||
|     void stop(); |     void stop(); | ||||||
|  |  | ||||||
| @@ -68,6 +71,7 @@ class BikeSystem { | |||||||
|     void resetTask(); |     void resetTask(); | ||||||
|     void displayTask1(); |     void displayTask1(); | ||||||
|     void displayTask2(); |     void displayTask2(); | ||||||
|  |     void cpuTask(); | ||||||
|  |  | ||||||
|     // stop flag, used for stopping the super-loop (set in stop()) |     // stop flag, used for stopping the super-loop (set in stop()) | ||||||
|     bool _stopFlag = false; |     bool _stopFlag = false; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user