Skip to content

Commit

Permalink
Allow for correcting rotator steps/rotation
Browse files Browse the repository at this point in the history
If the rotator uses a different stepping style, this affects the rotation calcs.
  • Loading branch information
EAGrahamJr committed Jun 14, 2024
1 parent dff7b2a commit b17ce23
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
11 changes: 11 additions & 0 deletions src/main/kotlin/crackers/kobots/parts/Execution.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package crackers.kobots.parts

import com.diozero.util.SleepUtil
import java.time.Duration
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.TimeUnit
Expand Down Expand Up @@ -50,3 +51,13 @@ fun ScheduledExecutorService.scheduleWithFixedDelay(
*/
fun ScheduledExecutorService.scheduleWithDelay(delay: kotlin.time.Duration, command: () -> Unit) =
scheduleWithFixedDelay(delay, delay, command)

/**
* Extension function to use a duration for sleeping.
*/
fun Duration.sleep() = SleepUtil.busySleep(toNanos())

/**
* Extension function to use a duration for sleeping.
*/
fun kotlin.time.Duration.sleep() = SleepUtil.busySleep(inWholeNanoseconds)
18 changes: 11 additions & 7 deletions src/main/kotlin/crackers/kobots/parts/movement/Rotator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,34 +61,38 @@ interface Rotator : Actuator<RotationMovement> {
* desired rotation. Each movement is executed as a single step of the motor.
*
* **NOTE** The accuracy of the movement is dependent on rounding errors in the calculation of the number of steps
* required to reach the destination. The _timing_ of each step may also affect if the motor receives the pulse or not.
* required to reach the destination. The _timing_ of each step may also affect if the motor receives the pulse or
* not. The intent of this "device" is to be _repeatable_. Note that [stepStyle] and [stepsPerRotation] (default to
* single-stepping and the native rotation of the stepper) should be adjusted.
*
* [theStepper] _should_ be "released" after use to avoid motor burnout and to allow for "re-calibration" if necessary.
*/
open class BasicStepperRotator(
private val theStepper: BasicStepperMotor,
gearRatio: Float = 1f,
reversed: Boolean = false,
val stepStyle: StepStyle = StepStyle.SINGLE
val stepStyle: StepStyle = StepStyle.SINGLE,
stepsPerRotation: Int = theStepper.stepsPerRotation.toInt()
) : Rotator, StepperActuator {

private val degreesToSteps: Map<Int, Int>
private val stepsToDegrees = mutableMapOf<Int, MutableList<Int>>()
protected val degreesToSteps: Map<Int, Int>
protected val stepsToDegrees: Map<Int, MutableList<Int>>

init {
require(gearRatio > 0f) { "gearRatio '$gearRatio' must be greater than zero." }
val stepRatio = theStepper.stepsPerRotation * gearRatio / 360
val stepRatio = stepsPerRotation * gearRatio / 360

// calculate how many steps off of "zero" each degree is
stepsToDegrees = mutableMapOf()
degreesToSteps = (0..359).map { deg: Int ->
val steps = (deg * stepRatio).roundToInt()
stepsToDegrees.compute(steps) { _, v -> (v ?: mutableListOf()).apply { add(deg) } }
deg to steps
}.toMap()
}

private val forwardDirection = if (reversed) BACKWARD else FORWARD
private val backwardDirection = if (reversed) FORWARD else BACKWARD
protected val forwardDirection = if (reversed) BACKWARD else FORWARD
protected val backwardDirection = if (reversed) FORWARD else BACKWARD

/**
* Pass through to release the stepper when it's not directly available.
Expand Down

0 comments on commit b17ce23

Please sign in to comment.