|
1 | 1 | package us.ihmc.exampleSimulations.yoFilteredDouble.emptyRobotSCS; |
2 | 2 |
|
| 3 | +import us.ihmc.exampleSimulations.yoFilteredDouble.emptyRobotSCS.YoFilteredDoubleController.TrajectoryGenerator.ChirpType; |
| 4 | +import us.ihmc.exampleSimulations.yoFilteredDouble.emptyRobotSCS.YoFilteredDoubleController.TrajectoryGenerator.Trajectory; |
3 | 5 | import us.ihmc.robotics.math.filters.ContinuousTransferFunction; |
4 | 6 | import us.ihmc.robotics.math.filters.TransferFunctionDiscretizer; |
5 | 7 | import us.ihmc.robotics.math.filters.YoFilteredDouble; |
6 | | -import us.ihmc.robotics.math.trajectories.generators.TrajectoryGenerator; |
7 | | -import us.ihmc.robotics.math.trajectories.generators.TrajectoryGenerator.ChirpType; |
8 | | -import us.ihmc.robotics.math.trajectories.generators.TrajectoryGenerator.Trajectory; |
9 | 8 | import us.ihmc.simulationconstructionset.util.RobotController; |
10 | 9 | import us.ihmc.yoVariables.registry.YoRegistry; |
11 | 10 | import us.ihmc.yoVariables.variable.YoDouble; |
12 | 11 |
|
| 12 | +import java.util.ArrayList; |
| 13 | + |
13 | 14 | public class YoFilteredDoubleController implements RobotController |
14 | 15 | { |
15 | 16 | private YoRegistry registry; |
@@ -195,4 +196,235 @@ public void doControl() |
195 | 196 | RefTraj2_nonjump_Filtered_Var.set(sinusoid_input.getDoubleValue()); |
196 | 197 | chirpFreqHz.set(chirp.getFreqCheckRad() / (2 * Math.PI)); |
197 | 198 | } |
| 199 | + |
| 200 | + /** |
| 201 | + * Class is designed for quickly generating several different trajectory types. A majority of the classes are self-explanatory. |
| 202 | + * The Triangle Waveform comes from https://en.wikipedia.org/wiki/Triangle_wave, denoting an alternative equation because the mod |
| 203 | + * operator is not the remainder operator. |
| 204 | + * The Multi-Square Waveform will generate a series of square waves in order repeating forever. It can be initialized using: |
| 205 | + * DesiredTrajectory = new TrajectoryGenerator("OpenLoop", registry, Trajectory.MULTI_SQUARE, 2000.0, 0.2, new ArrayList<Double>(Arrays.asList(1000.0, 0.0, |
| 206 | + * 2000.0, 0.0, 3000.0, 0.0))); |
| 207 | + * The Chirp signal is a sinusoid with a consistent amplitude, but is increasing in frequency. It is commonly used for System ID, |
| 208 | + * and was used for identifying the transfer function of the actuator. The equation was given by Dr. Southward from VT in the ME Dept. |
| 209 | + * There are two options for chirp signals: linear or exponential. This refers to the rate at which the frequency is increasing and |
| 210 | + * this choice depends on which frequencies you care about. |
| 211 | + * |
| 212 | + * @author Connor Herron |
| 213 | + */ |
| 214 | + |
| 215 | + public static class TrajectoryGenerator |
| 216 | + { |
| 217 | + private String name; |
| 218 | + private double totalTime; |
| 219 | + private double minFreqRad; |
| 220 | + private double maxFreqRad; |
| 221 | + private double prevCos; |
| 222 | + private double prevSin; |
| 223 | + private double chirpSlope; |
| 224 | + private double chirpAmplitude; |
| 225 | + private double curFreqRad; |
| 226 | + private double expoCoeff; |
| 227 | + private YoDouble freqCheckRad; |
| 228 | + private YoRegistry registry; |
| 229 | + private ArrayList<Double> multiSquareList = new ArrayList<>(); |
| 230 | + |
| 231 | + private double waveAmp; |
| 232 | + private double waveFreq; |
| 233 | + private double rampSlope; |
| 234 | + private double phaseOffset; |
| 235 | + private double amplitudeOffset; |
| 236 | + private double wavePeriod; |
| 237 | + |
| 238 | + private int initFlag = 0; |
| 239 | + |
| 240 | + public enum Trajectory |
| 241 | + { |
| 242 | + STEP, RAMP, SINE, TRIANGLE, SAWTOOTH, SQUARE, MULTI_SQUARE, CHIRP |
| 243 | + } |
| 244 | + |
| 245 | + public enum ChirpType |
| 246 | + { |
| 247 | + LINEAR, EXPONENTIAL |
| 248 | + } |
| 249 | + |
| 250 | + private Trajectory myTrajectory; |
| 251 | + private ChirpType myChirpType; |
| 252 | + |
| 253 | + public TrajectoryGenerator(String name, YoRegistry registry, Trajectory desiredWaveform, double waveAmp) |
| 254 | + { |
| 255 | + this(name, registry, desiredWaveform, waveAmp, 0.0); |
| 256 | + } |
| 257 | + |
| 258 | + public TrajectoryGenerator(String name, YoRegistry registry, Trajectory desiredWaveform, double waveAmp, double waveFreq) |
| 259 | + { |
| 260 | + this(name, registry, desiredWaveform, waveAmp, waveFreq, 0.0); |
| 261 | + } |
| 262 | + |
| 263 | + public TrajectoryGenerator(String name, YoRegistry registry, Trajectory desiredWaveform, double waveAmp, double waveFreq, double rampSlope) |
| 264 | + { |
| 265 | + this(name, registry, desiredWaveform, waveAmp, waveFreq, rampSlope, 0.0, 0.0); |
| 266 | + } |
| 267 | + |
| 268 | + public TrajectoryGenerator(String name, |
| 269 | + YoRegistry registry, |
| 270 | + Trajectory desiredWaveform, |
| 271 | + double waveAmp, |
| 272 | + double waveFreq, |
| 273 | + double rampSlope, |
| 274 | + double phaseOffset, |
| 275 | + double amplitudeOffset) |
| 276 | + { |
| 277 | + this.name = name; |
| 278 | + this.registry = registry; |
| 279 | + this.myTrajectory = desiredWaveform; |
| 280 | + this.waveAmp = waveAmp; |
| 281 | + this.waveFreq = waveFreq; |
| 282 | + this.wavePeriod = 1 / waveFreq; |
| 283 | + this.rampSlope = rampSlope; |
| 284 | + this.phaseOffset = phaseOffset; |
| 285 | + this.amplitudeOffset = amplitudeOffset; |
| 286 | + } |
| 287 | + |
| 288 | + public TrajectoryGenerator(String name, YoRegistry registry, Trajectory desiredWaveform, double waveAmp, double waveFreq, ArrayList<Double> multiSquare) |
| 289 | + { |
| 290 | + this.name = name; |
| 291 | + this.registry = registry; |
| 292 | + this.myTrajectory = desiredWaveform; |
| 293 | + this.waveAmp = waveAmp; |
| 294 | + this.waveFreq = waveFreq; |
| 295 | + this.wavePeriod = wavePeriod; |
| 296 | + this.multiSquareList = multiSquare; |
| 297 | + } |
| 298 | + |
| 299 | + // For running chirp signals |
| 300 | + public TrajectoryGenerator(String name, |
| 301 | + YoRegistry registry, |
| 302 | + Trajectory desiredWaveform, |
| 303 | + ChirpType desiredChirpType, |
| 304 | + double totalTime, |
| 305 | + double amplitude, |
| 306 | + double minFreqHz, |
| 307 | + double maxFreqHz) |
| 308 | + { |
| 309 | + this.name = name; |
| 310 | + this.registry = registry; |
| 311 | + this.myTrajectory = desiredWaveform; |
| 312 | + this.myChirpType = desiredChirpType; |
| 313 | + |
| 314 | + freqCheckRad = new YoDouble(name + "freqCheckRad", registry); |
| 315 | + |
| 316 | + initializeChirpSignal(totalTime, amplitude, minFreqHz, maxFreqHz); |
| 317 | + } |
| 318 | + |
| 319 | + public void initializeChirpSignal(double totalTime, double amplitude, double minFreqHz, double maxFreqHz) |
| 320 | + { |
| 321 | + this.totalTime = totalTime; |
| 322 | + this.minFreqRad = 2 * Math.PI * minFreqHz; |
| 323 | + this.maxFreqRad = 2 * Math.PI * maxFreqHz; |
| 324 | + this.chirpAmplitude = amplitude; |
| 325 | + |
| 326 | + // Calculate the slope of the chirp signal. |
| 327 | + this.chirpSlope = (this.maxFreqRad - this.minFreqRad) / (totalTime); |
| 328 | + |
| 329 | + // Initialize coefficient for the exponential function |
| 330 | + this.expoCoeff = Math.log10(maxFreqRad / minFreqRad) / totalTime; |
| 331 | + |
| 332 | + // initialize prevCos and prevSin |
| 333 | + this.prevCos = 1.0; |
| 334 | + this.prevSin = 0.0; |
| 335 | + } |
| 336 | + |
| 337 | + private double alpha; |
| 338 | + private double beta; |
| 339 | + private double cosNew; |
| 340 | + private double sinNew; |
| 341 | + |
| 342 | + /** |
| 343 | + * Method calculates the open loop current command (mA) to send for a linear sinusoidal chirp signal. This method is initialized |
| 344 | + * with the initializeOLChirpSignal method above. |
| 345 | + * |
| 346 | + * @param t_sec real time seconds. |
| 347 | + * @param dT real time control dT |
| 348 | + * @return commanded motor current (mA) |
| 349 | + */ |
| 350 | + public double runChirpSignal(double t_sec, double dT) |
| 351 | + { |
| 352 | + // Calculate current Freq (Rad) |
| 353 | + switch (myChirpType) |
| 354 | + { |
| 355 | + case LINEAR: |
| 356 | + this.curFreqRad = (this.chirpSlope * t_sec + this.minFreqRad); |
| 357 | + break; |
| 358 | + case EXPONENTIAL: |
| 359 | + this.curFreqRad = this.minFreqRad * Math.pow(10, this.expoCoeff * t_sec); |
| 360 | + break; |
| 361 | + } |
| 362 | + |
| 363 | + // Calculate new sinusoid value using double angle identity formula. |
| 364 | + alpha = Math.cos(this.curFreqRad * dT); |
| 365 | + beta = Math.sin(this.curFreqRad * dT); |
| 366 | + |
| 367 | + cosNew = alpha * this.prevCos - beta * this.prevSin; |
| 368 | + sinNew = beta * this.prevCos + alpha * this.prevSin; |
| 369 | + |
| 370 | + this.prevCos = cosNew; |
| 371 | + this.prevSin = sinNew; |
| 372 | + |
| 373 | + if (t_sec > this.totalTime) |
| 374 | + { |
| 375 | + freqCheckRad.set(0.0); |
| 376 | + return 0.0; |
| 377 | + } |
| 378 | + else |
| 379 | + { |
| 380 | + freqCheckRad.set(this.curFreqRad); |
| 381 | + return this.chirpAmplitude * sinNew; |
| 382 | + } |
| 383 | + } |
| 384 | + |
| 385 | + // Run method during loop. |
| 386 | + public double updateTrajectory(double curSecTime, double dT) |
| 387 | + { |
| 388 | + switch (myTrajectory) |
| 389 | + { |
| 390 | + case STEP: |
| 391 | + return waveAmp; |
| 392 | + case RAMP: |
| 393 | + return rampSlope * curSecTime; |
| 394 | + case SINE: |
| 395 | + return waveAmp * Math.sin(2 * Math.PI * waveFreq * curSecTime + phaseOffset) + amplitudeOffset; |
| 396 | + case TRIANGLE: |
| 397 | + return (4 * waveAmp / wavePeriod) * Math.abs((((curSecTime - (wavePeriod / 4)) % wavePeriod) + wavePeriod) % wavePeriod - wavePeriod / 2) |
| 398 | + - waveAmp; |
| 399 | + case SAWTOOTH: |
| 400 | + return waveAmp * waveFreq * Math.abs(curSecTime % (1 / waveFreq)); |
| 401 | + case SQUARE: |
| 402 | + return waveAmp * Math.signum(Math.sin(2 * Math.PI * waveFreq * curSecTime + phaseOffset)) + amplitudeOffset; |
| 403 | + case MULTI_SQUARE: |
| 404 | + return waveAmp = multiSquareList.get(Math.floorMod((int) (curSecTime * waveFreq), multiSquareList.size())); |
| 405 | + case CHIRP: |
| 406 | + return runChirpSignal(curSecTime, dT); |
| 407 | + default: |
| 408 | + return 0.0; |
| 409 | + } |
| 410 | + } |
| 411 | + |
| 412 | + // Wave Frequency is defined as [rad/s] |
| 413 | + public double updateTrajectoryDot(Trajectory Desired_Trajectory, double curSecTime, double waveAmp, double waveFreq, double phaseOffset) |
| 414 | + { |
| 415 | + if (Desired_Trajectory == Trajectory.SINE) |
| 416 | + { |
| 417 | + return 2 * Math.PI * waveFreq * waveAmp * Math.cos(2 * Math.PI * waveFreq * curSecTime + phaseOffset); |
| 418 | + } |
| 419 | + else |
| 420 | + { |
| 421 | + return 0; |
| 422 | + } |
| 423 | + } |
| 424 | + |
| 425 | + public double getFreqCheckRad() |
| 426 | + { |
| 427 | + return freqCheckRad.getDoubleValue(); |
| 428 | + } |
| 429 | + } |
198 | 430 | } |
0 commit comments