Slight improvements

This commit is contained in:
Sebastiaan de Schaetzen 2024-07-23 15:37:55 +02:00
parent 17029a0a14
commit c0023195c5
3 changed files with 41 additions and 19 deletions

View File

@ -15,6 +15,12 @@ BotState = {
servoBattery = nil, servoBattery = nil,
servoBatteryCorrected = nil, servoBatteryCorrected = nil,
camfeed = nil, camfeed = nil,
viewX = 0,
viewY = 0,
viewXSent = 0,
viewYSent = 0,
viewLastUpdate = 0,
} }
Ping = { Ping = {
@ -24,10 +30,8 @@ Ping = {
} }
ControllerState = { ControllerState = {
viewX = 0, rightX = 0,
viewY = 0, rightY = 0
viewChanged = false,
viewLastUpdated = 0
} }
function love.update2() function love.update2()
@ -41,26 +45,40 @@ function love.update2()
love.mqtt.send("command/ping", Ping.payload) love.mqtt.send("command/ping", Ping.payload)
print("Sending ping") print("Sending ping")
end end
if ControllerState.viewChanged and (now - ControllerState.viewLastUpdated) >= 0.05 then
BotState.viewX = BotState.viewX + ControllerState.rightX * 0.02
BotState.viewY = BotState.viewY + ControllerState.rightY * 0.02
local viewDX, viewDY = math.abs(BotState.viewX - BotState.viewXSent), math.abs(BotState.viewY - BotState.viewYSent)
if viewDX > 0.01 or viewDY > 0.01 and (now - BotState.viewLastUpdated) >= 0.05 then
love.mqtt.send("command/set_camera_xy", toJSON({ love.mqtt.send("command/set_camera_xy", toJSON({
x = -ControllerState.viewX * 0.3 + 0.5, x = -BotState.viewX * 0.3 + 0.5,
y = -ControllerState.viewY * 0.3 + 0.5 y = -BotState.viewY * 0.3 + 0.5
})) }))
ControllerState.viewChanged = false BotState.viewXSent = BotState.viewX
ControllerState.viewLastUpdated = now BotState.viewYSent = BotState.viewY
BotState.viewLastUpdated = now
end end
end end
function love.joystickaxis2(joystick, axis, value) function love.joystickaxis2(joystick, axis, value)
if axis == 3 and value ~= ControllerState.viewX then if axis == 3 then
ControllerState.viewX = value ControllerState.rightX = value
ControllerState.viewChanged = true elseif axis == 4 then
elseif axis == 4 and value ~= ControllerState.viewY then ControllerState.rightY = value
ControllerState.viewY = value
ControllerState.viewChanged = true
end end
end end
-- function love.joystickaxis2(joystick, axis, value)
-- if axis == 3 and value ~= ControllerState.viewX then
-- ControllerState.viewX = value
-- ControllerState.viewChanged = true
-- elseif axis == 4 and value ~= ControllerState.viewY then
-- ControllerState.viewY = value
-- ControllerState.viewChanged = true
-- end
-- end
function formatSafe(format, value, ...) function formatSafe(format, value, ...)
if value == nil then if value == nil then
return "unknown" return "unknown"

View File

@ -75,11 +75,18 @@ func subscribe(client mqtt.Client, topic string, handler mqtt.MessageHandler) {
} }
} }
func onConnect(client mqtt.Client) {
log.Print("Subscribing to command topics")
subscribe(client, "spider/command/ping", onPing)
subscribe(client, "spider/command/set_camera_xy", onSetCameraXY)
}
func main() { func main() {
opts := mqtt.NewClientOptions() opts := mqtt.NewClientOptions()
opts.AddBroker(broker) opts.AddBroker(broker)
opts.SetClientID("spider-host-client") opts.SetClientID("spider-host-client")
opts.SetResumeSubs(true) opts.SetResumeSubs(true)
opts.SetOnConnectHandler(onConnect)
client := mqtt.NewClient(opts) client := mqtt.NewClient(opts)
token := client.Connect() token := client.Connect()
@ -88,10 +95,6 @@ func main() {
log.Fatalf("Error connecting to MQTT broker: %v\n", token.Error()) log.Fatalf("Error connecting to MQTT broker: %v\n", token.Error())
} }
log.Print("Subscribing to command topics")
subscribe(client, "spider/command/ping", onPing)
subscribe(client, "spider/command/set_camera_xy", onSetCameraXY)
InitBattery() InitBattery()
InitServo() InitServo()

View File

@ -52,6 +52,7 @@ func InitServo() {
} }
// Halt the controllers to stop any current movement // Halt the controllers to stop any current movement
ServosOff() ServosOff()
log.Print("Started servos")
} }
func ServosOff() { func ServosOff() {