3. Vehicle Model You are developing a vehicle simulator, which describes the kin
ID: 3675804 • Letter: 3
Question
3. Vehicle Model You are developing a vehicle simulator, which describes the kinematic motion of a front-steered, two-wheel drive vehicle. The vehicle you are simulating is visually depicted below. The equations of motion for this vehicle are provided in (1). The vehicle you are simulating is visully depicted below. The equations ofmotion for this vebicle are provided in (l) x1- ,cos() cost) x2-111 cos(x) sin x(1 L) sin) x3 C1.*2) Where x, is translational forw ard motion. l is translational left rnight motion. ty s the tire angle, and , is the heading of the vehicle. For control inputs, n, represents vehicle velocity and in, represents angular rate of change for the tire angle. The wheelbase L is defined in the Vehicle h header file. Using a simple discretization of these differential equations wsth a duration discretization of these differential equations with a duratioe 7 tre can use the tollowving equations for motion The value for x, (ture angle) must atways be between t0 5236. 0.3236] radiaus sec (ie. -t 6 radians sec) If a value is coumanded outide this ronge then xy should saturate vong the above range. Ex tfthe tire angle rate is commanded to be the ralue 0,71 56, the tire angle rate ould equal the nasaatim rate of 0 3236 The leading should always be between [O, 2 ) If the heading is a negatire value, the headaanhould be converted into the range to. 2 , by repeatedly adding 2 Ex If the heading i, . 5 the head.ng ean be con, erted to-O 5-2 te. Defined values are present for these ranges inside ofExplanation / Answer
Vehicle.cpp
// Includes header files for clarity.
#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);
}
// Sets the value for _state.
void Vehicle::setState(State x)
{
_state = x;
return;
}
// Executes the Vehicle for the duration specified.
// This method does not do any correctness checking on values in u.
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
State.h
#ifndef STATE_H
#define STATE_H
// M_PI and other variables are defined in here
#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 0.5236
#define MIN_TIRE_ANGLE -0.5236
// 2PI is important for values of heading
#define M_TWO_TIMES_PI (2*M_PI)
/* This class has five private variables and the getter and setter methods for them. */
class State{
private:
double _xpos; // Vehicle position, forward (x1).
double _ypos; // Vehicle position, left/right (x2).
double _tire_angle; // Tire angle (radians) (x3).
double _heading; // Heading (radians) (x4).
double _timestamp; // Time stamp at which this state is measured.
public:
// Constructs a new State object with these initial values.
State(double x1, double x2, double x3, double x4, double timestamp);
// Constructs an empty State object.
State();
double getXPos() const; // Returns the _xpos.
void setXPos(double xpos); // Sets the _xpos.
double getYPos() const; // Returns the _ypos.
void setYPos(double ypos); // Sets the _ypos.
double getTireAngle() const; // Returns the _tire_angle.
void setTireAngle(double angle); // Sets the _tire_angle.
double getHeading() const; // Gets the _heading.
void setHeading(double heading); // Sets the _heading.
double getTimeStamp() const; // Gets the _timestamp.
void setTimeStamp(double timestamp); // Sets the _timestamp.
};
#endif // STATE_H
Input.h
#ifndef INPUT_H
#define INPUT_H
// M_PI and other variables are defined in here
#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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.