First picture is assignment and then the other 3 are the classes defined. Please
ID: 3675425 • Letter: F
Question
First picture is assignment and then the other 3 are the classes defined. Please help. Or give me some advice to get started. 3. Vehicle Model You are developing a vehicle simulator, which devenbes the knematic motion ofa frost-steered, two-wheel drive vehicle The vehicle you are sumalating is visually depicted below. The equations of smotion for this vebicle ase provided in (t) Where x is trnaslational forward wotios x au velsele. For control inpets, ir, repseients vehicle velocity, and wheelbase L is defined sn the Vehicle h Leader fle l left nght motion, Ns n the tise angle, and x, is the beading of the reptesents 'ngular rate of change for the tre nigde. The Using smpte discretization of these notion 5 mdnas sec) a value inExplanation / Answer
Vehicle.cpp
/**************************************************************************************************/
#include <iostream>
#include <math.h>
#include "Vehicle.h"
// Constructs a new Vehicle object with the default State value.
Vehicle:: Vehicle() {
_state.setXPos(0.0);
_state.setYPos(0.0);
_state.setTireAngle(0.0);
_state.setHeading(0.0);
}
void Vehicle::setState(State x)
{
_state = x;
return;
}
void Vehicle::stateUpdate(Input u, double duration)
{
// Temporary variables.
double _x1;
double _x2;
double _x3;
double _x4;
_x1 = _state.getXPos()+duration*u.getVelocity()*cos(_state.getTireAngle())*cos(_state.getHeading());
_x2 = _state.getYPos()+duration*u.getVelocity()*cos(_state.getTireAngle())*sin(_state.getHeading());
_x4 = _state.getHeading()+duration*u.getVelocity()*(1/L)*sin(_state.getTireAngle());
//Saturation check.
if(_x4<0)
_x4 = _x4 + M_TWO_TIMES_PI;
if(_x4>=M_TWO_TIMES_PI)
_x4 = _x4 - M_TWO_TIMES_PI;
_x3 = _state.getTireAngle()+duration*u.getTireAngleRate();
//Saturation check.
if(_x3<MIN_TIRE_ANGLE)
_x3 = MIN_TIRE_ANGLE;
if(_x3>MAX_TIRE_ANGLE)
_x3 = MAX_TIRE_ANGLE;
// State update.
_state.setXPos(_x1);
_state.setYPos(_x2);
_state.setTireAngle(_x3);
_state.setHeading(_x4);
_state.setTimeStamp(u.getTimeStamp()+duration);
}
// Gets the value for _state.
State Vehicle::getState() const{
return _state;
}
Vehicle.h
#ifndef VEHICLE_H
#define VEHICLE_H
#include "State.h"
#include "Input.h"
// Wheelbase length in meters.
#define L 2.6187
/* The State class has one private State object and 4 methods. */
class Vehicle {
private:
State _state; // the Current state of the vehicle.
void setState( State x ); // Sets the value for _state.
public:
// Constructs a new Vehicle object with the default State value.
Vehicle( );
// Executes the Vehicle for the duration specified.
// This method does not do any correctness checking on values in u.
void stateUpdate( Input u, double duration );
State getState( ) const; // Gets the value for _state.
};
#endif // VEHICLE_H
Input.cpp
#include <cstdlib>
#include <cstdio>
#include <string>
#include <iostream>
#include "Input.h"
#include "State.h"
#include "Vehicle.h"
// Default constructor
Input::Input() {
setVelocity(0.0);
setTireAngleRate(0.0);
setTimeStamp(0.0);
return;
}
// Constructs a new Input object with these initial values.
Input::Input(double vel, double tireAngleRate, double timestamp) {
setVelocity(vel);
setTireAngleRate(tireAngleRate);
setTimeStamp(timestamp);
return;
}
// Returns the _velocity.
double Input::getVelocity() const {
return this->_velocity;
}
// Sets the _velocity.
void Input::setVelocity(double vel) {
this->_velocity = vel;
}
// Returns the _tire_angle_rate.
double Input::getTireAngleRate() const {
return this->_tire_angle_rate;
}
// Sets the _tire_angle_rate.
void Input::setTireAngleRate(double angle) {
this->_tire_angle_rate = angle;
}
// Gets the _timestamp.
double Input::getTimeStamp() const {
return this->_timestamp;
}
// Sets the _timestamp.
void Input::setTimeStamp(double timestamp) {
this->_timestamp = timestamp;
}
/**************************************************************************************************/
Input.h
#ifndef INPUT_H
#define INPUT_H
#ifndef _USE_MATH_DEFINES
#define _USE_MATH_DEFINES
#endif
#include <math.h>
// the MAX and MIN values for tire angle are used
// in the setTireAngle method
#define MAX_TIRE_ANGLE_RATE 0.5236
#define MIN_TIRE_ANGLE_RATE -0.5236
/* This class has three private variables and the setter and getter methods for the three variables. */
class Input{
private:
double _velocity; // Commanded vehicle velocity (u1).
double _tire_angle_rate; // Commanded tire angle rate (u2).
double _timestamp; // Time stamp at which this command is valid.
public:
// Constructs a new Input object with these initial values.
Input(double vel, double tireAngleRate, double timestamp);
// Constructs an empty Input object.
Input();
double getVelocity() const; // Returns the _velocity.
void setVelocity(double vel); // Sets the _velocity.
double getTireAngleRate() const; // Returns the _tire_angle_rate.
void setTireAngleRate(double angle); // Sets the _tire_angle_rate.
double getTimeStamp() const; // Gets the _timestamp.
void setTimeStamp(double timestamp); // Sets the _timestamp.
};
#endif // INPUT_H
Director.cpp
/**************************************************************************************************/
#include <cstdlib>
#include <cstdio>
#include <string>
#include <iostream>
// Includes all the header files for clarity.
#include "Input.h"
#include "State.h"
#include "Vehicle.h"
#include "DataSource.h"
#include "Director.h"
#include "DataSink.h"
// Default constructor.
Director::Director() {
}
// Constructor which calls the methods of DataSource and DataSink class.
Director::Director(string inputfile, string outputfile)
{
// Method call for read input.
if(!source.ReadInputFile(inputfile))
{
return;
}
// Method call for sort.
source.sort(0,source.getInput().size()-1);
// Method call for generateState which updates the State.
sink.generateState(source.getInput());
// Method call for write in output file.
if(!sink.WriteOutputFile(outputfile,source.validate()))
{
return;
}
}
Director.h
#ifndef DIRECTOR_H
#define DIRECTOR_H
#include<string>
#include "Input.h"
#include "State.h"
#include "Vehicle.h"
#include "DataSource.h"
#include "Director.h"
#include "DataSink.h"
using namespace std;
/* This class has two private objects of type DataSource and DataSink and two constructors for interatcion.*/
class Director{
private:
DataSource source; // An object of DataSource class.
DataSink sink; // An object of DataSink class.
public:
Director(); // Default constructor.
Director(string inputfile, string outputfile); // Constructor which calls the methods of DataSource and DataSink class.
};
#endif // DIRECTOR_H
DataSource.cpp
#include <cstdlib>
#include <cstdio>
#include <string>
#include <iostream>
#include<fstream>
#include<vector>
#include "Input.h"
#include "State.h"
#include "Vehicle.h"
#include "DataSource.h"
#include "Director.h"
#include "DataSink.h"
// Default constructor.
DataSource::DataSource() {
// Initializes the temporary variables to 0.0.
this->velocity = 0.0;
this->tireAngleRate = 0.0;
this->timeStamp = 0.0;
}
// Method to read from input file and check for errors.
bool DataSource::ReadInputFile( std::string filename)
{
bool result = false;
ifstream file; // Input file stream.
file.open(filename.c_str( )); // Open file.
if (!file.is_open())
{
// Prints error message accordingly.
std::cerr << "Error! Unable to read from file " << filename << ". Aborting." << std::endl;
result = false;
return result;
}
// Reads data from file.
while (!file.eof()) {
// Stores data in temporary variables.
file>>velocity;
file>>tireAngleRate;
file>>timeStamp;
if(!file.fail())
{
// Pushes data to the vector of input objects.
inputData.push_back(Input(velocity,tireAngleRate,timeStamp));
}
}
file.close(); // Closes the file.
return true;
}
// Method to complete the partition part of the quicksort algorithm.
int DataSource::partition(int i, int k) {
int l = 0; // Lower point.
int h = 0; // Higher point.
int midpoint = 0;
double pivot = 0.0; // Pivot point.
double temp = 0.0; // Temporary variable for swapping.
bool done = false;
/* Pick middle element as pivot */
midpoint = i + (k - i) / 2; // Middle point.
pivot = inputData.at(midpoint).getTimeStamp(); // Pivot according to timeStamp.
l = i;
h = k;
while (!done) {
/* Increment l while timeStamp < pivot */
while (inputData.at(l).getTimeStamp() < pivot) {
++l;
}
/* Decrement h while pivot < timeStamp */
while (pivot < inputData.at(h).getTimeStamp()) {
--h;
}
/* If there are zero or one elements remaining,
all numbers are partitioned. Return h */
if (l >= h) {
done = true;
}
else {
/* Swap timeStamp of l and timeStamp of h,
update l and h */
temp = inputData.at(l).getTimeStamp();
inputData.at(l).setTimeStamp(inputData.at(h).getTimeStamp());
inputData.at(h).setTimeStamp(temp);
++l;
--h;
}
}
return h;
}
// Method to complete part of the quicksort algorithm.
void DataSource::sort(int i, int k) {
int j = 0; // Counter for partition.
/* Base case: If there are 1 or zero elements to sort,
partition is already sorted */
if (i >= k) {
return;
}
/* Partition the data within the array. Value j returned
from partitioning is location of last element in low partition. */
j = partition(i, k);
/* Recursively sort low partition (i to j) and
high partition (j + 1 to k) */
sort(i, j);
sort(j + 1, k);
return;
}
// Method to validate the control inputs.
bool DataSource::validate() {
double dur = 0.0; // Duration variable.
for(int a = 0;a<inputData.size();a++)
{
if(a == inputData.size() -1)
{
dur = 0.2; // Calculates duration.
}
else
{
dur = inputData.at(a+1).getTimeStamp()-inputData.at(a).getTimeStamp();
}
// Checks for timeStamp of 0.
if(inputData.at(0).getTimeStamp()!=0.0)
return false;
// Checks for range of TireAngle.
if(inputData.at(a).getTireAngleRate()< MIN_TIRE_ANGLE || inputData.at(a).getTireAngleRate()>MAX_TIRE_ANGLE)
return false;
// Checks for velocity range.
if(inputData.at(a).getVelocity()<0.0 || inputData.at(a).getVelocity()>30.0)
return false;
// Checks for non-negative timeStamps.
if(inputData.at(a).getTimeStamp()<0)
return false;
// Checks for duration range.
if(dur<0.005 || dur>0.201)
return false;
}
return true;
}
// Method to return the vector of input objects.
vector<Input> DataSource::getInput() {
return inputData;
}
/**************************************************************************************************/
DataSource.h
#ifndef DATASOURCE_H
#define DATASOURCE_H
#include <stdio.h>
#include"Input.h"
#include "State.h"
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
/* This class consists of four private variables and six methods which perform read, sort and validate functions. */
class DataSource{
private:
vector<Input> inputData; // A vector of objects of type Input.
double velocity; // Commanded vehicle velocity (u1) (Temporary).
double tireAngleRate; // Commanded tire angle rate (u2) (Temporary).
double timeStamp; // Time stamp at which this command is valid. (Temporary).
public:
DataSource(); // Default constructor for DataSource.
bool ReadInputFile(std::string filename); // Method to read in input file and check for failure in reading the file.
vector<Input> getInput(); // Method which returns the vector of input objects.
void sort(int i, int k); // Method which performs a part of the quicksort algorithm.
int partition(int i, int k); // Method which performs the partition part of the quicksort algorithm.
bool validate(); // Validates the control inputs from the file after they are read and sorted.
};
#endif // DATASOURCE_H
DataSink.cpp
#include <cstdlib>
#include <cstdio>
#include <string>
#include <iostream>
//Includes all the header files for clarity.
#include "Input.h"
#include "State.h"
#include "Vehicle.h"
#include "DataSource.h"
#include "Director.h"
#include "DataSink.h"
/**************************************************************************************************/
// Default Constructor.
DataSink::DataSink() {
}
// Method to add State to the vector of state objects.
void DataSink::addState(State s)
{
stateData.push_back(s); // Uses vector.push_back method to insert the object.
}
// Method to return the state.
vector<State> DataSink::getState()
{
return stateData; // Returns the vector of state objects.
}
//Method to generate the state by using Vehicle class.
void DataSink::generateState(vector<Input> input) {
double duration = 0.0; // Temporary variable for storing duration.
for(int j = 0;j<input.size();j++) // Loop to calculate duration and then update the state accordingly.
{
if(j == input.size() -1)
{
duration = 0.2; // Duration is set 0.2 for the last one.
}
else
{
duration = input.at(j+1).getTimeStamp()-input.at(j).getTimeStamp(); // Duration is calculated.
}
stateGenerator.stateUpdate(input.at(j),duration); // Method to update the state of the current object using the Vehicle class.
addState(stateGenerator.getState()); // Adds the updated state to the vector of state objects.
}
}
bool DataSink::WriteOutputFile(string outfile, bool res) {
ofstream outFS; // Output file stream.
// Open file.
outFS.open(outfile.c_str());
if (!outFS.is_open()) {
// Prints out the error statement accordingly.
std::cerr << "Error! Unable to write to file " << outfile << ". Aborting." << std::endl;
return false;
}
if(res) // Checks if validation results in true.
{
for(int k =0;k<stateData.size();k++)
{
// Writes to output file as a csv file.
outFS<<stateData.at(k).getTimeStamp()<<','<<stateData.at(k).getXPos()<<','<<stateData.at(k).getYPos()<<','<<stateData.at(k).getTireAngle()<<','<<stateData.at(k).getHeading()<<endl;
}
}
outFS.close(); // Closes the output stream.
return true;
}
/**************************************************************************************************/
DataSink.h
#ifndef DATASINK_H
#define DATASINK_H
#include <stdio.h>
#include"Input.h"
#include "State.h"
#include "Vehicle.h"
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
/* This class has two private variables of type vector<State> and Vehicle and five methods to implement the state generation and writing data to output file operations. */
class DataSink {
private:
vector<State> stateData; // A vector of objects of type State.
Vehicle stateGenerator; // An object of type Vehicle.
public:
DataSink(); // A default constructor.
void addState(State s); // Method to add State to the vector of state objects.
vector<State> getState(); // Method to return the state.
void generateState(vector<Input> input); //Method to generate the state by using Vehicle class.
bool WriteOutputFile(string outfile, bool res); // Method to write data to output file.
};
#endif // DATASINK_H
main.cpp
#include <cstdlib>
#include <cstdio>
#include <string>
#include <iostream>
#include "Input.h"
#include "State.h"
#include "Vehicle.h"
#include "DataSource.h"
#include "Director.h"
#include "DataSink.h"
int
main(int argc, char *argv[])
{
int result=EXIT_SUCCESS;
/* Logic to check arguments */
if (argc != 3)
{
// Prints Usage statement.
std::cout << std::endl << "Usage: " << argv[0] << " controlInputs stateOutputs"
<< std::endl << std::endl;
result = EXIT_FAILURE;
return result;
}
// Constructor call for director.
Director dir(argv[1],argv[2]);
return EXIT_SUCCESS;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.