An attempt at getting image data back
This commit is contained in:
		@@ -1,7 +1,10 @@
 | 
			
		||||
FROM golang:1.22.5
 | 
			
		||||
#FROM golang:1.22.5
 | 
			
		||||
FROM alpine:3.20.1
 | 
			
		||||
 | 
			
		||||
WORKDIR /app
 | 
			
		||||
 | 
			
		||||
#RUN apk update && apk add --no-cache libcamera-raspberrypi
 | 
			
		||||
 | 
			
		||||
COPY spider-host /spider-host
 | 
			
		||||
 | 
			
		||||
CMD ["/spider-host"]
 | 
			
		||||
 
 | 
			
		||||
@@ -4,10 +4,12 @@ go 1.22
 | 
			
		||||
 | 
			
		||||
require (
 | 
			
		||||
	github.com/eclipse/paho.mqtt.golang v1.4.3
 | 
			
		||||
	github.com/vladimirvivien/go4vl v0.0.5
 | 
			
		||||
	gobot.io/x/gobot v1.16.0
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
require (
 | 
			
		||||
	github.com/blackjack/webcam v0.6.1 // indirect
 | 
			
		||||
	github.com/gofrs/uuid v4.0.0+incompatible // indirect
 | 
			
		||||
	github.com/gorilla/websocket v1.5.0 // indirect
 | 
			
		||||
	github.com/hashicorp/errwrap v1.0.0 // indirect
 | 
			
		||||
@@ -17,5 +19,6 @@ require (
 | 
			
		||||
	github.com/sigurn/utils v0.0.0-20190728110027-e1fefb11a144 // indirect
 | 
			
		||||
	golang.org/x/net v0.8.0 // indirect
 | 
			
		||||
	golang.org/x/sync v0.1.0 // indirect
 | 
			
		||||
	golang.org/x/sys v0.14.0 // indirect
 | 
			
		||||
	periph.io/x/periph v3.6.2+incompatible // indirect
 | 
			
		||||
)
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
 | 
			
		||||
github.com/JuulLabs-OSS/cbgo v0.0.2/go.mod h1:L4YtGP+gnyD84w7+jN66ncspFRfOYB5aj9QSXaFHmBA=
 | 
			
		||||
github.com/blackjack/webcam v0.6.1 h1:K0T6Q0zto23U99gNAa5q/hFoye6uGcKr2aE6hFoxVoE=
 | 
			
		||||
github.com/blackjack/webcam v0.6.1/go.mod h1:zs+RkUZzqpFPHPiwBZ6U5B34ZXXe9i+SiHLKnnukJuI=
 | 
			
		||||
github.com/bmizerany/pat v0.0.0-20170815010413-6226ea591a40/go.mod h1:8rLXio+WjiTceGBHIoTvn60HIbs7Hm7bcHjyrSqYB9c=
 | 
			
		||||
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
 | 
			
		||||
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
 | 
			
		||||
@@ -66,6 +68,8 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
 | 
			
		||||
github.com/suapapa/go_eddystone v1.3.1/go.mod h1:bXC11TfJOS+3g3q/Uzd7FKd5g62STQEfeEIhcKe4Qy8=
 | 
			
		||||
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
 | 
			
		||||
github.com/veandco/go-sdl2 v0.3.3/go.mod h1:FB+kTpX9YTE+urhYiClnRzpOXbiWgaU3+5F2AB78DPg=
 | 
			
		||||
github.com/vladimirvivien/go4vl v0.0.5 h1:jHuo/CZOAzYGzrSMOc7anOMNDr03uWH5c1B5kQ+Chnc=
 | 
			
		||||
github.com/vladimirvivien/go4vl v0.0.5/go.mod h1:FP+/fG/X1DUdbZl9uN+l33vId1QneVn+W80JMc17OL8=
 | 
			
		||||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
 | 
			
		||||
go.bug.st/serial v1.1.1/go.mod h1:VmYBeyJWp5BnJ0tw2NUJHZdJTGl2ecBGABHlzRK1knY=
 | 
			
		||||
gobot.io/x/gobot v1.16.0 h1:MQN0c5iPYBkChpPPY/zM6Au0rihJZ4QmK98kn1DKBKQ=
 | 
			
		||||
@@ -98,6 +102,9 @@ golang.org/x/sys v0.0.0-20200728102440-3e129f6d46b1/go.mod h1:h1NjWce9XRLGQEsW7w
 | 
			
		||||
golang.org/x/sys v0.0.0-20200826173525-f9321e4c35a6/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 | 
			
		||||
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 | 
			
		||||
golang.org/x/sys v0.0.0-20201126233918-771906719818/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 | 
			
		||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 | 
			
		||||
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
 | 
			
		||||
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
 | 
			
		||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 | 
			
		||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
 | 
			
		||||
golang.org/x/tools v0.0.0-20200925191224-5d1fdd8fa346/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU=
 | 
			
		||||
 
 | 
			
		||||
@@ -2,6 +2,7 @@ package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"github.com/blackjack/webcam"
 | 
			
		||||
	mqtt "github.com/eclipse/paho.mqtt.golang"
 | 
			
		||||
	"log"
 | 
			
		||||
	"time"
 | 
			
		||||
@@ -10,16 +11,20 @@ import (
 | 
			
		||||
const broker = "tcp://mqtt.seeseepuff.be:1883"
 | 
			
		||||
const telemetryTopic = "spider/telemetry/"
 | 
			
		||||
 | 
			
		||||
func publishTelemetry(client mqtt.Client, topic string, payload any) {
 | 
			
		||||
	token := client.Publish(telemetryTopic+topic, 0, false, payload)
 | 
			
		||||
	token.Wait()
 | 
			
		||||
	if token.Error() != nil {
 | 
			
		||||
		log.Printf("Error publishing servo battery state: %v\n", token.Error())
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func publishServoBatteryTelemetry(client mqtt.Client) {
 | 
			
		||||
	value, err := GetBatteryServo()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	token := client.Publish(telemetryTopic+"servo_battery", 0, false, fmt.Sprintf("%f", value))
 | 
			
		||||
	token.Wait()
 | 
			
		||||
	if token.Error() != nil {
 | 
			
		||||
		log.Printf("Error publishing servo battery state: %v\n", token.Error())
 | 
			
		||||
	}
 | 
			
		||||
	publishTelemetry(client, "servo_battery", fmt.Sprintf("%f", value))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func publishCpuBatteryTelemetry(client mqtt.Client) {
 | 
			
		||||
@@ -27,18 +32,92 @@ func publishCpuBatteryTelemetry(client mqtt.Client) {
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	token := client.Publish(telemetryTopic+"cpu_battery", 0, false, fmt.Sprintf("%f", value))
 | 
			
		||||
	token.Wait()
 | 
			
		||||
	if token.Error() != nil {
 | 
			
		||||
		log.Printf("Error publishing servo battery state: %v\n", token.Error())
 | 
			
		||||
	}
 | 
			
		||||
	publishTelemetry(client, "cpu_battery", fmt.Sprintf("%f", value))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func publishTelemetry(client mqtt.Client) {
 | 
			
		||||
func publishSlowTelemetry(client mqtt.Client) {
 | 
			
		||||
	go publishServoBatteryTelemetry(client)
 | 
			
		||||
	go publishCpuBatteryTelemetry(client)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func onPing(client mqtt.Client, msg mqtt.Message) {
 | 
			
		||||
	log.Print("Got ping")
 | 
			
		||||
	publishTelemetry(client, "pong", msg.Payload())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func handleWebcam(client mqtt.Client) {
 | 
			
		||||
	cam, err := webcam.Open("/dev/video0")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatalf("Could not open webcam: %v\n", err)
 | 
			
		||||
	}
 | 
			
		||||
	defer cam.Close()
 | 
			
		||||
 | 
			
		||||
	supportedFormat := cam.GetSupportedFormats()
 | 
			
		||||
	//log.Printf("Supported formats:\n")
 | 
			
		||||
	imageFormat := webcam.PixelFormat(0)
 | 
			
		||||
	for format, description := range supportedFormat {
 | 
			
		||||
		if description == "10-bit Bayer GBGB/RGRG" {
 | 
			
		||||
			imageFormat = format
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	//width, height := 1296, 972
 | 
			
		||||
	//width, height := uint32(2592), uint32(1944)
 | 
			
		||||
	width, height := uint32(640), uint32(480)
 | 
			
		||||
 | 
			
		||||
	if imageFormat != webcam.PixelFormat(0) {
 | 
			
		||||
		_, _, _, err = cam.SetImageFormat(imageFormat, width, height)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Fatalf("Could not set image format: %v\n", err)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	supportedFramerates := cam.GetSupportedFramerates(imageFormat, width, height)
 | 
			
		||||
	log.Printf("Supported frame rates:\n")
 | 
			
		||||
	for _, format := range supportedFramerates {
 | 
			
		||||
		log.Printf(" - %s\n", format)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	controls := cam.GetControls()
 | 
			
		||||
	log.Printf("Controls:\n")
 | 
			
		||||
	for _, control := range controls {
 | 
			
		||||
		log.Printf(" - %s\n", control)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	//cam.SetAutoWhiteBalance(true)
 | 
			
		||||
	//cam.SetControl()
 | 
			
		||||
 | 
			
		||||
	//err = cam.SetFramerate(10)
 | 
			
		||||
	//if err != nil {
 | 
			
		||||
	//	log.Fatalf("Could not set framerate: %v\n", err)
 | 
			
		||||
	//}
 | 
			
		||||
 | 
			
		||||
	err = cam.StartStreaming()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatalf("Could not start streaming webcam: %v\n", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	count := 0
 | 
			
		||||
	for {
 | 
			
		||||
		err = cam.WaitForFrame(100)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Fatalf("Could not start streaming webcam: %v\n", err)
 | 
			
		||||
		}
 | 
			
		||||
		frame, err := cam.ReadFrame()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Fatalf("Could not read frame: %v\n", err)
 | 
			
		||||
		}
 | 
			
		||||
		if len(frame) > 0 {
 | 
			
		||||
			count++
 | 
			
		||||
			if count > 30*4 {
 | 
			
		||||
				print("Publishing frame\n")
 | 
			
		||||
				publishTelemetry(client, "camfeed", frame)
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	opts := mqtt.NewClientOptions()
 | 
			
		||||
	opts.AddBroker(broker)
 | 
			
		||||
@@ -51,16 +130,24 @@ func main() {
 | 
			
		||||
		log.Fatalf("Error connecting to MQTT broker: %v\n", token.Error())
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	log.Print("Subscribing to command topics")
 | 
			
		||||
	token = client.Subscribe("spider/command/ping", 0, onPing)
 | 
			
		||||
	token.Wait()
 | 
			
		||||
	if token.Error() != nil {
 | 
			
		||||
		log.Fatalf("Failed to subscribe to command topic: %v\n", token.Error())
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	InitBattery()
 | 
			
		||||
	//go handleWebcam(client)
 | 
			
		||||
 | 
			
		||||
	ticker := time.NewTicker(3 * time.Second)
 | 
			
		||||
	defer ticker.Stop()
 | 
			
		||||
	slowTelemetry := time.NewTicker(3 * time.Second)
 | 
			
		||||
	defer slowTelemetry.Stop()
 | 
			
		||||
 | 
			
		||||
	publishTelemetry(client)
 | 
			
		||||
	publishSlowTelemetry(client)
 | 
			
		||||
	for {
 | 
			
		||||
		select {
 | 
			
		||||
		case <-ticker.C:
 | 
			
		||||
			publishTelemetry(client)
 | 
			
		||||
		case <-slowTelemetry.C:
 | 
			
		||||
			publishSlowTelemetry(client)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user