Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Car.cpp
00001 #include "Car.h" 00002 #include "signal_wrapper.h" 00003 00004 // 00005 Car::Car(int id) { 00006 this->id = id; 00007 00008 // Include any other necessary initialization 00009 this->tick = 0; 00010 00011 thread = NULL; // Initialize thread to null, since the starting of the simulation is done in reset(-) 00012 } 00013 00014 00015 // gets new speed sets tick to 0 00016 void Car::new_speed() 00017 { 00018 int temp_speed = 0; 00019 // get a valid speed 5-15 00020 while( temp_speed < 5 ) 00021 { 00022 temp_speed = rand() %16; 00023 } 00024 this->speed = temp_speed; 00025 00026 this->tick = 0; 00027 } 00028 00029 void Car::update_pos() 00030 { 00031 // update speed every 5 ticks 00032 if(this->tick == 5) 00033 { 00034 new_speed(); 00035 } 00036 00037 // update position based on speed 00038 this->position += this->speed; 00039 this->tick++; 00040 } 00041 00042 void Car::update() { 00043 while (true) { 00044 // wait for the predefined cycle time 00045 uint32_t flags = wait_for_signal( CAR_SIGNAL ); 00046 00047 // update position of car 00048 if( flags == CAR_SIGNAL ) 00049 { 00050 update_pos(); 00051 } 00052 // unknown signal 00053 else 00054 { 00055 assert(0); 00056 } 00057 00058 00059 if(this->position >= ROADLENGTH) 00060 { 00061 // notify to end simulation and terminate yourself 00062 simulation = 0; 00063 send_signal( CAR_UPDATE_MAIN_SIGNAL ); 00064 thread->terminate(); 00065 } 00066 00067 // signal back saying update compete 00068 send_signal( CAR_UPDATE_MAIN_SIGNAL ); 00069 } 00070 } 00071 00072 00073 int Car::is_simulating() 00074 { 00075 return simulation; 00076 } 00077 00078 void Car::reset(int speed) { 00079 // handle any necessary coordination with main thread 00080 00081 // reset any other attributes that may be necessary 00082 // ... 00083 // ... 00084 00085 if (thread != NULL) { // Clear out the existing thread, if it exists, since we don't know how long it has waited for 00086 delete thread; 00087 } 00088 thread = new Thread(); // Create a new thread for the car 00089 thread->start( callback(this, &Car::update) ); // Start the thread with the car's update method 00090 00091 this->simulation = 1; 00092 this->tick = 0; 00093 this->position = 0; 00094 this->speed = speed; 00095 }
Generated on Thu Jul 14 2022 17:18:57 by
1.7.2