// This method will do the main data processing job. ModuleState::MODULE_EXITCODE Proxy::body() { uint32_t captureCounter = 0; while (getModuleState() == ModuleState::RUNNING) { // Capture frame. if (m_camera != NULL) { core::data::image::SharedImage si = m_camera->capture(); Container c(Container::SHARED_IMAGE, si); distribute(c); captureCounter++; } Container containerVehicleControl = getKeyValueDataStore().get(Container::VEHICLECONTROL); VehicleControl vc = containerVehicleControl.getData<VehicleControl>(); cerr << "Speed: '" << vc.getSpeed() << "'" << endl; cerr << "Angle: '" << vc.getSteeringWheelAngle() << "'" << endl; // TODO: Here, you need to implement the data links to the embedded system // to read data from IR/US. // Test *************************** // Markus Erlach string in = ""; string rec; char command[10]; cout << "Enter command to send, " << endl; cout << "Command alternatives: w, f, s, r, n, h, v, m" << endl; cin >> in; /* w, f, s, r, n, h, v w = set speed to 1560, f = accelerate by 10 s = slow down, r = reverse, n = neutral h = turn right, v = turn left */ strcpy(command, in.c_str()); write(port, command, 10); cout << "Proxy2 wrote: "<< command << endl; rec = msv::readSerial(); decode(rec); Container c = Container(Container::USER_DATA_0, sensorBoardData); distribute(c); /*int IR1Data = sensorBoardData.getValueForKey_MapOfDistances(0); cout << "SBD IR1: " << IR1Data << endl; */ //flushes the input queue, which contains data that have been received but not yet read. tcflush(port, TCIFLUSH); } cout << "Proxy: Captured " << captureCounter << " frames." << endl; return ModuleState::OKAY; }
int main(int argc, char** argv) { SensorManager sensors; sensors.initialize(); VehicleControl control; control.initialize(); //control.setSpeed(1); while (1) { sensors.update(); char c = getchar(); if (c == '\n') { std::cout << "Ultrasonic = " << sensors.getUltrasonic() << std::endl; std::cout << "Light_Left = " << sensors.getLeftLightTrip() << std::endl; std::cout << "Light_Right = " << sensors.getRightLightTrip() << std::endl; std::cout << "IR_Left = " << sensors.getLeftInfraredTrip() << std::endl; std::cout << "IR_Right = " << sensors.getRightInfraredTrip() << std::endl; } } return 0; }
ModuleState::MODULE_EXITCODE Vehicle::runLinearBicycleModelNew(const bool &withSpeedController) { LinearBicycleModelNew lbmn(getKeyValueConfiguration(), withSpeedController); KeyValueDataStore &kvs = getKeyValueDataStore(); while (getModuleState() == ModuleState::RUNNING) { // Get current ForceControl. Container c = kvs.get(Container::VEHICLECONTROL); VehicleControl vc = c.getData<VehicleControl>(); cerr << "VehicleControl: '" << vc.toString() << "'" << endl; if (withSpeedController) { lbmn.speed(vc.getSpeed()); } else { lbmn.accelerate(vc.getAcceleration()); } lbmn.steer(vc.getSteeringWheelAngle()); if (vc.getBrakeLights()) { cout << "Turn ON brake lights." << endl; } if (vc.getLeftFlashingLights()) { cout << "Turn ON left flashing lights." << endl; } if (vc.getRightFlashingLights()) { cout << "Turn ON right flashing lights." << endl; } EgoState es = lbmn.computeEgoState(); // Get vehicle data. VehicleData vd = lbmn.getVehicleData(); cerr << "VehicleData: '" << vd.toString() << "'" << endl; Container container(Container::EGOSTATE, es); getConference().send(container); // Send vehicledata. Container c2(Container::VEHICLEDATA, vd); getConference().send(c2); } return ModuleState::OKAY; }
// This method will do the main data processing job. coredata::dmcp::ModuleExitCodeMessage::ModuleExitCode BoxParker::body() { double distanceOld = 0; double absPathStart = 0; double absPathEnd = 0; int stageMoving = 0; int stageMeasuring = 0; while (getModuleStateAndWaitForRemainingTimeInTimeslice() == coredata::dmcp::ModuleStateMessage::RUNNING) { // 1. Get most recent vehicle data: Container containerVehicleData = getKeyValueDataStore().get(Container::VEHICLEDATA); VehicleData vd = containerVehicleData.getData<VehicleData> (); // 2. Get most recent sensor board data describing virtual sensor data: Container containerSensorBoardData = getKeyValueDataStore().get(Container::USER_DATA_0); SensorBoardData sbd = containerSensorBoardData.getData<SensorBoardData> (); // Create vehicle control data. VehicleControl vc; // Moving state machine. if (stageMoving == 0) { // Go forward. vc.setSpeed(1); vc.setSteeringWheelAngle(0); } if ((stageMoving > 0) && (stageMoving < 20)) { // Move slightly forward. vc.setSpeed(1); vc.setSteeringWheelAngle(0); stageMoving++; } if ((stageMoving >= 20) && (stageMoving < 25)) { // Stop. vc.setSpeed(0); vc.setSteeringWheelAngle(0); stageMoving++; } if ((stageMoving >= 25) && (stageMoving < 80)) { // Backwards, steering wheel to the right. vc.setSpeed(-2); vc.setSteeringWheelAngle(25); stageMoving++; } if (stageMoving >= 80) { // Stop. vc.setSpeed(0); vc.setSteeringWheelAngle(0); stageMoving++; } if (stageMoving >= 150) { // End component. break; } // Measuring state machine. switch (stageMeasuring) { case 0: { // Initialize measurement. distanceOld = sbd.getValueForKey_MapOfDistances(2); stageMeasuring++; } break; case 1: { // Checking for distance sequence +, -. if ((distanceOld > 0) && (sbd.getValueForKey_MapOfDistances(2) < 0)) { // Found distance sequence +, -. stageMeasuring = 2; absPathStart = vd.getAbsTraveledPath(); } distanceOld = sbd.getValueForKey_MapOfDistances(2); } break; case 2: { // Checking for distance sequence -, +. if ((distanceOld < 0) && (sbd.getValueForKey_MapOfDistances(2) > 0)) { // Found distance sequence -, +. stageMeasuring = 1; absPathEnd = vd.getAbsTraveledPath(); const double GAP_SIZE = (absPathEnd - absPathStart); cerr << "Size = " << GAP_SIZE << endl; m_foundGaps.push_back(GAP_SIZE); if ((stageMoving < 1) && (GAP_SIZE > 3.5)) { stageMoving = 1; } } distanceOld = sbd.getValueForKey_MapOfDistances(2); } break; } // Create container for finally sending the data. Container c(Container::VEHICLECONTROL, vc); // Send container. getConference().send(c); } return coredata::dmcp::ModuleExitCodeMessage::OKAY; }
// This method will do the main data processing job. ModuleState::MODULE_EXITCODE Proxy::body() { uint32_t captureCounter = 0; SensorBoardData sensorBoardData; core::data::environment::VehicleData vd; while (getModuleState() == ModuleState::RUNNING) { // Capture frame. if (m_camera != NULL) { core::data::image::SharedImage si = m_camera->capture(); Container c(Container::SHARED_IMAGE, si); distribute(c); captureCounter++; } Container containerVehicleControl = getKeyValueDataStore().get(Container::VEHICLECONTROL); VehicleControl vc = containerVehicleControl.getData<VehicleControl> (); cerr << "Speed data: " << vc.getSpeed() << endl; cout << "Angle : " << vc.getSteeringWheelAngle()<<endl; // TODO: Here, you need to implement the data links to the embedded system // to read data from IR/US. int angle=60; int angleFromDriver= (int)vc.getSteeringWheelAngle(); // receive steeringaAngle from VehicleControl //convert the angle for arduino if(angleFromDriver <0) angleFromDriver*=-1; else angleFromDriver*=-1; angle+=angleFromDriver; stringstream ss; ss << angle; if(angle<100 && angle >10) convertedAngle="0"+ss.str(); else if(angle <10 && angle>-1) convertedAngle="00"+ss.str(); else convertedAngle=ss.str(); // send different values depending on drivers speed if(vc.getSpeed()>0) userInput="600"+convertedAngle+","; else if(vc.getSpeed()<1 && vc.getSpeed()>-1) userInput="512"+convertedAngle+","; else if(vc.getSpeed()<-1) userInput="200"+convertedAngle+","; cout<<userInput<<endl; if(wd!=-1 && wd!=0) msv::write(userInput); // write to arduino if(fd!=-1 && fd!=0){ readings=msv::read(); // read from arduino cout<< "readings are "<< readings << endl; int length=atoi(readings.substr(0,2).c_str()); unsigned int finalLength=length+5; // check length //decode netstring received from arduino if(readings.length()==finalLength){ string ir1=readings.substr(3,3); valIr1=atoi(ir1.c_str()); string ir2=readings.substr(6,3); valIr2=atoi(ir2.c_str()); string ir3=readings.substr(9,3); valIr3=atoi(ir3.c_str()); string us1=readings.substr(12,3); valUs1=atoi(us1.c_str()); string us2=readings.substr(15,3); valUs2=atoi(us2.c_str()); string us3=readings.substr(18,3); valUs3=atoi(us3.c_str()); string wheelE=readings.substr(21,length-18); valWheelE=atoi(wheelE.c_str()); } cout<<"Wheel Encoder value " << valWheelE <<endl; //Map decoded sensor values sensorBoardData.putTo_MapOfDistances(4,valUs2); sensorBoardData.putTo_MapOfDistances(3,valUs3); sensorBoardData.putTo_MapOfDistances(1,valIr3); sensorBoardData.putTo_MapOfDistances(2,valIr2); sensorBoardData.putTo_MapOfDistances(0,valIr1); sensorBoardData.putTo_MapOfDistances(5,valUs1); vd.setAbsTraveledPath(valWheelE); Container Pvd=Container(Container::VEHICLEDATA, vd); Container c = Container(Container::USER_DATA_0, sensorBoardData); distribute(c); distribute(Pvd); tcflush(fd, TCIFLUSH); } if(wd!=-1 && wd!=0) tcflush(wd, TCOFLUSH); //usleep(2000000); } if(wd!=-1 && wd!=0){ msv::write("512060,"); msv::close(wd); } if(fd!=-1 && fd!=0) msv::close(fd); cout << "Proxy: Captured " << captureCounter << " frames." << endl; return ModuleState::OKAY; }
// This method will do the main data processing job. odcore::data::dmcp::ModuleExitCodeMessage::ModuleExitCode Proxy::body() { uint32_t captureCounter = 0; uint32_t sendCounter = 0; // Make sure that we don't send every loop uint32_t delayCounter = 0; // Delay the send over serial on startup uint32_t portNumber = 0; //int16_t oldSpeed = 1500; //int16_t oldAngle = 90; size_t readSize = 1; // Size of data to read from the serial port bool newCommand = false; string port = "/dev/ttyACM"; unsigned long baud = 57600; string result = ""; // Incoming serial buffer bool serialOpen = false; // Try opening the serial connections, tries ports 0-4 automatically while(!serialOpen && portNumber < 4){ string xport = port + to_string(portNumber); try { my_serial = unique_ptr<serial::Serial>(new Serial(xport, baud, serial::Timeout::simpleTimeout(1000))); serialOpen = true; cout << "Trying port " << xport << endl; } catch (IOException e){ cerr << "Error setting up serialport: " << xport << endl; serialOpen = false; } // Test if the serial port has been created correctly. cout << "Is the serial port open?"; if(my_serial->isOpen()){ cout << " Yes." << endl; } else { cout << " No." << endl; } portNumber ++; } while (getModuleStateAndWaitForRemainingTimeInTimeslice() == odcore::data::dmcp::ModuleStateMessage::RUNNING) { // Delay all communication for a short while to not overflow the arduino if (delayCounter < 40){ delayCounter++; } else { // Capture frame. if (m_camera.get() != NULL) { odcore::data::image::SharedImage si = m_camera->capture(); Container c(si); distribute(c); captureCounter++; } // Create objects where we get speed and steering angle from Container containerVehicleControl = getKeyValueDataStore().get(automotive::VehicleControl::ID()); VehicleControl vc = containerVehicleControl.getData<VehicleControl>(); double vcSpeed = vc.getSpeed(); int16_t speedTemp = vcSpeed; // Set the speed to constant values if (vcSpeed > 0){ speedTemp = 1615; } else if (vcSpeed < 0){ speedTemp = 1070; } else { speedTemp = 1500; } //vcSpeed = speedTemp; double vcAngle = vc.getSteeringWheelAngle(); // Convert steering angle from radiants to degrees int16_t vcDegree = (vcAngle * cartesian::Constants::RAD2DEG); // Send the exact value we want to set steering at if (vcDegree > 25){ vcDegree = 90 + 40; } else if (vcDegree < -25){ vcDegree = 90 - 30; } else { vcDegree = 90 + vcDegree; } sendCounter++; // Dont send to arduino every loop //if(my_serial->isOpen() && sendCounter > 5 && (oldSpeed != speedTemp || oldAngle != vcDegree)){ if(my_serial->isOpen() && sendCounter > 5){ sendOverSerial(speedTemp, vcDegree); sendCounter = 0; //oldSpeed = speedTemp; //oldAngle = vcDegree; } // Read to a buffer for incoming data while (my_serial->available() > 0){ string c = my_serial->read(readSize); result += c; if (c == ","){ newCommand = true; // When a full netstring has arrived } } if (newCommand){ string decoded = netstrings.decodeNetstring(result); sbdDistribute(decoded); // Distribute SensorBoardData vdDistribute(decoded); // Distribute VehicleData newCommand = false; result = ""; // clear buffer } } } cout << "Proxy: Captured " << captureCounter << " frames." << endl; return odcore::data::dmcp::ModuleExitCodeMessage::OKAY; }
// This method will do the main data processing job. coredata::dmcp::ModuleExitCodeMessage::ModuleExitCode Overtaker::body() { const int32_t ULTRASONIC_FRONT_CENTER = 3; const int32_t ULTRASONIC_FRONT_RIGHT = 4; const int32_t INFRARED_FRONT_RIGHT = 0; const int32_t INFRARED_REAR_RIGHT = 2; const double OVERTAKING_DISTANCE = 5.5; const double HEADING_PARALLEL = 0.04; // Overall state machines for moving and measuring. enum StateMachineMoving { FORWARD, TO_LEFT_LANE_LEFT_TURN, TO_LEFT_LANE_RIGHT_TURN, CONTINUE_ON_LEFT_LANE, TO_RIGHT_LANE_RIGHT_TURN, TO_RIGHT_LANE_LEFT_TURN }; enum StateMachineMeasuring { DISABLE, FIND_OBJECT_INIT, FIND_OBJECT, FIND_OBJECT_PLAUSIBLE, HAVE_BOTH_IR, HAVE_BOTH_IR_SAME_DISTANCE, END_OF_OBJECT }; StateMachineMoving stageMoving = FORWARD; StateMachineMeasuring stageMeasuring = FIND_OBJECT_INIT; // State counter for dynamically moving back to right lane. int32_t stageToRightLaneRightTurn = 0; int32_t stageToRightLaneLeftTurn = 0; // Distance variables to ensure we are overtaking only stationary or slowly driving obstacles. double distanceToObstacle = 0; double distanceToObstacleOld = 0; while (getModuleStateAndWaitForRemainingTimeInTimeslice() == coredata::dmcp::ModuleStateMessage::RUNNING) { // 1. Get most recent vehicle data: Container containerVehicleData = getKeyValueDataStore().get(Container::VEHICLEDATA); VehicleData vd = containerVehicleData.getData<VehicleData> (); // 2. Get most recent sensor board data: Container containerSensorBoardData = getKeyValueDataStore().get(Container::USER_DATA_0); SensorBoardData sbd = containerSensorBoardData.getData<SensorBoardData> (); // Create vehicle control data. VehicleControl vc; // Moving state machine. if (stageMoving == FORWARD) { // Go forward. vc.setSpeed(2); vc.setSteeringWheelAngle(0); stageToRightLaneLeftTurn = 0; stageToRightLaneRightTurn = 0; } else if (stageMoving == TO_LEFT_LANE_LEFT_TURN) { // Move to the left lane: Turn left part until both IRs see something. vc.setSpeed(1); vc.setSteeringWheelAngle(-25); // State machine measuring: Both IRs need to see something before leaving this moving state. stageMeasuring = HAVE_BOTH_IR; stageToRightLaneRightTurn++; } else if (stageMoving == TO_LEFT_LANE_RIGHT_TURN) { // Move to the left lane: Turn right part until both IRs have the same distance to obstacle. vc.setSpeed(1); vc.setSteeringWheelAngle(25); // State machine measuring: Both IRs need to have the same distance before leaving this moving state. stageMeasuring = HAVE_BOTH_IR_SAME_DISTANCE; stageToRightLaneLeftTurn++; } else if (stageMoving == CONTINUE_ON_LEFT_LANE) { // Move to the left lane: Passing stage. vc.setSpeed(2); vc.setSteeringWheelAngle(0); // Find end of object. stageMeasuring = END_OF_OBJECT; } else if (stageMoving == TO_RIGHT_LANE_RIGHT_TURN) { // Move to the right lane: Turn right part. vc.setSpeed(1.5); vc.setSteeringWheelAngle(25); stageToRightLaneRightTurn--; if (stageToRightLaneRightTurn == 0) { stageMoving = TO_RIGHT_LANE_LEFT_TURN; } } else if (stageMoving == TO_RIGHT_LANE_LEFT_TURN) { // Move to the left lane: Turn left part. vc.setSpeed(.9); vc.setSteeringWheelAngle(-25); stageToRightLaneLeftTurn--; if (stageToRightLaneLeftTurn == 0) { // Start over. stageMoving = FORWARD; stageMeasuring = FIND_OBJECT_INIT; distanceToObstacle = 0; distanceToObstacleOld = 0; } } // Measuring state machine. if (stageMeasuring == FIND_OBJECT_INIT) { distanceToObstacleOld = sbd.getValueForKey_MapOfDistances(ULTRASONIC_FRONT_CENTER); stageMeasuring = FIND_OBJECT; } else if (stageMeasuring == FIND_OBJECT) { distanceToObstacle = sbd.getValueForKey_MapOfDistances(ULTRASONIC_FRONT_CENTER); // Approaching an obstacle (stationary or driving slower than us). if ( (distanceToObstacle > 0) && (((distanceToObstacleOld - distanceToObstacle) > 0) || (fabs(distanceToObstacleOld - distanceToObstacle) < 1e-2)) ) { // Check if overtaking shall be started. stageMeasuring = FIND_OBJECT_PLAUSIBLE; } distanceToObstacleOld = distanceToObstacle; } else if (stageMeasuring == FIND_OBJECT_PLAUSIBLE) { if (sbd.getValueForKey_MapOfDistances(ULTRASONIC_FRONT_CENTER) < OVERTAKING_DISTANCE) { stageMoving = TO_LEFT_LANE_LEFT_TURN; // Disable measuring until requested from moving state machine again. stageMeasuring = DISABLE; } else { stageMeasuring = FIND_OBJECT; } } else if (stageMeasuring == HAVE_BOTH_IR) { // Remain in this stage until both IRs see something. if ( (sbd.getValueForKey_MapOfDistances(INFRARED_FRONT_RIGHT) > 0) && (sbd.getValueForKey_MapOfDistances(INFRARED_REAR_RIGHT) > 0) ) { // Turn to right. stageMoving = TO_LEFT_LANE_RIGHT_TURN; } } else if (stageMeasuring == HAVE_BOTH_IR_SAME_DISTANCE) { // Remain in this stage until both IRs have the similar distance to obstacle (i.e. turn car) // and the driven parts of the turn are plausible. const double IR_FR = sbd.getValueForKey_MapOfDistances(INFRARED_FRONT_RIGHT); const double IR_RR = sbd.getValueForKey_MapOfDistances(INFRARED_REAR_RIGHT); if ((fabs(IR_FR - IR_RR) < HEADING_PARALLEL) && ((stageToRightLaneLeftTurn - stageToRightLaneRightTurn) > 0)) { // Straight forward again. stageMoving = CONTINUE_ON_LEFT_LANE; } } else if (stageMeasuring == END_OF_OBJECT) { // Find end of object. distanceToObstacle = sbd.getValueForKey_MapOfDistances(ULTRASONIC_FRONT_RIGHT); if (distanceToObstacle < 0) { // Move to right lane again. stageMoving = TO_RIGHT_LANE_RIGHT_TURN; // Disable measuring until requested from moving state machine again. stageMeasuring = DISABLE; } } // Create container for finally sending the data. Container c(Container::VEHICLECONTROL, vc); // Send container. getConference().send(c); } return coredata::dmcp::ModuleExitCodeMessage::OKAY; }
// This method will do the main data processing job. ModuleState::MODULE_EXITCODE Driver::body() { std::ofstream myFile("sensors.bin", ios::binary); while (getModuleState() == ModuleState::RUNNING) { // In the following, you find example for the various data sources that are available: // 1. Get most recent vehicle data: Container containerVehicleData = getKeyValueDataStore().get(Container::VEHICLEDATA); VehicleData vd = containerVehicleData.getData<VehicleData> (); // cerr << "Most recent vehicle data: '" << vd.toString() << "'" << endl; // 2. Get most recent sensor board data: Container containerSensorBoardData = getKeyValueDataStore().get(Container::USER_DATA_0); SensorBoardData sbd = containerSensorBoardData.getData<SensorBoardData> (); timeval time; string test; stringstream teststream; double val = sbd.getDistance(0); myFile.write((char*)&val, sizeof(val)); val = sbd.getDistance(1); myFile.write((char*)&val, sizeof(val)); val = sbd.getDistance(2); myFile.write((char*)&val, sizeof(val)); val = sbd.getDistance(3); myFile.write((char*)&val, sizeof(val)); val = sbd.getDistance(4); myFile.write((char*)&val, sizeof(val)); gettimeofday(&time, NULL); unsigned long millis = (time.tv_usec / 1000); teststream << millis; if (millis < 10){ test = remove_letter(currentDateTime(), ':') + "00" + teststream.str(); } else if (millis < 100 and millis >= 10) { test = remove_letter(currentDateTime(), ':') + "0" + teststream.str(); } else { test = remove_letter(currentDateTime(), ':') + teststream.str(); } cerr << test << endl; int timestamp; timestamp = atoi(test.c_str()); myFile.write((char*)×tamp, sizeof(timestamp)); // 3. Get most recent user button data: Container containerUserButtonData = getKeyValueDataStore().get(Container::USER_BUTTON); UserButtonData ubd = containerUserButtonData.getData<UserButtonData> (); // cerr << "Most recent user button data: '" << ubd.toString() << "'" << endl; // 4. Get most recent steering data as fill from lanedetector for example: Container containerSteeringData = getKeyValueDataStore().get(Container::USER_DATA_1); SteeringData sd = containerSteeringData.getData<SteeringData> (); // cerr << "Most recent steering data: '" << sd.toString() << "'" << endl; // Design your control algorithm here depending on the input data from above. // Create vehicle control data. VehicleControl vc; // With setSpeed you can set a desired speed for the vehicle in the range of -2.0 (backwards) .. 0 (stop) .. +2.0 (forwards) vc.setSpeed(1.5); // With setSteeringWheelAngle, you can steer in the range of -26 (left) .. 0 (straight) .. +25 (right) double desiredSteeringWheelAngle = 0; // 4 degree but SteeringWheelAngle expects the angle in radians! vc.setSteeringWheelAngle(desiredSteeringWheelAngle * Constants::DEG2RAD); // You can also turn on or off various lights: vc.setBrakeLights(false); vc.setLeftFlashingLights(false); vc.setRightFlashingLights(true); // Create container for finally sending the data. Container c(Container::VEHICLECONTROL, vc); // Send container. getConference().send(c); } myFile.close(); return ModuleState::OKAY; }