I am trying motion profiling for the first time. I am able to run closed loop, including motion magic perfectly fine. I’m trying to write to two talons (I want a seperate profile for the left and right drives), and neither worked.
Here is the parts of the code that I feel is most important, but I’ll also give the entire method in case it helps
TrajectoryPoint outerPoint = new TrajectoryPoint();
TrajectoryPoint innerPoint = new TrajectoryPoint();
double timer = 0;
double stepSize = 10;
double outerCurrentVel = inVelOuter;
double innerCurrentVel = inVelInner;
double outerCurrentPos = outer.getSelectedSensorPosition(0);
double innerCurrentPos = inner.getSelectedSensorPosition(0);
boolean isFirst = true;
inner.clearMotionProfileTrajectories();
outer.clearMotionProfileTrajectories();
outerPoint.profileSlotSelect0 = 0;
innerPoint.profileSlotSelect0 = 0;
outerPoint.headingDeg = 0;
innerPoint.profileSlotSelect0 = 0;
outerPoint.profileSlotSelect1 = 1;
innerPoint.profileSlotSelect1 = 1;
ProfileNotifier pNotifier = new ProfileNotifier(10, innerDist, outerDist, inner, outer);
while (timer < time) {
timer += .1;
if (timer < firstAccelerationTime) {
outerCurrentVel += firstAccelerationOuter * stepSize;
outerCurrentPos += outerCurrentVel * stepSize;
innerCurrentVel += firstAccelerationInner * stepSize;
innerCurrentPos += outerCurrentVel * stepSize;
outerPoint.isLastPoint = false;
outerPoint.zeroPos = false;
outerPoint.velocity = outerCurrentVel;
outerPoint.position = outerCurrentPos;
outerPoint.timeDur = TrajectoryDuration.Trajectory_Duration_10ms;
innerPoint.isLastPoint = false;
outerPoint.zeroPos = false;
innerPoint.velocity = innerCurrentVel;
innerPoint.position = innerCurrentPos;
innerPoint.timeDur = TrajectoryDuration.Trajectory_Duration_10ms;
if (isFirst) {
outerPoint.zeroPos = true;
innerPoint.zeroPos = true;
isFirst = false;
}
pNotifier.addPoint();
} //Other conditions removed for simplicity
inner.pushMotionProfileTrajectory(innerPoint);
outer.pushMotionProfileTrajectory(outerPoint);
}
Here my entire code:
public static ProfileNotifier generateTurnMotionControl(double cruzV, double outV, double turnR, double turnAngle,
boolean right, TalonSRX leftT2, TalonSRX rightT2) {
System.out.println("Generating profile");
if (Config.robotWidth == 0) {
throw new RuntimeException("measure the robot width");
}
TalonSRX outer = (right ? leftT2 : rightT2);
TalonSRX inner = (right ? rightT2 : leftT2);
double angle = Math.toRadians(turnAngle);
DriveTrain drive = (DriveTrain) Robot.getSubsystem(SubsystemNames.DRIVE_TRAIN);
double outerDist = drive.inchesToCycles(turnR + Config.robotWidth) * angle;
double innerDist = drive.inchesToCycles(turnR) * angle;
double inVelOuter = outer.getSelectedSensorVelocity(0);
double inVelInner = inner.getSelectedSensorPosition(0);
double v1 = (inVelOuter + cruzV) / 8d;
double v2 = cruzV / 2d;
double v3 = (cruzV + outV) / 8d;
double time = outerDist / (v1 + v2 + v3) * 100; // cycles / (cycles / 100ms) -> 100ms
double firstAccelerationTime = time / 4d;
double cruzTime = time / 2d;
double secondAccelerationTime = time / 4d;
double innerCruzVel = 4 * ((innerDist / time) - (inVelInner / 8d) - (outV / 8d)) / 3d;
double firstAccelerationOuter = (cruzV - inVelOuter) / firstAccelerationTime;
double secondAccelerationOuter = (outV - cruzV) / secondAccelerationTime;
double firstAccelerationInner = (innerCruzVel - inVelInner) / firstAccelerationTime;
double secondAccelerationInner = (outV - innerCruzVel) / secondAccelerationTime;
TrajectoryPoint outerPoint = new TrajectoryPoint();
TrajectoryPoint innerPoint = new TrajectoryPoint();
double timer = 0;
double stepSize = 10;
double outerCurrentVel = inVelOuter;
double innerCurrentVel = inVelInner;
double outerCurrentPos = outer.getSelectedSensorPosition(0);
double innerCurrentPos = inner.getSelectedSensorPosition(0);
boolean isFirst = true;
inner.clearMotionProfileTrajectories();
outer.clearMotionProfileTrajectories();
outerPoint.profileSlotSelect0 = 0;
innerPoint.profileSlotSelect0 = 0;
outerPoint.headingDeg = 0;
innerPoint.profileSlotSelect0 = 0;
outerPoint.profileSlotSelect1 = 1;
innerPoint.profileSlotSelect1 = 1;
ProfileNotifier pNotifier = new ProfileNotifier(10, innerDist, outerDist, inner, outer);
//leftT2.configMotionProfileTrajectoryPeriod(0, 10);
//rightT2.configMotionProfileTrajectoryPeriod(0, 10);
System.out.println("first acc time: " + firstAccelerationTime);
System.out.println("first acc time: " + cruzTime);
System.out.println("first acc time: " + secondAccelerationTime);
while (timer < time) {
System.out.println("happy face one");
timer += .1;
if (timer < firstAccelerationTime) {
System.out.println("happy face two");
outerCurrentVel += firstAccelerationOuter * stepSize;
outerCurrentPos += outerCurrentVel * stepSize;
innerCurrentVel += firstAccelerationInner * stepSize;
innerCurrentPos += outerCurrentVel * stepSize;
outerPoint.isLastPoint = false;
outerPoint.zeroPos = false;
outerPoint.velocity = outerCurrentVel;
outerPoint.position = outerCurrentPos;
outerPoint.timeDur = TrajectoryDuration.Trajectory_Duration_10ms;
innerPoint.isLastPoint = false;
outerPoint.zeroPos = false;
innerPoint.velocity = innerCurrentVel;
innerPoint.position = innerCurrentPos;
innerPoint.timeDur = TrajectoryDuration.Trajectory_Duration_10ms;
if (isFirst) {
outerPoint.zeroPos = true;
innerPoint.zeroPos = true;
isFirst = false;
}
pNotifier.addPoint();
} else if (timer < firstAccelerationTime + cruzTime) {
outerCurrentVel += cruzV;
outerCurrentPos += outerCurrentVel * stepSize;
innerCurrentVel += innerCruzVel;
innerCurrentPos += outerCurrentVel * stepSize;
outerPoint.isLastPoint = false;
outerPoint.zeroPos = false;
outerPoint.velocity = outerCurrentVel;
outerPoint.position = outerCurrentPos;
outerPoint.timeDur = TrajectoryDuration.Trajectory_Duration_10ms;
innerPoint.isLastPoint = false;
outerPoint.zeroPos = false;
innerPoint.velocity = innerCurrentVel;
innerPoint.position = innerCurrentPos;
innerPoint.timeDur = TrajectoryDuration.Trajectory_Duration_10ms;
pNotifier.addPoint();
} else if (timer < firstAccelerationTime + cruzTime + secondAccelerationTime) {
outerCurrentVel += secondAccelerationOuter * stepSize;
outerCurrentPos += outerCurrentVel * stepSize;
innerCurrentVel += secondAccelerationInner * stepSize;
innerCurrentPos += outerCurrentVel * stepSize;
outerPoint.isLastPoint = false;
outerPoint.zeroPos = false;
outerPoint.velocity = outerCurrentVel;
outerPoint.position = outerCurrentPos;
outerPoint.timeDur = TrajectoryDuration.Trajectory_Duration_10ms;
innerPoint.isLastPoint = false;
outerPoint.zeroPos = false;
innerPoint.velocity = innerCurrentVel;
innerPoint.position = innerCurrentPos;
innerPoint.timeDur = TrajectoryDuration.Trajectory_Duration_10ms;
pNotifier.addPoint();
} else if (timer >= time) {
innerPoint.isLastPoint = true;
innerPoint.isLastPoint = true;
pNotifier.addPoint();
}
System.out.println("generating " + (inner == null) + " " + (innerPoint==null));
printPoint(innerPoint);
inner.pushMotionProfileTrajectory(innerPoint);
outer.pushMotionProfileTrajectory(outerPoint);
}
System.out.println("done generating profile");
return pNotifier;
}