BA
/
BaBoRo_test2
Backup 1
Motion.cpp@0:02dd72d1d465, 2018-04-24 (annotated)
- Committer:
- borlanic
- Date:
- Tue Apr 24 11:45:18 2018 +0000
- Revision:
- 0:02dd72d1d465
BaBoRo_test2 - backup 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
borlanic | 0:02dd72d1d465 | 1 | /* |
borlanic | 0:02dd72d1d465 | 2 | * Motion.cpp |
borlanic | 0:02dd72d1d465 | 3 | * Copyright (c) 2018, ZHAW |
borlanic | 0:02dd72d1d465 | 4 | * All rights reserved. |
borlanic | 0:02dd72d1d465 | 5 | */ |
borlanic | 0:02dd72d1d465 | 6 | |
borlanic | 0:02dd72d1d465 | 7 | #include <cmath> |
borlanic | 0:02dd72d1d465 | 8 | #include <algorithm> |
borlanic | 0:02dd72d1d465 | 9 | #include "Motion.h" |
borlanic | 0:02dd72d1d465 | 10 | |
borlanic | 0:02dd72d1d465 | 11 | using namespace std; |
borlanic | 0:02dd72d1d465 | 12 | |
borlanic | 0:02dd72d1d465 | 13 | const float Motion::DEFAULT_LIMIT = 1.0f; // default value for limits |
borlanic | 0:02dd72d1d465 | 14 | const float Motion::MINIMUM_LIMIT = 1.0e-9f; // smallest value allowed for limits |
borlanic | 0:02dd72d1d465 | 15 | |
borlanic | 0:02dd72d1d465 | 16 | /** |
borlanic | 0:02dd72d1d465 | 17 | * Creates a <code>Motion</code> object. |
borlanic | 0:02dd72d1d465 | 18 | * The values for position, velocity and acceleration are set to 0. |
borlanic | 0:02dd72d1d465 | 19 | */ |
borlanic | 0:02dd72d1d465 | 20 | Motion::Motion() { |
borlanic | 0:02dd72d1d465 | 21 | |
borlanic | 0:02dd72d1d465 | 22 | position = 0.0; |
borlanic | 0:02dd72d1d465 | 23 | velocity = 0.0f; |
borlanic | 0:02dd72d1d465 | 24 | |
borlanic | 0:02dd72d1d465 | 25 | profileVelocity = DEFAULT_LIMIT; |
borlanic | 0:02dd72d1d465 | 26 | profileAcceleration = DEFAULT_LIMIT; |
borlanic | 0:02dd72d1d465 | 27 | profileDeceleration = DEFAULT_LIMIT; |
borlanic | 0:02dd72d1d465 | 28 | } |
borlanic | 0:02dd72d1d465 | 29 | |
borlanic | 0:02dd72d1d465 | 30 | /** |
borlanic | 0:02dd72d1d465 | 31 | * Creates a <code>Motion</code> object with given values for position and velocity. |
borlanic | 0:02dd72d1d465 | 32 | * @param position the initial position value of this motion, given in [m] or [rad]. |
borlanic | 0:02dd72d1d465 | 33 | * @param velocity the initial velocity value of this motion, given in [m/s] or [rad/s]. |
borlanic | 0:02dd72d1d465 | 34 | */ |
borlanic | 0:02dd72d1d465 | 35 | Motion::Motion(double position, float velocity) { |
borlanic | 0:02dd72d1d465 | 36 | |
borlanic | 0:02dd72d1d465 | 37 | this->position = position; |
borlanic | 0:02dd72d1d465 | 38 | this->velocity = velocity; |
borlanic | 0:02dd72d1d465 | 39 | |
borlanic | 0:02dd72d1d465 | 40 | profileVelocity = DEFAULT_LIMIT; |
borlanic | 0:02dd72d1d465 | 41 | profileAcceleration = DEFAULT_LIMIT; |
borlanic | 0:02dd72d1d465 | 42 | profileDeceleration = DEFAULT_LIMIT; |
borlanic | 0:02dd72d1d465 | 43 | } |
borlanic | 0:02dd72d1d465 | 44 | |
borlanic | 0:02dd72d1d465 | 45 | /** |
borlanic | 0:02dd72d1d465 | 46 | * Creates a <code>Motion</code> object with given values for position and velocity. |
borlanic | 0:02dd72d1d465 | 47 | * @param motion another <code>Motion</code> object to copy the values from. |
borlanic | 0:02dd72d1d465 | 48 | */ |
borlanic | 0:02dd72d1d465 | 49 | Motion::Motion(const Motion& motion) { |
borlanic | 0:02dd72d1d465 | 50 | |
borlanic | 0:02dd72d1d465 | 51 | position = motion.position; |
borlanic | 0:02dd72d1d465 | 52 | velocity = motion.velocity; |
borlanic | 0:02dd72d1d465 | 53 | |
borlanic | 0:02dd72d1d465 | 54 | profileVelocity = motion.profileVelocity; |
borlanic | 0:02dd72d1d465 | 55 | profileAcceleration = motion.profileAcceleration; |
borlanic | 0:02dd72d1d465 | 56 | profileDeceleration = motion.profileDeceleration; |
borlanic | 0:02dd72d1d465 | 57 | } |
borlanic | 0:02dd72d1d465 | 58 | |
borlanic | 0:02dd72d1d465 | 59 | /** |
borlanic | 0:02dd72d1d465 | 60 | * Deletes the Motion object. |
borlanic | 0:02dd72d1d465 | 61 | */ |
borlanic | 0:02dd72d1d465 | 62 | Motion::~Motion() {} |
borlanic | 0:02dd72d1d465 | 63 | |
borlanic | 0:02dd72d1d465 | 64 | /** |
borlanic | 0:02dd72d1d465 | 65 | * Sets the values for position and velocity. |
borlanic | 0:02dd72d1d465 | 66 | * @param position the desired position value of this motion, given in [m] or [rad]. |
borlanic | 0:02dd72d1d465 | 67 | * @param velocity the desired velocity value of this motion, given in [m/s] or [rad/s]. |
borlanic | 0:02dd72d1d465 | 68 | */ |
borlanic | 0:02dd72d1d465 | 69 | void Motion::set(double position, float velocity) { |
borlanic | 0:02dd72d1d465 | 70 | |
borlanic | 0:02dd72d1d465 | 71 | this->position = position; |
borlanic | 0:02dd72d1d465 | 72 | this->velocity = velocity; |
borlanic | 0:02dd72d1d465 | 73 | } |
borlanic | 0:02dd72d1d465 | 74 | |
borlanic | 0:02dd72d1d465 | 75 | /** |
borlanic | 0:02dd72d1d465 | 76 | * Sets the values for position and velocity. |
borlanic | 0:02dd72d1d465 | 77 | * @param motion another <code>Motion</code> object to copy the values from. |
borlanic | 0:02dd72d1d465 | 78 | */ |
borlanic | 0:02dd72d1d465 | 79 | void Motion::set(const Motion& motion) { |
borlanic | 0:02dd72d1d465 | 80 | |
borlanic | 0:02dd72d1d465 | 81 | position = motion.position; |
borlanic | 0:02dd72d1d465 | 82 | velocity = motion.velocity; |
borlanic | 0:02dd72d1d465 | 83 | } |
borlanic | 0:02dd72d1d465 | 84 | |
borlanic | 0:02dd72d1d465 | 85 | /** |
borlanic | 0:02dd72d1d465 | 86 | * Sets the position value. |
borlanic | 0:02dd72d1d465 | 87 | * @param position the desired position value of this motion, given in [m] or [rad]. |
borlanic | 0:02dd72d1d465 | 88 | */ |
borlanic | 0:02dd72d1d465 | 89 | void Motion::setPosition(double position) { |
borlanic | 0:02dd72d1d465 | 90 | |
borlanic | 0:02dd72d1d465 | 91 | this->position = position; |
borlanic | 0:02dd72d1d465 | 92 | } |
borlanic | 0:02dd72d1d465 | 93 | |
borlanic | 0:02dd72d1d465 | 94 | /** |
borlanic | 0:02dd72d1d465 | 95 | * Gets the position value. |
borlanic | 0:02dd72d1d465 | 96 | * @return the position value of this motion, given in [m] or [rad]. |
borlanic | 0:02dd72d1d465 | 97 | */ |
borlanic | 0:02dd72d1d465 | 98 | double Motion::getPosition() { |
borlanic | 0:02dd72d1d465 | 99 | |
borlanic | 0:02dd72d1d465 | 100 | return position; |
borlanic | 0:02dd72d1d465 | 101 | } |
borlanic | 0:02dd72d1d465 | 102 | |
borlanic | 0:02dd72d1d465 | 103 | /** |
borlanic | 0:02dd72d1d465 | 104 | * Sets the velocity value. |
borlanic | 0:02dd72d1d465 | 105 | * @param velocity the desired velocity value of this motion, given in [m/s] or [rad/s]. |
borlanic | 0:02dd72d1d465 | 106 | */ |
borlanic | 0:02dd72d1d465 | 107 | void Motion::setVelocity(float velocity) { |
borlanic | 0:02dd72d1d465 | 108 | |
borlanic | 0:02dd72d1d465 | 109 | this->velocity = velocity; |
borlanic | 0:02dd72d1d465 | 110 | } |
borlanic | 0:02dd72d1d465 | 111 | |
borlanic | 0:02dd72d1d465 | 112 | /** |
borlanic | 0:02dd72d1d465 | 113 | * Gets the velocity value. |
borlanic | 0:02dd72d1d465 | 114 | * @return the velocity value of this motion, given in [m/s] or [rad/s]. |
borlanic | 0:02dd72d1d465 | 115 | */ |
borlanic | 0:02dd72d1d465 | 116 | float Motion::getVelocity() { |
borlanic | 0:02dd72d1d465 | 117 | |
borlanic | 0:02dd72d1d465 | 118 | return velocity; |
borlanic | 0:02dd72d1d465 | 119 | } |
borlanic | 0:02dd72d1d465 | 120 | |
borlanic | 0:02dd72d1d465 | 121 | /** |
borlanic | 0:02dd72d1d465 | 122 | * Sets the limit for the velocity value. |
borlanic | 0:02dd72d1d465 | 123 | * @param profileVelocity the limit of the velocity. |
borlanic | 0:02dd72d1d465 | 124 | */ |
borlanic | 0:02dd72d1d465 | 125 | void Motion::setProfileVelocity(float profileVelocity) { |
borlanic | 0:02dd72d1d465 | 126 | |
borlanic | 0:02dd72d1d465 | 127 | if (profileVelocity > MINIMUM_LIMIT) this->profileVelocity = profileVelocity; else this->profileVelocity = MINIMUM_LIMIT; |
borlanic | 0:02dd72d1d465 | 128 | } |
borlanic | 0:02dd72d1d465 | 129 | |
borlanic | 0:02dd72d1d465 | 130 | /** |
borlanic | 0:02dd72d1d465 | 131 | * Sets the limit for the acceleration value. |
borlanic | 0:02dd72d1d465 | 132 | * @param profileAcceleration the limit of the acceleration. |
borlanic | 0:02dd72d1d465 | 133 | */ |
borlanic | 0:02dd72d1d465 | 134 | void Motion::setProfileAcceleration(float profileAcceleration) { |
borlanic | 0:02dd72d1d465 | 135 | |
borlanic | 0:02dd72d1d465 | 136 | if (profileAcceleration > MINIMUM_LIMIT) this->profileAcceleration = profileAcceleration; else this->profileAcceleration = MINIMUM_LIMIT; |
borlanic | 0:02dd72d1d465 | 137 | } |
borlanic | 0:02dd72d1d465 | 138 | |
borlanic | 0:02dd72d1d465 | 139 | /** |
borlanic | 0:02dd72d1d465 | 140 | * Sets the limit for the deceleration value. |
borlanic | 0:02dd72d1d465 | 141 | * @param profileDeceleration the limit of the deceleration. |
borlanic | 0:02dd72d1d465 | 142 | */ |
borlanic | 0:02dd72d1d465 | 143 | void Motion::setProfileDeceleration(float profileDeceleration) { |
borlanic | 0:02dd72d1d465 | 144 | |
borlanic | 0:02dd72d1d465 | 145 | if (profileDeceleration > MINIMUM_LIMIT) this->profileDeceleration = profileDeceleration; else this->profileDeceleration = MINIMUM_LIMIT; |
borlanic | 0:02dd72d1d465 | 146 | } |
borlanic | 0:02dd72d1d465 | 147 | |
borlanic | 0:02dd72d1d465 | 148 | /** |
borlanic | 0:02dd72d1d465 | 149 | * Sets the limits for velocity, acceleration and deceleration values. |
borlanic | 0:02dd72d1d465 | 150 | * @param profileVelocity the limit of the velocity. |
borlanic | 0:02dd72d1d465 | 151 | * @param profileAcceleration the limit of the acceleration. |
borlanic | 0:02dd72d1d465 | 152 | * @param profileDeceleration the limit of the deceleration. |
borlanic | 0:02dd72d1d465 | 153 | */ |
borlanic | 0:02dd72d1d465 | 154 | void Motion::setLimits(float profileVelocity, float profileAcceleration, float profileDeceleration) { |
borlanic | 0:02dd72d1d465 | 155 | |
borlanic | 0:02dd72d1d465 | 156 | if (profileVelocity > MINIMUM_LIMIT) this->profileVelocity = profileVelocity; else this->profileVelocity = MINIMUM_LIMIT; |
borlanic | 0:02dd72d1d465 | 157 | if (profileAcceleration > MINIMUM_LIMIT) this->profileAcceleration = profileAcceleration; else this->profileAcceleration = MINIMUM_LIMIT; |
borlanic | 0:02dd72d1d465 | 158 | if (profileDeceleration > MINIMUM_LIMIT) this->profileDeceleration = profileDeceleration; else this->profileDeceleration = MINIMUM_LIMIT; |
borlanic | 0:02dd72d1d465 | 159 | } |
borlanic | 0:02dd72d1d465 | 160 | |
borlanic | 0:02dd72d1d465 | 161 | /** |
borlanic | 0:02dd72d1d465 | 162 | * Gets the time needed to move to a given target position. |
borlanic | 0:02dd72d1d465 | 163 | * @param targetPosition the desired target position given in [m] or [rad]. |
borlanic | 0:02dd72d1d465 | 164 | * @return the time to move to the target position, given in [s]. |
borlanic | 0:02dd72d1d465 | 165 | */ |
borlanic | 0:02dd72d1d465 | 166 | float Motion::getTimeToPosition(double targetPosition) { |
borlanic | 0:02dd72d1d465 | 167 | |
borlanic | 0:02dd72d1d465 | 168 | // calculate position, when velocity is reduced to zero |
borlanic | 0:02dd72d1d465 | 169 | |
borlanic | 0:02dd72d1d465 | 170 | double stopPosition = (velocity > 0.0f) ? position+(double)(velocity*velocity/profileDeceleration*0.5f) : position-(double)(velocity*velocity/profileDeceleration*0.5f); |
borlanic | 0:02dd72d1d465 | 171 | |
borlanic | 0:02dd72d1d465 | 172 | if (targetPosition > stopPosition) { // positive velocity required |
borlanic | 0:02dd72d1d465 | 173 | |
borlanic | 0:02dd72d1d465 | 174 | if (velocity > profileVelocity) { // slow down to profile velocity first |
borlanic | 0:02dd72d1d465 | 175 | |
borlanic | 0:02dd72d1d465 | 176 | float t1 = (velocity-profileVelocity)/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 177 | float t2 = (float)(targetPosition-stopPosition)/profileVelocity; |
borlanic | 0:02dd72d1d465 | 178 | float t3 = profileVelocity/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 179 | |
borlanic | 0:02dd72d1d465 | 180 | return t1+t2+t3; |
borlanic | 0:02dd72d1d465 | 181 | |
borlanic | 0:02dd72d1d465 | 182 | } else if (velocity > 0.0f) { // speed up to profile velocity |
borlanic | 0:02dd72d1d465 | 183 | |
borlanic | 0:02dd72d1d465 | 184 | float t1 = (profileVelocity-velocity)/profileAcceleration; |
borlanic | 0:02dd72d1d465 | 185 | float t3 = profileVelocity/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 186 | float t2 = ((float)(targetPosition-position)-(velocity+profileVelocity)*0.5f*t1)/profileVelocity-0.5f*t3; |
borlanic | 0:02dd72d1d465 | 187 | |
borlanic | 0:02dd72d1d465 | 188 | if (t2 < 0.0f) { |
borlanic | 0:02dd72d1d465 | 189 | float maxVelocity = sqrt((2.0f*(float)(targetPosition-position)*profileAcceleration+velocity*velocity)*profileDeceleration/(profileAcceleration+profileDeceleration)); |
borlanic | 0:02dd72d1d465 | 190 | t1 = (maxVelocity-velocity)/profileAcceleration; |
borlanic | 0:02dd72d1d465 | 191 | t2 = 0.0f; |
borlanic | 0:02dd72d1d465 | 192 | t3 = maxVelocity/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 193 | } |
borlanic | 0:02dd72d1d465 | 194 | |
borlanic | 0:02dd72d1d465 | 195 | return t1+t2+t3; |
borlanic | 0:02dd72d1d465 | 196 | |
borlanic | 0:02dd72d1d465 | 197 | } else { // slow down to zero first, and then speed up to profile velocity |
borlanic | 0:02dd72d1d465 | 198 | |
borlanic | 0:02dd72d1d465 | 199 | float t1 = -velocity/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 200 | float t2 = profileVelocity/profileAcceleration; |
borlanic | 0:02dd72d1d465 | 201 | float t4 = profileVelocity/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 202 | float t3 = ((float)(targetPosition-position)-velocity*0.5f*t1)/profileVelocity-0.5f*(t2+t4); |
borlanic | 0:02dd72d1d465 | 203 | |
borlanic | 0:02dd72d1d465 | 204 | if (t3 < 0.0f) { |
borlanic | 0:02dd72d1d465 | 205 | float maxVelocity = sqrt((2.0f*(float)(targetPosition-position)*profileDeceleration+velocity*velocity)*profileAcceleration/(profileAcceleration+profileDeceleration)); |
borlanic | 0:02dd72d1d465 | 206 | t2 = maxVelocity/profileAcceleration; |
borlanic | 0:02dd72d1d465 | 207 | t3 = 0.0f; |
borlanic | 0:02dd72d1d465 | 208 | t4 = maxVelocity/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 209 | } |
borlanic | 0:02dd72d1d465 | 210 | |
borlanic | 0:02dd72d1d465 | 211 | return t1+t2+t3+t4; |
borlanic | 0:02dd72d1d465 | 212 | } |
borlanic | 0:02dd72d1d465 | 213 | |
borlanic | 0:02dd72d1d465 | 214 | } else { // negative velocity required |
borlanic | 0:02dd72d1d465 | 215 | |
borlanic | 0:02dd72d1d465 | 216 | if (velocity < -profileVelocity) { // slow down to (negative) profile velocity first |
borlanic | 0:02dd72d1d465 | 217 | |
borlanic | 0:02dd72d1d465 | 218 | float t1 = (-profileVelocity-velocity)/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 219 | float t2 = (float)(stopPosition-targetPosition)/profileVelocity; |
borlanic | 0:02dd72d1d465 | 220 | float t3 = profileVelocity/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 221 | |
borlanic | 0:02dd72d1d465 | 222 | return t1+t2+t3; |
borlanic | 0:02dd72d1d465 | 223 | |
borlanic | 0:02dd72d1d465 | 224 | } else if (velocity < 0.0f) { // speed up to (negative) profile velocity |
borlanic | 0:02dd72d1d465 | 225 | |
borlanic | 0:02dd72d1d465 | 226 | float t1 = (velocity+profileVelocity)/profileAcceleration; |
borlanic | 0:02dd72d1d465 | 227 | float t3 = profileVelocity/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 228 | float t2 = ((float)(position-targetPosition)+(velocity-profileVelocity)*0.5f*t1)/profileVelocity-0.5f*t3; |
borlanic | 0:02dd72d1d465 | 229 | |
borlanic | 0:02dd72d1d465 | 230 | if (t2 < 0.0f) { |
borlanic | 0:02dd72d1d465 | 231 | float minVelocity = -sqrt((-2.0f*(float)(targetPosition-position)*profileAcceleration+velocity*velocity)*profileDeceleration/(profileAcceleration+profileDeceleration)); |
borlanic | 0:02dd72d1d465 | 232 | t1 = (velocity-minVelocity)/profileAcceleration; |
borlanic | 0:02dd72d1d465 | 233 | t2 = 0.0f; |
borlanic | 0:02dd72d1d465 | 234 | t3 = -minVelocity/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 235 | } |
borlanic | 0:02dd72d1d465 | 236 | |
borlanic | 0:02dd72d1d465 | 237 | return t1+t2+t3; |
borlanic | 0:02dd72d1d465 | 238 | |
borlanic | 0:02dd72d1d465 | 239 | } else { // slow down to zero first, and then speed up to (negative) profile velocity |
borlanic | 0:02dd72d1d465 | 240 | |
borlanic | 0:02dd72d1d465 | 241 | float t1 = velocity/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 242 | float t2 = profileVelocity/profileAcceleration; |
borlanic | 0:02dd72d1d465 | 243 | float t4 = profileVelocity/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 244 | float t3 = (-(float)(targetPosition-position)+velocity*0.5f*t1)/profileVelocity-0.5f*(t2+t4); |
borlanic | 0:02dd72d1d465 | 245 | |
borlanic | 0:02dd72d1d465 | 246 | if (t3 < 0.0f) { |
borlanic | 0:02dd72d1d465 | 247 | float minVelocity = -sqrt((-2.0f*(float)(targetPosition-position)*profileDeceleration+velocity*velocity)*profileAcceleration/(profileAcceleration+profileDeceleration)); |
borlanic | 0:02dd72d1d465 | 248 | t2 = -minVelocity/profileAcceleration; |
borlanic | 0:02dd72d1d465 | 249 | t3 = 0.0f; |
borlanic | 0:02dd72d1d465 | 250 | t4 = -minVelocity/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 251 | } |
borlanic | 0:02dd72d1d465 | 252 | |
borlanic | 0:02dd72d1d465 | 253 | return t1+t2+t3+t4; |
borlanic | 0:02dd72d1d465 | 254 | } |
borlanic | 0:02dd72d1d465 | 255 | } |
borlanic | 0:02dd72d1d465 | 256 | } |
borlanic | 0:02dd72d1d465 | 257 | |
borlanic | 0:02dd72d1d465 | 258 | /** |
borlanic | 0:02dd72d1d465 | 259 | * Increments the current motion towards a given target velocity. |
borlanic | 0:02dd72d1d465 | 260 | * @param targetVelocity the desired target velocity given in [m/s] or [rad/s]. |
borlanic | 0:02dd72d1d465 | 261 | * @param period the time period to increment the motion values for, given in [s]. |
borlanic | 0:02dd72d1d465 | 262 | */ |
borlanic | 0:02dd72d1d465 | 263 | void Motion::incrementToVelocity(float targetVelocity, float period) { |
borlanic | 0:02dd72d1d465 | 264 | |
borlanic | 0:02dd72d1d465 | 265 | if (targetVelocity < -profileVelocity) targetVelocity = -profileVelocity; |
borlanic | 0:02dd72d1d465 | 266 | else if (targetVelocity > profileVelocity) targetVelocity = profileVelocity; |
borlanic | 0:02dd72d1d465 | 267 | |
borlanic | 0:02dd72d1d465 | 268 | if (targetVelocity > 0.0f) { |
borlanic | 0:02dd72d1d465 | 269 | |
borlanic | 0:02dd72d1d465 | 270 | if (velocity > targetVelocity) { // slow down to target velocity |
borlanic | 0:02dd72d1d465 | 271 | |
borlanic | 0:02dd72d1d465 | 272 | float t1 = (velocity-targetVelocity)/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 273 | |
borlanic | 0:02dd72d1d465 | 274 | if (t1 > period) { |
borlanic | 0:02dd72d1d465 | 275 | position += (double)((velocity-profileDeceleration*0.5f*period)*period); |
borlanic | 0:02dd72d1d465 | 276 | velocity += -profileDeceleration*period; |
borlanic | 0:02dd72d1d465 | 277 | } else { |
borlanic | 0:02dd72d1d465 | 278 | position += (double)((velocity-profileDeceleration*0.5f*t1)*t1); |
borlanic | 0:02dd72d1d465 | 279 | velocity += -profileDeceleration*t1; |
borlanic | 0:02dd72d1d465 | 280 | position += (double)(velocity*(period-t1)); |
borlanic | 0:02dd72d1d465 | 281 | } |
borlanic | 0:02dd72d1d465 | 282 | |
borlanic | 0:02dd72d1d465 | 283 | } else if (velocity > 0.0f) { // speed up to target velocity |
borlanic | 0:02dd72d1d465 | 284 | |
borlanic | 0:02dd72d1d465 | 285 | float t1 = (targetVelocity-velocity)/profileAcceleration; |
borlanic | 0:02dd72d1d465 | 286 | |
borlanic | 0:02dd72d1d465 | 287 | if (t1 > period) { |
borlanic | 0:02dd72d1d465 | 288 | position += (double)((velocity+profileAcceleration*0.5f*period)*period); |
borlanic | 0:02dd72d1d465 | 289 | velocity += profileAcceleration*period; |
borlanic | 0:02dd72d1d465 | 290 | } else { |
borlanic | 0:02dd72d1d465 | 291 | position += (double)((velocity+profileAcceleration*0.5f*t1)*t1); |
borlanic | 0:02dd72d1d465 | 292 | velocity += profileAcceleration*t1; |
borlanic | 0:02dd72d1d465 | 293 | position += (double)(velocity*(period-t1)); |
borlanic | 0:02dd72d1d465 | 294 | } |
borlanic | 0:02dd72d1d465 | 295 | |
borlanic | 0:02dd72d1d465 | 296 | } else { // slow down to zero first, and then speed up to target velocity |
borlanic | 0:02dd72d1d465 | 297 | |
borlanic | 0:02dd72d1d465 | 298 | float t1 = -velocity/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 299 | float t2 = targetVelocity/profileAcceleration; |
borlanic | 0:02dd72d1d465 | 300 | |
borlanic | 0:02dd72d1d465 | 301 | if (t1 > period) { |
borlanic | 0:02dd72d1d465 | 302 | position += (double)((velocity+profileDeceleration*0.5f*period)*period); |
borlanic | 0:02dd72d1d465 | 303 | velocity += profileDeceleration*period; |
borlanic | 0:02dd72d1d465 | 304 | } else if (t1+t2 > period) { |
borlanic | 0:02dd72d1d465 | 305 | position += (double)((velocity+profileDeceleration*0.5f*t1)*t1); |
borlanic | 0:02dd72d1d465 | 306 | velocity += profileDeceleration*t1; |
borlanic | 0:02dd72d1d465 | 307 | position += (double)((velocity+profileAcceleration*0.5f*(period-t1))*(period-t1)); |
borlanic | 0:02dd72d1d465 | 308 | velocity += profileAcceleration*(period-t1); |
borlanic | 0:02dd72d1d465 | 309 | } else { |
borlanic | 0:02dd72d1d465 | 310 | position += (double)((velocity+profileDeceleration*0.5f*t1)*t1); |
borlanic | 0:02dd72d1d465 | 311 | velocity += profileDeceleration*t1; |
borlanic | 0:02dd72d1d465 | 312 | position += (double)((velocity+profileAcceleration*0.5f*t2)*t2); |
borlanic | 0:02dd72d1d465 | 313 | velocity += profileAcceleration*t2; |
borlanic | 0:02dd72d1d465 | 314 | position += (double)(velocity*(period-t1-t2)); |
borlanic | 0:02dd72d1d465 | 315 | } |
borlanic | 0:02dd72d1d465 | 316 | } |
borlanic | 0:02dd72d1d465 | 317 | |
borlanic | 0:02dd72d1d465 | 318 | } else { |
borlanic | 0:02dd72d1d465 | 319 | |
borlanic | 0:02dd72d1d465 | 320 | if (velocity < targetVelocity) { // slow down to (negative) target velocity |
borlanic | 0:02dd72d1d465 | 321 | |
borlanic | 0:02dd72d1d465 | 322 | float t1 = (targetVelocity-velocity)/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 323 | |
borlanic | 0:02dd72d1d465 | 324 | if (t1 > period) { |
borlanic | 0:02dd72d1d465 | 325 | position += (double)((velocity+profileDeceleration*0.5f*period)*period); |
borlanic | 0:02dd72d1d465 | 326 | velocity += profileDeceleration*period; |
borlanic | 0:02dd72d1d465 | 327 | } else { |
borlanic | 0:02dd72d1d465 | 328 | position += (double)((velocity+profileDeceleration*0.5f*t1)*t1); |
borlanic | 0:02dd72d1d465 | 329 | velocity += profileDeceleration*t1; |
borlanic | 0:02dd72d1d465 | 330 | position += (double)(velocity*(period-t1)); |
borlanic | 0:02dd72d1d465 | 331 | } |
borlanic | 0:02dd72d1d465 | 332 | |
borlanic | 0:02dd72d1d465 | 333 | } else if (velocity < 0.0f) { // speed up to (negative) target velocity |
borlanic | 0:02dd72d1d465 | 334 | |
borlanic | 0:02dd72d1d465 | 335 | float t1 = (velocity-targetVelocity)/profileAcceleration; |
borlanic | 0:02dd72d1d465 | 336 | |
borlanic | 0:02dd72d1d465 | 337 | if (t1 > period) { |
borlanic | 0:02dd72d1d465 | 338 | position += (double)((velocity-profileAcceleration*0.5f*period)*period); |
borlanic | 0:02dd72d1d465 | 339 | velocity += -profileAcceleration*period; |
borlanic | 0:02dd72d1d465 | 340 | } else { |
borlanic | 0:02dd72d1d465 | 341 | position += (double)((velocity-profileAcceleration*0.5f*t1)*t1); |
borlanic | 0:02dd72d1d465 | 342 | velocity += -profileAcceleration*t1; |
borlanic | 0:02dd72d1d465 | 343 | position += (double)(velocity*(period-t1)); |
borlanic | 0:02dd72d1d465 | 344 | } |
borlanic | 0:02dd72d1d465 | 345 | |
borlanic | 0:02dd72d1d465 | 346 | } else { // slow down to zero first, and then speed up to (negative) target velocity |
borlanic | 0:02dd72d1d465 | 347 | |
borlanic | 0:02dd72d1d465 | 348 | float t1 = velocity/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 349 | float t2 = -targetVelocity/profileAcceleration; |
borlanic | 0:02dd72d1d465 | 350 | |
borlanic | 0:02dd72d1d465 | 351 | if (t1 > period) { |
borlanic | 0:02dd72d1d465 | 352 | position += (double)((velocity-profileDeceleration*0.5f*period)*period); |
borlanic | 0:02dd72d1d465 | 353 | velocity += -profileDeceleration*period; |
borlanic | 0:02dd72d1d465 | 354 | } else if (t1+t2 > period) { |
borlanic | 0:02dd72d1d465 | 355 | position += (double)((velocity-profileDeceleration*0.5f*t1)*t1); |
borlanic | 0:02dd72d1d465 | 356 | velocity += -profileDeceleration*t1; |
borlanic | 0:02dd72d1d465 | 357 | position += (double)((velocity-profileAcceleration*0.5f*(period-t1))*(period-t1)); |
borlanic | 0:02dd72d1d465 | 358 | velocity += -profileAcceleration*(period-t1); |
borlanic | 0:02dd72d1d465 | 359 | } else { |
borlanic | 0:02dd72d1d465 | 360 | position += (double)((velocity-profileDeceleration*0.5f*t1)*t1); |
borlanic | 0:02dd72d1d465 | 361 | velocity += -profileDeceleration*t1; |
borlanic | 0:02dd72d1d465 | 362 | position += (double)((velocity-profileAcceleration*0.5f*t2)*t2); |
borlanic | 0:02dd72d1d465 | 363 | velocity += -profileAcceleration*t2; |
borlanic | 0:02dd72d1d465 | 364 | position += (double)(velocity*(period-t1-t2)); |
borlanic | 0:02dd72d1d465 | 365 | } |
borlanic | 0:02dd72d1d465 | 366 | } |
borlanic | 0:02dd72d1d465 | 367 | } |
borlanic | 0:02dd72d1d465 | 368 | } |
borlanic | 0:02dd72d1d465 | 369 | |
borlanic | 0:02dd72d1d465 | 370 | /** |
borlanic | 0:02dd72d1d465 | 371 | * Increments the current motion towards a given target position. |
borlanic | 0:02dd72d1d465 | 372 | * @param targetPosition the desired target position given in [m] or [rad]. |
borlanic | 0:02dd72d1d465 | 373 | * @param period the time period to increment the motion values for, given in [s]. |
borlanic | 0:02dd72d1d465 | 374 | */ |
borlanic | 0:02dd72d1d465 | 375 | void Motion::incrementToPosition(double targetPosition, float period) { |
borlanic | 0:02dd72d1d465 | 376 | |
borlanic | 0:02dd72d1d465 | 377 | // calculate position, when velocity is reduced to zero |
borlanic | 0:02dd72d1d465 | 378 | |
borlanic | 0:02dd72d1d465 | 379 | double stopPosition = (velocity > 0.0f) ? position+(double)(velocity*velocity/profileDeceleration*0.5f) : position-(double)(velocity*velocity/profileDeceleration*0.5f); |
borlanic | 0:02dd72d1d465 | 380 | |
borlanic | 0:02dd72d1d465 | 381 | if (targetPosition > stopPosition) { // positive velocity required |
borlanic | 0:02dd72d1d465 | 382 | |
borlanic | 0:02dd72d1d465 | 383 | if (velocity > profileVelocity) { // slow down to profile velocity first |
borlanic | 0:02dd72d1d465 | 384 | |
borlanic | 0:02dd72d1d465 | 385 | float t1 = (velocity-profileVelocity)/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 386 | float t2 = (float)(targetPosition-stopPosition)/profileVelocity; |
borlanic | 0:02dd72d1d465 | 387 | float t3 = profileVelocity/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 388 | |
borlanic | 0:02dd72d1d465 | 389 | if (t1 > period) { |
borlanic | 0:02dd72d1d465 | 390 | position += (double)((velocity-profileDeceleration*0.5f*period)*period); |
borlanic | 0:02dd72d1d465 | 391 | velocity += -profileDeceleration*period; |
borlanic | 0:02dd72d1d465 | 392 | } else if (t1+t2 > period) { |
borlanic | 0:02dd72d1d465 | 393 | position += (double)((velocity-profileDeceleration*0.5f*t1)*t1); |
borlanic | 0:02dd72d1d465 | 394 | velocity += -profileDeceleration*t1; |
borlanic | 0:02dd72d1d465 | 395 | position += (double)(velocity*(period-t1)); |
borlanic | 0:02dd72d1d465 | 396 | } else if (t1+t2+t3 > period) { |
borlanic | 0:02dd72d1d465 | 397 | position += (double)((velocity-profileDeceleration*0.5f*t1)*t1); |
borlanic | 0:02dd72d1d465 | 398 | velocity += -profileDeceleration*t1; |
borlanic | 0:02dd72d1d465 | 399 | position += (double)(velocity*t2); |
borlanic | 0:02dd72d1d465 | 400 | position += (double)((velocity-profileDeceleration*0.5f*(period-t1-t2))*(period-t1-t2)); |
borlanic | 0:02dd72d1d465 | 401 | velocity += -profileDeceleration*(period-t1-t2); |
borlanic | 0:02dd72d1d465 | 402 | } else { |
borlanic | 0:02dd72d1d465 | 403 | position += (double)((velocity-profileDeceleration*0.5f*t1)*t1); |
borlanic | 0:02dd72d1d465 | 404 | velocity += -profileDeceleration*t1; |
borlanic | 0:02dd72d1d465 | 405 | position += (double)(velocity*t2); |
borlanic | 0:02dd72d1d465 | 406 | position += (double)((velocity-profileDeceleration*0.5f*t3)*t3); |
borlanic | 0:02dd72d1d465 | 407 | velocity += -profileDeceleration*t3; |
borlanic | 0:02dd72d1d465 | 408 | } |
borlanic | 0:02dd72d1d465 | 409 | |
borlanic | 0:02dd72d1d465 | 410 | } else if (velocity > 0.0f) { // speed up to profile velocity |
borlanic | 0:02dd72d1d465 | 411 | |
borlanic | 0:02dd72d1d465 | 412 | float t1 = (profileVelocity-velocity)/profileAcceleration; |
borlanic | 0:02dd72d1d465 | 413 | float t3 = profileVelocity/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 414 | float t2 = ((float)(targetPosition-position)-(velocity+profileVelocity)*0.5f*t1)/profileVelocity-0.5f*t3; |
borlanic | 0:02dd72d1d465 | 415 | |
borlanic | 0:02dd72d1d465 | 416 | if (t2 < 0.0f) { |
borlanic | 0:02dd72d1d465 | 417 | float maxVelocity = sqrt((2.0f*(float)(targetPosition-position)*profileAcceleration+velocity*velocity)*profileDeceleration/(profileAcceleration+profileDeceleration)); |
borlanic | 0:02dd72d1d465 | 418 | t1 = (maxVelocity-velocity)/profileAcceleration; |
borlanic | 0:02dd72d1d465 | 419 | t2 = 0.0f; |
borlanic | 0:02dd72d1d465 | 420 | t3 = maxVelocity/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 421 | } |
borlanic | 0:02dd72d1d465 | 422 | |
borlanic | 0:02dd72d1d465 | 423 | if (t1 > period) { |
borlanic | 0:02dd72d1d465 | 424 | position += (double)((velocity+profileAcceleration*0.5f*period)*period); |
borlanic | 0:02dd72d1d465 | 425 | velocity += profileAcceleration*period; |
borlanic | 0:02dd72d1d465 | 426 | } else if (t1+t2 > period) { |
borlanic | 0:02dd72d1d465 | 427 | position += (double)((velocity+profileAcceleration*0.5f*t1)*t1); |
borlanic | 0:02dd72d1d465 | 428 | velocity += profileAcceleration*t1; |
borlanic | 0:02dd72d1d465 | 429 | position += (double)(velocity*(period-t1)); |
borlanic | 0:02dd72d1d465 | 430 | } else if (t1+t2+t3 > period) { |
borlanic | 0:02dd72d1d465 | 431 | position += (double)((velocity+profileAcceleration*0.5f*t1)*t1); |
borlanic | 0:02dd72d1d465 | 432 | velocity += profileAcceleration*t1; |
borlanic | 0:02dd72d1d465 | 433 | position += (double)(velocity*t2); |
borlanic | 0:02dd72d1d465 | 434 | position += (double)((velocity-profileDeceleration*0.5f*(period-t1-t2))*(period-t1-t2)); |
borlanic | 0:02dd72d1d465 | 435 | velocity += -profileDeceleration*(period-t1-t2); |
borlanic | 0:02dd72d1d465 | 436 | } else { |
borlanic | 0:02dd72d1d465 | 437 | position += (double)((velocity+profileAcceleration*0.5f*t1)*t1); |
borlanic | 0:02dd72d1d465 | 438 | velocity += profileAcceleration*t1; |
borlanic | 0:02dd72d1d465 | 439 | position += (double)(velocity*t2); |
borlanic | 0:02dd72d1d465 | 440 | position += (double)((velocity-profileDeceleration*0.5f*t3)*t3); |
borlanic | 0:02dd72d1d465 | 441 | velocity += -profileDeceleration*t3; |
borlanic | 0:02dd72d1d465 | 442 | } |
borlanic | 0:02dd72d1d465 | 443 | |
borlanic | 0:02dd72d1d465 | 444 | } else { // slow down to zero first, and then speed up to profile velocity |
borlanic | 0:02dd72d1d465 | 445 | |
borlanic | 0:02dd72d1d465 | 446 | float t1 = -velocity/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 447 | float t2 = profileVelocity/profileAcceleration; |
borlanic | 0:02dd72d1d465 | 448 | float t4 = profileVelocity/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 449 | float t3 = ((float)(targetPosition-position)-velocity*0.5f*t1)/profileVelocity-0.5f*(t2+t4); |
borlanic | 0:02dd72d1d465 | 450 | |
borlanic | 0:02dd72d1d465 | 451 | if (t3 < 0.0f) { |
borlanic | 0:02dd72d1d465 | 452 | float maxVelocity = sqrt((2.0f*(float)(targetPosition-position)*profileDeceleration+velocity*velocity)*profileAcceleration/(profileAcceleration+profileDeceleration)); |
borlanic | 0:02dd72d1d465 | 453 | t2 = maxVelocity/profileAcceleration; |
borlanic | 0:02dd72d1d465 | 454 | t3 = 0.0f; |
borlanic | 0:02dd72d1d465 | 455 | t4 = maxVelocity/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 456 | } |
borlanic | 0:02dd72d1d465 | 457 | |
borlanic | 0:02dd72d1d465 | 458 | if (t1 > period) { |
borlanic | 0:02dd72d1d465 | 459 | position += (double)((velocity+profileDeceleration*0.5f*period)*period); |
borlanic | 0:02dd72d1d465 | 460 | velocity += profileDeceleration*period; |
borlanic | 0:02dd72d1d465 | 461 | } else if (t1+t2 > period) { |
borlanic | 0:02dd72d1d465 | 462 | position += (double)((velocity+profileDeceleration*0.5f*t1)*t1); |
borlanic | 0:02dd72d1d465 | 463 | velocity += profileDeceleration*t1; |
borlanic | 0:02dd72d1d465 | 464 | position += (double)((velocity+profileAcceleration*0.5f*(period-t1))*(period-t1)); |
borlanic | 0:02dd72d1d465 | 465 | velocity += profileAcceleration*(period-t1); |
borlanic | 0:02dd72d1d465 | 466 | } else if (t1+t2+t3 > period) { |
borlanic | 0:02dd72d1d465 | 467 | position += (double)((velocity+profileDeceleration*0.5f*t1)*t1); |
borlanic | 0:02dd72d1d465 | 468 | velocity += profileDeceleration*t1; |
borlanic | 0:02dd72d1d465 | 469 | position += (double)((velocity+profileAcceleration*0.5f*t2)*t2); |
borlanic | 0:02dd72d1d465 | 470 | velocity += profileAcceleration*t2; |
borlanic | 0:02dd72d1d465 | 471 | position += (double)(velocity*(period-t1-t2)); |
borlanic | 0:02dd72d1d465 | 472 | } else if (t1+t2+t3+t4 > period) { |
borlanic | 0:02dd72d1d465 | 473 | position += (double)((velocity+profileDeceleration*0.5f*t1)*t1); |
borlanic | 0:02dd72d1d465 | 474 | velocity += profileDeceleration*t1; |
borlanic | 0:02dd72d1d465 | 475 | position += (double)((velocity+profileAcceleration*0.5f*t2)*t2); |
borlanic | 0:02dd72d1d465 | 476 | velocity += profileAcceleration*t2; |
borlanic | 0:02dd72d1d465 | 477 | position += (double)(velocity*t3); |
borlanic | 0:02dd72d1d465 | 478 | position += (double)((velocity-profileDeceleration*0.5f*(period-t1-t2-t3))*(period-t1-t2-t3)); |
borlanic | 0:02dd72d1d465 | 479 | velocity += -profileDeceleration*(period-t1-t2-t3); |
borlanic | 0:02dd72d1d465 | 480 | } else { |
borlanic | 0:02dd72d1d465 | 481 | position += (double)((velocity+profileDeceleration*0.5f*t1)*t1); |
borlanic | 0:02dd72d1d465 | 482 | velocity += profileDeceleration*t1; |
borlanic | 0:02dd72d1d465 | 483 | position += (double)((velocity+profileAcceleration*0.5f*t2)*t2); |
borlanic | 0:02dd72d1d465 | 484 | velocity += profileAcceleration*t2; |
borlanic | 0:02dd72d1d465 | 485 | position += (double)(velocity*t3); |
borlanic | 0:02dd72d1d465 | 486 | position += (double)((velocity-profileDeceleration*0.5f*t4)*t4); |
borlanic | 0:02dd72d1d465 | 487 | velocity += -profileDeceleration*t4; |
borlanic | 0:02dd72d1d465 | 488 | } |
borlanic | 0:02dd72d1d465 | 489 | } |
borlanic | 0:02dd72d1d465 | 490 | |
borlanic | 0:02dd72d1d465 | 491 | } else { // negative velocity required |
borlanic | 0:02dd72d1d465 | 492 | |
borlanic | 0:02dd72d1d465 | 493 | if (velocity < -profileVelocity) { // slow down to (negative) profile velocity first |
borlanic | 0:02dd72d1d465 | 494 | |
borlanic | 0:02dd72d1d465 | 495 | float t1 = (-profileVelocity-velocity)/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 496 | float t2 = (float)(stopPosition-targetPosition)/profileVelocity; |
borlanic | 0:02dd72d1d465 | 497 | float t3 = profileVelocity/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 498 | |
borlanic | 0:02dd72d1d465 | 499 | if (t1 > period) { |
borlanic | 0:02dd72d1d465 | 500 | position += (double)((velocity+profileDeceleration*0.5f*period)*period); |
borlanic | 0:02dd72d1d465 | 501 | velocity += profileDeceleration*period; |
borlanic | 0:02dd72d1d465 | 502 | } else if (t1+t2 > period) { |
borlanic | 0:02dd72d1d465 | 503 | position += (double)((velocity+profileDeceleration*0.5f*t1)*t1); |
borlanic | 0:02dd72d1d465 | 504 | velocity += profileDeceleration*t1; |
borlanic | 0:02dd72d1d465 | 505 | position += (double)(velocity*(period-t1)); |
borlanic | 0:02dd72d1d465 | 506 | } else if (t1+t2+t3 > period) { |
borlanic | 0:02dd72d1d465 | 507 | position += (double)((velocity+profileDeceleration*0.5f*t1)*t1); |
borlanic | 0:02dd72d1d465 | 508 | velocity += profileDeceleration*t1; |
borlanic | 0:02dd72d1d465 | 509 | position += (double)(velocity*t2); |
borlanic | 0:02dd72d1d465 | 510 | position += (double)((velocity+profileDeceleration*0.5f*(period-t1-t2))*(period-t1-t2)); |
borlanic | 0:02dd72d1d465 | 511 | velocity += profileDeceleration*(period-t1-t2); |
borlanic | 0:02dd72d1d465 | 512 | } else { |
borlanic | 0:02dd72d1d465 | 513 | position += (double)((velocity+profileDeceleration*0.5f*t1)*t1); |
borlanic | 0:02dd72d1d465 | 514 | velocity += profileDeceleration*t1; |
borlanic | 0:02dd72d1d465 | 515 | position += (double)(velocity*t2); |
borlanic | 0:02dd72d1d465 | 516 | position += (double)((velocity+profileDeceleration*0.5f*t3)*t3); |
borlanic | 0:02dd72d1d465 | 517 | velocity += profileDeceleration*t3; |
borlanic | 0:02dd72d1d465 | 518 | } |
borlanic | 0:02dd72d1d465 | 519 | |
borlanic | 0:02dd72d1d465 | 520 | } else if (velocity < 0.0f) { // speed up to (negative) profile velocity |
borlanic | 0:02dd72d1d465 | 521 | |
borlanic | 0:02dd72d1d465 | 522 | float t1 = (velocity+profileVelocity)/profileAcceleration; |
borlanic | 0:02dd72d1d465 | 523 | float t3 = profileVelocity/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 524 | float t2 = ((float)(position-targetPosition)+(velocity-profileVelocity)*0.5f*t1)/profileVelocity-0.5f*t3; |
borlanic | 0:02dd72d1d465 | 525 | |
borlanic | 0:02dd72d1d465 | 526 | if (t2 < 0.0f) { |
borlanic | 0:02dd72d1d465 | 527 | float minVelocity = -sqrt((-2.0f*(float)(targetPosition-position)*profileAcceleration+velocity*velocity)*profileDeceleration/(profileAcceleration+profileDeceleration)); |
borlanic | 0:02dd72d1d465 | 528 | t1 = (velocity-minVelocity)/profileAcceleration; |
borlanic | 0:02dd72d1d465 | 529 | t2 = 0.0f; |
borlanic | 0:02dd72d1d465 | 530 | t3 = -minVelocity/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 531 | } |
borlanic | 0:02dd72d1d465 | 532 | |
borlanic | 0:02dd72d1d465 | 533 | if (t1 > period) { |
borlanic | 0:02dd72d1d465 | 534 | position += (double)((velocity-profileAcceleration*0.5f*period)*period); |
borlanic | 0:02dd72d1d465 | 535 | velocity += -profileAcceleration*period; |
borlanic | 0:02dd72d1d465 | 536 | } else if (t1+t2 > period) { |
borlanic | 0:02dd72d1d465 | 537 | position += (double)((velocity-profileAcceleration*0.5f*t1)*t1); |
borlanic | 0:02dd72d1d465 | 538 | velocity += -profileAcceleration*t1; |
borlanic | 0:02dd72d1d465 | 539 | position += (double)(velocity*(period-t1)); |
borlanic | 0:02dd72d1d465 | 540 | } else if (t1+t2+t3 > period) { |
borlanic | 0:02dd72d1d465 | 541 | position += (double)((velocity-profileAcceleration*0.5f*t1)*t1); |
borlanic | 0:02dd72d1d465 | 542 | velocity += -profileAcceleration*t1; |
borlanic | 0:02dd72d1d465 | 543 | position += (double)(velocity*t2); |
borlanic | 0:02dd72d1d465 | 544 | position += (double)((velocity+profileDeceleration*0.5f*(period-t1-t2))*(period-t1-t2)); |
borlanic | 0:02dd72d1d465 | 545 | velocity += profileDeceleration*(period-t1-t2); |
borlanic | 0:02dd72d1d465 | 546 | } else { |
borlanic | 0:02dd72d1d465 | 547 | position += (double)((velocity-profileAcceleration*0.5f*t1)*t1); |
borlanic | 0:02dd72d1d465 | 548 | velocity += -profileAcceleration*t1; |
borlanic | 0:02dd72d1d465 | 549 | position += (double)(velocity*t2); |
borlanic | 0:02dd72d1d465 | 550 | position += (double)((velocity+profileDeceleration*0.5f*t3)*t3); |
borlanic | 0:02dd72d1d465 | 551 | velocity += profileDeceleration*t3; |
borlanic | 0:02dd72d1d465 | 552 | } |
borlanic | 0:02dd72d1d465 | 553 | |
borlanic | 0:02dd72d1d465 | 554 | } else { // slow down to zero first, and then speed up to (negative) profile velocity |
borlanic | 0:02dd72d1d465 | 555 | |
borlanic | 0:02dd72d1d465 | 556 | float t1 = velocity/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 557 | float t2 = profileVelocity/profileAcceleration; |
borlanic | 0:02dd72d1d465 | 558 | float t4 = profileVelocity/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 559 | float t3 = (-(float)(targetPosition-position)+velocity*0.5f*t1)/profileVelocity-0.5f*(t2+t4); |
borlanic | 0:02dd72d1d465 | 560 | |
borlanic | 0:02dd72d1d465 | 561 | if (t3 < 0.0f) { |
borlanic | 0:02dd72d1d465 | 562 | float minVelocity = -sqrt((-2.0f*(float)(targetPosition-position)*profileDeceleration+velocity*velocity)*profileAcceleration/(profileAcceleration+profileDeceleration)); |
borlanic | 0:02dd72d1d465 | 563 | t2 = -minVelocity/profileAcceleration; |
borlanic | 0:02dd72d1d465 | 564 | t3 = 0.0f; |
borlanic | 0:02dd72d1d465 | 565 | t4 = -minVelocity/profileDeceleration; |
borlanic | 0:02dd72d1d465 | 566 | } |
borlanic | 0:02dd72d1d465 | 567 | |
borlanic | 0:02dd72d1d465 | 568 | if (t1 > period) { |
borlanic | 0:02dd72d1d465 | 569 | position += (double)((velocity-profileDeceleration*0.5f*period)*period); |
borlanic | 0:02dd72d1d465 | 570 | velocity += -profileDeceleration*period; |
borlanic | 0:02dd72d1d465 | 571 | } else if (t1+t2 > period) { |
borlanic | 0:02dd72d1d465 | 572 | position += (double)((velocity-profileDeceleration*0.5f*t1)*t1); |
borlanic | 0:02dd72d1d465 | 573 | velocity += -profileDeceleration*t1; |
borlanic | 0:02dd72d1d465 | 574 | position += (double)((velocity-profileAcceleration*0.5f*(period-t1))*(period-t1)); |
borlanic | 0:02dd72d1d465 | 575 | velocity += -profileAcceleration*(period-t1); |
borlanic | 0:02dd72d1d465 | 576 | } else if (t1+t2+t3 > period) { |
borlanic | 0:02dd72d1d465 | 577 | position += (double)((velocity-profileDeceleration*0.5f*t1)*t1); |
borlanic | 0:02dd72d1d465 | 578 | velocity += -profileDeceleration*t1; |
borlanic | 0:02dd72d1d465 | 579 | position += (double)((velocity-profileAcceleration*0.5f*t2)*t2); |
borlanic | 0:02dd72d1d465 | 580 | velocity += -profileAcceleration*t2; |
borlanic | 0:02dd72d1d465 | 581 | position += (double)(velocity*(period-t1-t2)); |
borlanic | 0:02dd72d1d465 | 582 | } else if (t1+t2+t3+t4 > period) { |
borlanic | 0:02dd72d1d465 | 583 | position += (double)((velocity-profileDeceleration*0.5f*t1)*t1); |
borlanic | 0:02dd72d1d465 | 584 | velocity += -profileDeceleration*t1; |
borlanic | 0:02dd72d1d465 | 585 | position += (double)((velocity-profileAcceleration*0.5f*t2)*t2); |
borlanic | 0:02dd72d1d465 | 586 | velocity += -profileAcceleration*t2; |
borlanic | 0:02dd72d1d465 | 587 | position += (double)(velocity*t3); |
borlanic | 0:02dd72d1d465 | 588 | position += (double)((velocity+profileDeceleration*0.5f*(period-t1-t2-t3))*(period-t1-t2-t3)); |
borlanic | 0:02dd72d1d465 | 589 | velocity += profileDeceleration*(period-t1-t2-t3); |
borlanic | 0:02dd72d1d465 | 590 | } else { |
borlanic | 0:02dd72d1d465 | 591 | position += (double)((velocity-profileDeceleration*0.5f*t1)*t1); |
borlanic | 0:02dd72d1d465 | 592 | velocity += -profileDeceleration*t1; |
borlanic | 0:02dd72d1d465 | 593 | position += (double)((velocity-profileAcceleration*0.5f*t2)*t2); |
borlanic | 0:02dd72d1d465 | 594 | velocity += -profileAcceleration*t2; |
borlanic | 0:02dd72d1d465 | 595 | position += (double)(velocity*t3); |
borlanic | 0:02dd72d1d465 | 596 | position += (double)((velocity+profileDeceleration*0.5f*t4)*t4); |
borlanic | 0:02dd72d1d465 | 597 | velocity += profileDeceleration*t4; |
borlanic | 0:02dd72d1d465 | 598 | } |
borlanic | 0:02dd72d1d465 | 599 | } |
borlanic | 0:02dd72d1d465 | 600 | } |
borlanic | 0:02dd72d1d465 | 601 | } |
borlanic | 0:02dd72d1d465 | 602 | |
borlanic | 0:02dd72d1d465 | 603 |