Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added .vscode-insiders
Empty file.
14 changes: 13 additions & 1 deletion main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,22 @@ int main() {
Profile::ProfilePoint current;
Profile::ProfilePoint goal;
{
goal.position = 1.0;
goal.position = 2.0;
goal.velocity = 0.0;
}
Profile profile(current);
profile.SetGoal(goal);

std::string maxVignore;
std::cout << "Ignore max velocity? ";
std::cin >> maxVignore;
if (maxVignore == "yes"){
ignore_max_velocity = true;
}
else if (maxVignore == "no"){
ignore_max_velocity = false;
}
std::cout << profile.GetTime() << std::endl;
std::cout << "This is part two of the assignment" << std::endl;
profile.GetSetpoint();
}
78 changes: 77 additions & 1 deletion profile.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,81 @@
#include "profile.h"
#include <cmath>
#include <sstream>
#include <iostream>

double time; //time total
double time_acc; //time accelerating or decelerating
double time_const; //time at constance

double distance_while_acc; //distance while accelerating or decelerating
double distance_traveled_after_acc;

double Vzero = 0;
double velocity_max = kMaxVelocity;
double velocity_goal;
double velocity_current;
double acceleration = kMaxAcceleration;

double position_beg; //begginning position
double position_inp; //goal position

const double Profile::GetTime() {

if (ignore_max_velocity == true){
time = (Vzero + sqrt(sqrt(Vzero) +
4 * 0.5 * acceleration * (1.0)))
/ acceleration;
}
else {
time_acc = (velocity_max - Vzero) / acceleration;
distance_while_acc = velocity_max * time_acc / 2;
distance_traveled_after_acc = 0.0 - 2 * distance_while_acc;
time_const = distance_while_acc / velocity_max;
time = 2 * time_acc + time_const;
}
std::ostringstream strs;
strs << time;
std::string str = strs.str();
std::cout << time << std::endl;

return 0.0;
}
} //Profile:GetTime

const double Profile::GetSetpoint(){
double t; //user inputed time
std::string time;
std::cout << "Velocity at: ";
std::cin >> t;
Vzero = current_.velocity;
velocity_goal = goal_.velocity;

position_beg = current_.position;
position_inp = goal_.position;

time_acc = std::abs(velocity_max - Vzero) / acceleration;
distance_while_acc = 0.5 * std::abs(velocity_max + Vzero) * time_acc;

distance_traveled_after_acc = position_inp - distance_while_acc - distance_while_acc;
time_const = distance_traveled_after_acc / velocity_max;

time = time_acc + time_const + time_acc;

//check the velocity of the time on the graph

if (t < time_acc) {
velocity_current = Vzero + (velocity_max - Vzero) * (t) / time_acc;
}
else if (t >= time_acc && t <= (time_acc + time_const)) {
velocity_current = velocity_max;
}
else if (t > (time_acc + time_const) && t <= t) {
velocity_current = velocity_max + (velocity_goal - velocity_max) * (t - time_acc - time_const) / time_acc;
}
else {
velocity_current = 0;
}
std::ostringstream strss;
strss << velocity_current;
std::string str = strss.str();
std::cout << velocity_current << std::endl;
} //Profile::GetSetpoint
12 changes: 9 additions & 3 deletions profile.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
#ifndef PROFILE_H_
#define PROFILE_H_

constexpr kMaxVelocity = 1.0;
constexpr kMaxAcceleration = 1.0;
constexpr double kMaxVelocity = 1.0;
constexpr double kMaxAcceleration = 1.0;
bool ignore_max_velocity;

class Profile {
public:
struct ProfilePoint {
double position = 0.0;
double velocity = 0.0;
};

Profile(ProfilePoint current) {
current_ = current;
}
void SetGoal(ProfilePoint goal) { goal_ = goal; }
void SetGoal(ProfilePoint goal) {
goal_ = goal;
}

const double GetTime();
const double GetSetpoint();

private:
ProfilePoint current_;
Expand Down