This commit is contained in:
henne 2025-08-09 20:38:37 +02:00
parent b8fcce1996
commit fe0a870f6c
No known key found for this signature in database
7 changed files with 70 additions and 28 deletions

View file

@ -17,6 +17,9 @@ type Config struct {
RadarWaveform bool `form:"radar_waveform" json:"radar_waveform"`
TriggerDistance int `form:"trigger_distance" json:"trigger_distance"`
TriggerSpeed int `form:"trigger_speed" json:"trigger_speed"`
TriggerOutput int `form:"trigger_output" json:"trigger_output"`
SpeedOutput int `form:"speed_output" json:"speed_output"`
TargetOutput int `form:"target_output" json:"target_output"`
}
func initConfig() {

View file

@ -19,7 +19,6 @@ func init() {
r = radar_lib.New(config.C.Radar.Port, config.C.Radar.Baud)
r.SetEventHandler(onEvent)
r.SetSpeedHandler(onSpeedEvent)
SetCommunicationConfig()
}
func onSpeedEvent(speed int) {
@ -65,8 +64,8 @@ func SetSpeedConfig(speed int, minDistance, maxDistance, minSpeed, maxSpeed, tri
}
}
func SetCommunicationConfig() {
err := r.SetCommunicationConfig(radar_lib.PortRS485, radar_lib.Baud115200, radar_lib.OutputTypeValidOutput, radar_lib.OutputTypeNoOutput, radar_lib.OutputTypeNoOutput, 50)
func SetCommunicationConfig(speedOutput, targetOutput, triggerOutput int) {
err := r.SetCommunicationConfig(radar_lib.PortRS485, radar_lib.Baud115200, radar_lib.OutputType(speedOutput), radar_lib.OutputType(targetOutput), radar_lib.OutputType(triggerOutput), 50)
if err != nil {
log.Printf("%v", err)
}

View file

@ -104,12 +104,11 @@ type Radar struct {
func (r *Radar) write(data []byte) error {
r.lock.Lock()
defer r.lock.Unlock()
log.Printf("%v", data)
n, err := r.port.Write(data)
log.Printf("writing %#x", data)
_, err := r.port.Write(data)
if err != nil {
return err
}
log.Printf("%d data written", n)
return r.port.Drain()
}
@ -150,29 +149,13 @@ func (r *Radar) SetTargetSpeedConfig(direction Direction, minDistance, maxDistan
func (r *Radar) SetCommunicationConfig(port Port, baud Baud, speedOutput, targetOutput, triggerOutput OutputType, communicationPeriod int) error {
data := startSequence
outputProtocol := 0
switch speedOutput {
case OutputTypePeriodicOutput:
outputProtocol |= (1 << 4)
case OutputTypeValidOutput:
outputProtocol |= (1 << 5)
}
speed := byte(speedOutput)
target := byte(targetOutput)
trigger := byte(triggerOutput)
switch targetOutput {
case OutputTypePeriodicOutput:
outputProtocol |= (1 << 2)
case OutputTypeValidOutput:
outputProtocol |= (1 << 3)
}
var outputProtocol byte = (speed << 4) | (target << 2) | trigger
switch triggerOutput {
case OutputTypePeriodicOutput:
outputProtocol |= (1 << 0)
case OutputTypeValidOutput:
outputProtocol |= (1 << 1)
}
data = append(data, 0x03, byte(port), byte(baud), byte(outputProtocol), byte(communicationPeriod))
data = append(data, 0x04, byte(port), byte(baud), outputProtocol, byte(int(communicationPeriod/50)))
data = append(data, endSequence...)
return r.write(data)
}

View file

@ -21,7 +21,6 @@ func (r *Radar) listenSerial() {
for i := range n {
rcvBuf[lastIndex] = buf[i]
if i > 1 && buf[i-1] == 0x0D && buf[i] == 0x0A {
log.Printf("%v", buf)
r.decodeInput(rcvBuf[0 : lastIndex+1])
lastIndex = 0
} else {
@ -32,6 +31,7 @@ func (r *Radar) listenSerial() {
}
func (r *Radar) decodeInput(buf []byte) {
log.Printf("RCV> %x", buf)
// trigger message
if len(buf) > 2 && buf[0] == 0x56 && buf[1] == 0x50 {
log.Printf("Radar Trigger Message Event %d", buf[2])

View file

@ -89,6 +89,7 @@ func Init() {
r.GET("", HandleIndex)
r.GET("setup", HandleSetup)
r.POST("setup", HandleSetupSave)
r.POST("setup/communication", HandleCommunicationSave)
r.GET("test", HandleTest)
r.GET("ticket/:id/delete", HandleDelete)
r.GET("ticket/:id", HandleTicket)

View file

@ -38,5 +38,30 @@ func HandleSetupSave(ctx *gin.Context) {
logrus.Error(err)
}
radar.SetConfig(cfg.RadarHeight, cfg.RadarAngle, cfg.RadarWaveform)
}
type SetupCommunicationForm struct {
SpeedOutput int `form:"speed_output" json:"speed_output"`
TriggerOutput int `form:"trigger_output" json:"trigger_output"`
TargetOutput int `form:"target_output" json:"target_output"`
}
func HandleCommunicationSave(ctx *gin.Context) {
var form SetupCommunicationForm
if err := ctx.ShouldBind(&form); err != nil {
ctx.String(400, "bad input")
return
}
cfg := db.GetConfig()
cfg.TriggerOutput = form.TriggerOutput
cfg.TargetOutput = form.TargetOutput
cfg.SpeedOutput = form.SpeedOutput
db.SetConfig(*cfg)
err := templates.ExecuteTemplate(ctx.Writer, "setup", gin.H{"Config": cfg, "Msg": "Einstellungen wurden gespeichert", "MsgType": "success"})
if err != nil {
logrus.Error(err)
}
radar.SetCommunicationConfig(cfg.SpeedOutput, cfg.TargetOutput, cfg.TriggerOutput)
}

View file

@ -133,4 +133,35 @@
</fieldset>
<input type="submit" value="Speichern" />
</form>
<h1>Communication Settings</h1>
<form action="/setup/communication" method="post">
<fieldset>
<label>
Speed Output
<select name="speed_output">
<option value="0" {{ if eq .Config.SpeedOutput 0}}selected{{end}}>No Output</option>
<option value="1" {{ if eq .Config.SpeedOutput 1}}selected{{end}}>Periodic Output</option>
<option value="2" {{ if eq .Config.SpeedOutput 2}}selected{{end}}>Valid Output</option>
</select>
</label>
<label>
Target Output
<select name="target_output">
<option value="0" {{ if eq .Config.TargetOutput 0}}selected{{end}}>No Output</option>
<option value="1" {{ if eq .Config.TargetOutput 1}}selected{{end}}>Periodic Output</option>
<option value="2" {{ if eq .Config.TargetOutput 2}}selected{{end}}>Valid Output</option>
</select>
</label>
<label>
Trigger Output
<select name="trigger_output">
<option value="0" {{ if eq .Config.TriggerOutput 0}}selected{{end}}>No Output</option>
<option value="1" {{ if eq .Config.TriggerOutput 1}}selected{{end}}>Periodic Output</option>
<option value="2" {{ if eq .Config.TriggerOutput 2}}selected{{end}}>Valid Output</option>
</select>
</label>
</fieldset>
<input type="submit" value="Speichern" />
</form>
{{ template "footer" . }}