Skip to content

Commit

Permalink
Move stuff to show off junk
Browse files Browse the repository at this point in the history
Still not sure about this orrery thing.
  • Loading branch information
EAGrahamJr committed Nov 17, 2023
1 parent e92c4f2 commit b12e52f
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 14 deletions.
3 changes: 3 additions & 0 deletions servomatic/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ plugins {

dependencies {
implementation("com.typesafe:config:1.4.1")
implementation("crackers.automation:hassk:0.0.1") {
exclude(group = "ch.qos.logback")
}
// implementation("com.diozero:diozero-provider-pigpio:$DIOZERO_VER")
// implementation("com.diozero:diozero-provider-remote:$DIOZERO_VER")
}
Expand Down
60 changes: 60 additions & 0 deletions servomatic/src/main/kotlin/crackers/kobots/app/OrreryThing.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2022-2023 by E. A. Graham, Jr.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package crackers.kobots.app

import crackers.kobots.parts.movement.ActionSpeed
import java.util.concurrent.atomic.AtomicReference
import kotlin.math.roundToInt

/**
* TODO fill this in
*/
object OrreryThing {
/**
* Show the sun's elevation on the orrery: 180 on the rotator is 0 degrees elevation and 0 is 180 degrees elevation
*/
private val elevation = AtomicReference(0f)
internal var sunElevation: Float
get() = elevation.get()
set(value) {
// in this case, + or - indicates rising or setting
val next = when {
value > 0 -> {
90 + (3 * (25 + value))
}

value < 0 -> {
90 - (3 * (25 - value))
}

else -> 0f
}
elevation.set(next.coerceIn(0f, 180f))
servoRequest(orrerySun)
}

val orrerySun by lazy {
crackers.kobots.parts.movement.sequence {
name = "Orrery Sun"
action {
SuzerainOfServos.orreryRotor rotate sunElevation.roundToInt()
requestedSpeed = ActionSpeed.SLOW
}
}
}

}
30 changes: 21 additions & 9 deletions servomatic/src/main/kotlin/crackers/kobots/app/ServoThing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,22 @@ package crackers.kobots.app

import com.diozero.devices.ServoController
import crackers.kobots.app.AppCommon.REMOTE_PI
import crackers.kobots.app.AppCommon.hasskClient
import crackers.kobots.app.AppCommon.mqttClient
import crackers.kobots.app.AppCommon.whileRunning
import crackers.kobots.app.SuzerainOfServos.INTERNAL_TOPIC
import crackers.kobots.mqtt.KobotsMQTT.Companion.KOBOTS_EVENTS
import crackers.kobots.parts.app.KobotSleep
import crackers.kobots.parts.app.publishToTopic
import crackers.kobots.parts.enumValue
import crackers.kobots.parts.movement.ActionSequence
import crackers.kobots.parts.movement.SequenceRequest
import crackers.kobots.parts.scheduleAtFixedRate
import org.json.JSONObject
import org.slf4j.LoggerFactory
import kotlin.system.exitProcess
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds

/**
* Handles a bunch of different servos for various things. Everything should have an MQTT interface.
Expand Down Expand Up @@ -73,23 +78,30 @@ fun main(args: Array<String>?) {
when (payload.optString("source")) {
"TheArm" -> doArmThing(payload)
"Proximity" -> doAlertThing(payload)
else -> logger.info("Received $payload")
else -> logger.debug("Received $payload")
}
}

startAliveCheck()
allowEmergencyStop()
}

// TODO the Sparkfun I2c port on the servo hat is not working
// PadPrincipal.start()
hat.use { hat ->
// logger.info("luminosity at start ${proxy.luminosity}")
AppCommon.awaitTermination()

KobotSleep.seconds(1)
AppCommon.executor.scheduleAtFixedRate(30.seconds, 5.minutes) {
whileRunning {
with(hasskClient) {
val rising = sensor("sun_solar_rising").state().state.toBoolean()
val elevation = sensor("sun_solar_elevation").state().state.toFloat().let {
if (it < 0) 0f else if (rising) it else -it
}
OrreryThing.sunElevation = elevation
}
}
}
// PadPrincipal.stop()

// TODO the Sparkfun I2c port on the servo hat is not working
AppCommon.awaitTermination()
KobotSleep.seconds(1)
hat.close()
logger.warn("Servomatic exit")
exitProcess(0)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ import crackers.kobots.devices.MG90S_TRIM
import crackers.kobots.parts.app.KobotsAction
import crackers.kobots.parts.app.KobotsSubscriber
import crackers.kobots.parts.app.joinTopic
import crackers.kobots.parts.movement.ActionSpeed
import crackers.kobots.parts.movement.SequenceExecutor
import crackers.kobots.parts.movement.ServoRotator
import crackers.kobots.parts.movement.sequence
import crackers.kobots.parts.movement.*

/**
* All the things
Expand All @@ -45,10 +42,13 @@ object SuzerainOfServos : SequenceExecutor("Suzie", AppCommon.mqttClient) {
ServoRotator(wavyServo, 0..90, 0..150)
}

val orreryRotor by lazy {
SimpleServoRotator(hat.getServo(2, MG90S_TRIM, 180), 0..180)
}

init {
joinTopic(INTERNAL_TOPIC, KobotsSubscriber<KobotsAction> { handleRequest(it) })
}

}

val swirlyMax by lazy {
Expand Down

0 comments on commit b12e52f

Please sign in to comment.