From b12e52f2b623d5087d65d474f1039b5874e6e0ef Mon Sep 17 00:00:00 2001 From: "E. A. Graham Jr" <10370165+EAGrahamJr@users.noreply.github.com> Date: Fri, 17 Nov 2023 08:40:14 -0800 Subject: [PATCH] Move stuff to show off junk Still not sure about this orrery thing. --- servomatic/build.gradle.kts | 3 + .../kotlin/crackers/kobots/app/OrreryThing.kt | 60 +++++++++++++++++++ .../kotlin/crackers/kobots/app/ServoThing.kt | 30 +++++++--- .../crackers/kobots/app/SuzerainOfServos.kt | 10 ++-- 4 files changed, 89 insertions(+), 14 deletions(-) create mode 100644 servomatic/src/main/kotlin/crackers/kobots/app/OrreryThing.kt diff --git a/servomatic/build.gradle.kts b/servomatic/build.gradle.kts index 554ec63..d28365b 100644 --- a/servomatic/build.gradle.kts +++ b/servomatic/build.gradle.kts @@ -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") } diff --git a/servomatic/src/main/kotlin/crackers/kobots/app/OrreryThing.kt b/servomatic/src/main/kotlin/crackers/kobots/app/OrreryThing.kt new file mode 100644 index 0000000..240a92d --- /dev/null +++ b/servomatic/src/main/kotlin/crackers/kobots/app/OrreryThing.kt @@ -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 + } + } + } + +} diff --git a/servomatic/src/main/kotlin/crackers/kobots/app/ServoThing.kt b/servomatic/src/main/kotlin/crackers/kobots/app/ServoThing.kt index c3f181f..00c1300 100644 --- a/servomatic/src/main/kotlin/crackers/kobots/app/ServoThing.kt +++ b/servomatic/src/main/kotlin/crackers/kobots/app/ServoThing.kt @@ -18,7 +18,9 @@ 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 @@ -26,9 +28,12 @@ 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. @@ -73,7 +78,7 @@ fun main(args: Array?) { when (payload.optString("source")) { "TheArm" -> doArmThing(payload) "Proximity" -> doAlertThing(payload) - else -> logger.info("Received $payload") + else -> logger.debug("Received $payload") } } @@ -81,15 +86,22 @@ fun main(args: Array?) { 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) } diff --git a/servomatic/src/main/kotlin/crackers/kobots/app/SuzerainOfServos.kt b/servomatic/src/main/kotlin/crackers/kobots/app/SuzerainOfServos.kt index c33a81b..74d66d4 100644 --- a/servomatic/src/main/kotlin/crackers/kobots/app/SuzerainOfServos.kt +++ b/servomatic/src/main/kotlin/crackers/kobots/app/SuzerainOfServos.kt @@ -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 @@ -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 { handleRequest(it) }) } - } val swirlyMax by lazy {