Skip to content
This repository has been archived by the owner on Dec 24, 2018. It is now read-only.

Commit

Permalink
Use notifier class for path following
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewda committed Aug 26, 2018
1 parent d483cda commit 4e2ce92
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/main/java/org/sert2521/powerup/autonomous/Autonomous.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.sert2521.powerup.autonomous

import edu.wpi.first.wpilibj.Notifier
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard
import jaci.pathfinder.Pathfinder
import jaci.pathfinder.followers.EncoderFollower
Expand Down Expand Up @@ -136,6 +137,7 @@ object Auto : RobotLifecycle {

private abstract class PathFollowerBase(protected val path: PathBase) : Command() {
private val pathIndex get() = pathIndexField.get(path.left) as Int
private val notifier = Notifier { followPath() }

init {
requires(Drivetrain)
Expand All @@ -153,9 +155,18 @@ private abstract class PathFollowerBase(protected val path: PathBase) : Command(
right.configureEncoder(0, ENCODER_TICKS_PER_REVOLUTION, WHEEL_DIAMETER)
right.configurePIDVA(1.0, 0.0, 0.05, 1 / MAX_VELOCITY, 0.0)
}

notifier.startPeriodic(PERIOD)
}

override fun execute() = path.isFinished

override fun onDestroy() {
pathProgress = null
notifier.stop()
}

override fun execute(): Boolean {
private fun followPath() {
val leftPosition = Drivetrain.leftPosition
val rightPosition = Drivetrain.rightPosition

Expand All @@ -166,12 +177,6 @@ private abstract class PathFollowerBase(protected val path: PathBase) : Command(
calculate(leftPosition, rightPosition, turn).apply { drive(first, second) }

pathProgress = pathIndex.toDouble() / path.trajectory.segments.size.toDouble()

return path.isFinished
}

override fun onDestroy() {
pathProgress = null
}

protected open fun calculate(leftPosition: Int, rightPosition: Int, turn: Double) =
Expand All @@ -183,6 +188,7 @@ private abstract class PathFollowerBase(protected val path: PathBase) : Command(

private companion object {
const val TURN_IMPORTANCE = 0.0005
const val PERIOD = 0.01 // 100Hz

val pathIndexField: Field = EncoderFollower::class.java.getDeclaredField("segment").apply {
isAccessible = true
Expand Down

0 comments on commit 4e2ce92

Please sign in to comment.