Fix execution on rpi
This commit is contained in:
		
							
								
								
									
										19
									
								
								part/part.go
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								part/part.go
									
									
									
									
									
								
							@@ -1,11 +1,12 @@
 | 
			
		||||
package part
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"github.com/cyrilix/robocar-base/mqttdevice"
 | 
			
		||||
	"github.com/cyrilix/robocar-base/service"
 | 
			
		||||
	"github.com/cyrilix/robocar-base/types"
 | 
			
		||||
	mqtt "github.com/eclipse/paho.mqtt.golang"
 | 
			
		||||
	"log"
 | 
			
		||||
	log "github.com/sirupsen/logrus"
 | 
			
		||||
	"sync"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
@@ -78,13 +79,7 @@ func (p *ThrottlePart) Stop() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (p *ThrottlePart) onDriveMode(_ mqtt.Client, message mqtt.Message) {
 | 
			
		||||
	payload := message.Payload()
 | 
			
		||||
	value := mqttdevice.NewMqttValue(payload)
 | 
			
		||||
	m, err := value.DriveModeValue()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Printf("invalid drive mode: %v", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	m := types.ParseString(string(message.Payload()))
 | 
			
		||||
 | 
			
		||||
	p.muDriveMode.Lock()
 | 
			
		||||
	defer p.muDriveMode.Unlock()
 | 
			
		||||
@@ -93,17 +88,17 @@ func (p *ThrottlePart) onDriveMode(_ mqtt.Client, message mqtt.Message) {
 | 
			
		||||
 | 
			
		||||
func (p *ThrottlePart) onRCThrottle(_ mqtt.Client, message mqtt.Message) {
 | 
			
		||||
	payload := message.Payload()
 | 
			
		||||
	value := mqttdevice.NewMqttValue(payload)
 | 
			
		||||
	val, err := value.Float64Value()
 | 
			
		||||
	var throttle types.Throttle
 | 
			
		||||
	err := json.Unmarshal(payload, &throttle)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Printf("invalid throttle value from arduino: %v", err)
 | 
			
		||||
		log.Errorf("unable to parse throttle json: %v", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	p.muDriveMode.RLock()
 | 
			
		||||
	defer p.muDriveMode.RUnlock()
 | 
			
		||||
	if p.driveMode == types.DriveModeUser {
 | 
			
		||||
		p.pub.Publish(p.throttleTopic, mqttdevice.NewMqttValue(types.Throttle{Value: val, Confidence: 1.0}))
 | 
			
		||||
		p.pub.Publish(p.throttleTopic, mqttdevice.NewMqttValue(throttle))
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -28,16 +28,16 @@ func TestDefaultThrottle(t *testing.T) {
 | 
			
		||||
	p := NewPart(nil, pub, throttleTopic, driveModeTopic, rcThrottleTopic, minValue, 1., 200)
 | 
			
		||||
 | 
			
		||||
	cases := []struct {
 | 
			
		||||
		driveMode        types.DriveMode
 | 
			
		||||
		rcThrottle       float64
 | 
			
		||||
		expectedThrottle float64
 | 
			
		||||
		driveMode        string
 | 
			
		||||
		rcThrottle       types.Throttle
 | 
			
		||||
		expectedThrottle types.Throttle
 | 
			
		||||
	}{
 | 
			
		||||
		{types.DriveModeUser, 0.3, 0.3},
 | 
			
		||||
		{types.DriveModePilot, 0.5, minValue},
 | 
			
		||||
		{types.DriveModePilot, 0.4, minValue},
 | 
			
		||||
		{types.DriveModeUser, 0.5, 0.5},
 | 
			
		||||
		{types.DriveModeUser, 0.4, 0.4},
 | 
			
		||||
		{types.DriveModeUser, 0.6, 0.6},
 | 
			
		||||
		{types.ToString(types.DriveModeUser), types.Throttle{Value: 0.3, Confidence: 1.0},types.Throttle{Value: 0.3, Confidence: 1.0}},
 | 
			
		||||
		{types.ToString(types.DriveModePilot), types.Throttle{Value: 0.5, Confidence: 1.0}, types.Throttle{Value: minValue, Confidence: 1.0}},
 | 
			
		||||
		{types.ToString(types.DriveModePilot), types.Throttle{Value: 0.4, Confidence: 1.0}, types.Throttle{Value: minValue, Confidence: 1.0}},
 | 
			
		||||
		{types.ToString(types.DriveModeUser), types.Throttle{Value: 0.5, Confidence: 1.0}, types.Throttle{Value: 0.5, Confidence: 1.0}},
 | 
			
		||||
		{types.ToString(types.DriveModeUser), types.Throttle{Value: 0.4, Confidence: 1.0}, types.Throttle{Value: 0.4, Confidence: 1.0}},
 | 
			
		||||
		{types.ToString(types.DriveModeUser), types.Throttle{Value: 0.6, Confidence: 1.0}, types.Throttle{Value: 0.6, Confidence: 1.0}},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	go p.Start()
 | 
			
		||||
@@ -60,7 +60,7 @@ func TestDefaultThrottle(t *testing.T) {
 | 
			
		||||
				t.Fail()
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			if throttle.Value != c.expectedThrottle {
 | 
			
		||||
			if throttle != c.expectedThrottle {
 | 
			
		||||
				t.Errorf("bad throttle value for mode %v: %v, wants %v", c.driveMode, throttle.Value, c.expectedThrottle)
 | 
			
		||||
			}
 | 
			
		||||
			if throttle.Confidence != 1. {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user