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
8 changes: 7 additions & 1 deletion main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ int main() {
Profile::ProfilePoint current;
Profile::ProfilePoint goal;
{
// Position (distance) of 'goal'
goal.position = 1.0;
// Velocity at 'goal'
goal.velocity = 0.0;
}

Profile profile(current);
profile.SetGoal(goal);
std::cout << profile.GetTime() << std::endl;

// TRUE = will calculate using maximum velocity; FALSE = won't calculate using maximum velocity
bool use_max_velocity = true;
std::cout << profile.GetTime(current, goal, use_max_velocity) << std::endl;
}
46 changes: 44 additions & 2 deletions profile.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,47 @@
#include "profile.h"
#include <cmath>
#include <iostream>

const double Profile::GetTime() {
return 0.0;
// NOTE - This is a very rough draft, and not a finished product. These are just some of the calculations I will be using
const double Profile::GetTime(ProfilePoint current, ProfilePoint goal, bool use_max_velocity) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should just use the member variables

// 'a' = acceleration
double a = kMaxAcceleration;

// 't' = time
double t_accelerating;
double t_decelerating;
double t_constant_velocity;
double t_total;

// 'd' = distance
double d_accelerating;
double d_constant_velocity;
double d_decelerating;

// 'v' = velocity
double v_initial = current.velocity;
double v_max = kMaxVelocity;
double v_goal = goal.velocity;

// 'p' = position
double p_beginning = current.position;
double p_goal = goal.position;

if (use_max_velocity) {
// Time to reach the goal respecting ending at 0 velocity and `kMaxVelocity` and `kMaxAcceleration`
t_accelerating = std::abs(v_max-v_initial) / a;
t_decelerating = std::abs(v_goal-v_max) / a;

d_accelerating = 0.5 * std::abs(v_max + v_initial) * t_accelerating;
d_decelerating = 0.5 * std::abs(v_goal - v_max) * t_decelerating;

d_constant_velocity = p_goal - d_accelerating - d_decelerating;
t_constant_velocity = d_constant_velocity / v_max;

t_total = t_accelerating + t_constant_velocity + t_decelerating;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if there isn't time to reach the max velocity before reaching the goal?

} else {
t_total = (-v_initial + std::sqrt(std::pow(v_initial, 2) + (4 * .5 * a * goal.position))) / a;
}

return t_total;
}
13 changes: 9 additions & 4 deletions profile.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
#ifndef PROFILE_H_
#define PROFILE_H_

constexpr kMaxVelocity = 1.0;
constexpr kMaxAcceleration = 1.0;
constexpr double kMaxVelocity = 1.0;
constexpr double kMaxAcceleration = 1.0;

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

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

void SetGoal(ProfilePoint goal) {
goal_ = goal;
}

const double GetTime(ProfilePoint current, ProfilePoint goal, bool use_max_velocity);

private:
ProfilePoint current_;
Expand Down