From 9db7c9b5d3f73b2d5083665f434d78b755a5eb5e Mon Sep 17 00:00:00 2001 From: Cyrille Nofficial Date: Thu, 2 May 2024 20:51:42 +0200 Subject: [PATCH] monitoring: add configuration logs --- cmd/rc-road/rc-road.go | 19 +++++++++++++++ road/detect.go | 13 ++++++++++ road/monitor.go | 54 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 road/monitor.go diff --git a/cmd/rc-road/rc-road.go b/cmd/rc-road/rc-road.go index d742c1d..447c91a 100644 --- a/cmd/rc-road/rc-road.go +++ b/cmd/rc-road/rc-road.go @@ -77,12 +77,30 @@ func main() { }() zap.ReplaceGlobals(lgr) + zap.S().Infof("topic road : %v", roadTopic) + zap.S().Infof("topic camera : %v", cameraTopic) + zap.S().Infof("horizon : %v", horizon) + zap.S().Infof("image width : %v", imgWidth) + zap.S().Infof("image height : %v", imgHeight) + zap.S().Infof("white threshold low : %v", whiteThresholdLow) + zap.S().Infof("white threshold high : %v", whiteThresholdHigh) + zap.S().Infof("canny threshold low : %v", cannyThresholdLow) + zap.S().Infof("canny threshold high : %v", cannyThresholdHigh) + zap.S().Infof("hough lines rho : %v", houghLinesRho) + zap.S().Infof("hough lines theta : %v", houghLinesTheta) + zap.S().Infof("hough lines threshold : %v", houghLinesThreshold) + zap.S().Infof("hough lines min line length: %v", houghLinesMinLineLength) + zap.S().Infof("hough lines max line gap : %v", houghLinesMaxLineGap) + client, err := cli.Connect(mqttBroker, username, password, clientId) if err != nil { zap.S().Fatalf("unable to connect to mqtt bus: %v", err) } defer client.Disconnect(50) + monitor := &road.DefaultMonitor{} + go monitor.Start() + p := road.NewPart(client, cameraTopic, roadTopic, road.NewDetector( @@ -96,6 +114,7 @@ func main() { road.WithRegionOfInterest(imgWidth, imgHeight, horizon), road.WithPointOnRoad(image.Point{X: imgWidth / 2, Y: imgHeight - 30}), road.WithHoughLines(houghLinesRho, float32(houghLinesTheta), houghLinesThreshold, houghLinesMinLineLength, houghLinesMaxLineGap), + road.WithMonitor(monitor), ), ) defer p.Stop() diff --git a/road/detect.go b/road/detect.go index 78ae61b..495ae59 100644 --- a/road/detect.go +++ b/road/detect.go @@ -83,6 +83,12 @@ func WithHoughLines(rho int, theta float32, threshold int, minLineLength int, ma } } +func WithMonitor(monitor Monitor) DetectorOption { + return func(d *Detector) { + d.monitor = monitor + } +} + func NewDetector(options ...DetectorOption) *Detector { whiteThreshold := 20. @@ -112,6 +118,8 @@ func NewDetector(options ...DetectorOption) *Detector { roiMask: *roiMask, pointOnRoad: pointOnRoad, + + monitor: &FakeMonitor{}, } for _, option := range options { @@ -162,6 +170,8 @@ type Detector struct { roiMask gocv.Mat pointOnRoad image.Point + + monitor Monitor } func (d *Detector) Close() { @@ -248,6 +258,9 @@ func (d *Detector) Detect(img *gocv.Mat) ([]*events.Point, *events.Ellipse) { // Run Hough on edge detected image road, ellipsis := d.getRoadShapeWithHoughLines(maskedEdges) + + d.monitor.Increment() + return road, ellipsis } diff --git a/road/monitor.go b/road/monitor.go new file mode 100644 index 0000000..14d5ccf --- /dev/null +++ b/road/monitor.go @@ -0,0 +1,54 @@ +package road + +import ( + "go.uber.org/zap" + "sync" + "time" +) + +type Monitor interface { + Increment() + Start() +} + +type DefaultMonitor struct { + muCounter sync.Mutex + counter uint +} + +func (d *DefaultMonitor) Increment() { + d.muCounter.Lock() + defer d.muCounter.Unlock() + d.counter = d.counter + 1 +} + +func (d *DefaultMonitor) Start() { + start := time.Now().UnixNano() + for { + select { + case <-time.After(10 * time.Second): + start = d.displayFPS(start) + + } + } + +} + +func (d *DefaultMonitor) displayFPS(start int64) int64 { + d.muCounter.Lock() + defer d.muCounter.Unlock() + + end := time.Now().UnixNano() + count := int64(d.counter) + go func() { + zap.S().Infof("Current FPS: %d img/s", count/((end-start)/1_000_000_000)) + }() + d.counter = 0 + return end +} + +type FakeMonitor struct{} + +func (f *FakeMonitor) Increment() {} + +func (f *FakeMonitor) Start() {}