feat: remove secondary controler and manage max Throttle with channel3

This commit is contained in:
cyrilix committed 2023-11-02 18:50:14 +01:00
1 parent f960fcc768
commit f85239537f
3 files changed
+121 -94

No files matched your search

+66 -41
View File
@@ -63,8 +63,7 @@ func TestArduinoPart_Update(t *testing.T) {
a := Part{client: nil, serial: conn, pubFrequency: 100,
pwmSteeringConfig: NewAsymetricPWMConfig(MinPwmAngle, MaxPwmAngle, MiddlePwmAngle),
pwmThrottleConfig: &DefaultPwmThrottle,
pwmSecondaryThrottleConfig: &DefaultPwmThrottle,
pwmSecondarySteeringConfig: NewPWMConfig(MinPwmThrottle, MaxPwmThrottle),
pwmMaxThrottleCtrlConfig: &DefaultPwmThrottle,
throttleFeedbackThresholds: tools.NewThresholdConfig(),
}
go func() {
@@ -77,98 +76,106 @@ func TestArduinoPart_Update(t *testing.T) {
channel1, channel2, channel3, channel4, channel5, channel6, channel7, channel8, channel9 := 678, 910, 1012, 1678, 1910, 112, 0, 0, 0
cases := []struct {
name, content string
throttlePwmConfig *PWMConfig
expectedThrottle, expectedSteering float32
expectedDriveMode events.DriveMode
expectedSwitchRecord bool
name, content string
throttlePwmConfig *PWMConfig
expectedThrottle, expectedSteering, expectedMaxThrottleCtrl float32
expectedDriveMode events.DriveMode
expectedSwitchRecord bool
}{
{"Good value",
fmt.Sprintf("12345,%d,%d,%d,%d,%d,%d,%d,%d,%d,50\n", channel1, channel2, channel3, channel4, channel5, channel6, channel7, channel8, channel9),
defaultPwmThrottleConfig, -1., -1.,
defaultPwmThrottleConfig, -1., -1., 0.01,
events.DriveMode_USER, false},
{"Invalid line",
"12350,invalid line\n", defaultPwmThrottleConfig,
-1., -1., events.DriveMode_INVALID, false},
-1., -1., 0.01, events.DriveMode_INVALID, false},
{"Switch record on",
fmt.Sprintf("12355,%d,%d,%d,%d,%d,%d,%d,%d,%d,50\n", channel1, channel2, channel3, channel4, 998, channel6, channel7, channel8, channel9),
defaultPwmThrottleConfig, -1., -1., events.DriveMode_USER, true},
defaultPwmThrottleConfig, -1., -1., 0.01, events.DriveMode_USER, true},
{"Switch record off",
fmt.Sprintf("12360,%d,%d,%d,%d,%d,%d,%d,%d,%d,50\n", channel1, channel2, channel3, channel4, 1987, channel6, channel7, channel8, channel9),
defaultPwmThrottleConfig, -1., -1., events.DriveMode_USER, false},
defaultPwmThrottleConfig, -1., -1., 0.01, events.DriveMode_USER, false},
{"Switch record off",
fmt.Sprintf("12365,%d,%d,%d,%d,%d,%d,%d,%d,%d,50\n", channel1, channel2, channel3, channel4, 1850, channel6, channel7, channel8, channel9),
defaultPwmThrottleConfig, -1., -1., events.DriveMode_USER, false},
defaultPwmThrottleConfig, -1., -1., 0.01, events.DriveMode_USER, false},
{"Switch record on",
fmt.Sprintf("12370,%d,%d,%d,%d,%d,%d,%d,%d,%d,50\n", channel1, channel2, channel3, channel4, 1003, channel6, channel7, channel8, channel9),
defaultPwmThrottleConfig, -1., -1., events.DriveMode_USER, true},
defaultPwmThrottleConfig, -1., -1., 0.01, events.DriveMode_USER, true},
{"DriveMode: user",
fmt.Sprintf("12375,%d,%d,%d,%d,%d,%d,%d,%d,%d,50\n", channel1, channel2, channel3, channel4, channel5, 998, channel7, channel8, channel9),
defaultPwmThrottleConfig, -1., -1., events.DriveMode_USER, false},
defaultPwmThrottleConfig, -1., -1., 0.01, events.DriveMode_USER, false},
{"DriveMode: pilot",
fmt.Sprintf("12380,%d,%d,%d,%d,%d,%d,%d,%d,%d,50\n", channel1, channel2, channel3, channel4, channel5, 1987, channel7, channel8, channel9),
defaultPwmThrottleConfig, -1., -1., events.DriveMode_PILOT, false},
defaultPwmThrottleConfig, -1., -1., 0.01, events.DriveMode_PILOT, false},
{"DriveMode: pilot",
fmt.Sprintf("12385,%d,%d,%d,%d,%d,%d,%d,%d,%d,50\n", channel1, channel2, channel3, channel4, channel5, 1850, channel7, channel8, channel9),
defaultPwmThrottleConfig, -1., -1., events.DriveMode_PILOT, false},
defaultPwmThrottleConfig, -1., -1., 0.01, events.DriveMode_PILOT, false},
// DriveMode: user
{"DriveMode: user",
fmt.Sprintf("12390,%d,%d,%d,%d,%d,%d,%d,%d,%d,50\n", channel1, channel2, channel3, channel4, channel5, 1003, channel7, channel8, channel9),
defaultPwmThrottleConfig, -1., -1., events.DriveMode_USER, false},
defaultPwmThrottleConfig, -1., -1., 0.01, events.DriveMode_USER, false},
// DriveMode: copilot
{"DriveMode: copilot",
fmt.Sprintf("12390,%d,%d,%d,%d,%d,%d,%d,%d,%d,50\n", channel1, channel2, channel3, channel4, channel5, 1250, channel7, channel8, channel9),
defaultPwmThrottleConfig, -1., -1., events.DriveMode_COPILOT, false},
defaultPwmThrottleConfig, -1., -1., 0.01, events.DriveMode_COPILOT, false},
{"Sterring: over left", fmt.Sprintf("12395,%d,%d,%d,%d,%d,%d,%d,%d,%d,50\n", 99, channel2, channel3, channel4, channel5, channel6, channel7, channel8, channel9),
defaultPwmThrottleConfig, -1., -1., events.DriveMode_USER, false},
defaultPwmThrottleConfig, -1., -1., 0.01, events.DriveMode_USER, false},
{"Sterring: left",
fmt.Sprintf("12400,%d,%d,%d,%d,%d,%d,%d,%d,%d,50\n", int(MinPwmAngle+40), channel2, channel3, channel4, channel5, channel6, channel7, channel8, channel9),
defaultPwmThrottleConfig, -1., -0.92, events.DriveMode_USER, false},
defaultPwmThrottleConfig, -1., -0.92, 0.01, events.DriveMode_USER, false},
{"Sterring: middle",
fmt.Sprintf("12405,%d,%d,%d,%d,%d,%d,%d,%d,%d,50\n", 1450, channel2, channel3, channel4, channel5, channel6, channel7, channel8, channel9),
defaultPwmThrottleConfig, -1., -0.09, events.DriveMode_USER, false},
defaultPwmThrottleConfig, -1., -0.09, 0.01, events.DriveMode_USER, false},
{"Sterring: right",
fmt.Sprintf("12410,%d,%d,%d,%d,%d,%d,%d,%d,%d,50\n", 1958, channel2, channel3, channel4, channel5, channel6, channel7, channel8, channel9),
defaultPwmThrottleConfig, -1., 0.95, events.DriveMode_USER, false},
defaultPwmThrottleConfig, -1., 0.95, 0.01, events.DriveMode_USER, false},
{"Sterring: over right",
fmt.Sprintf("12415,%d,%d,%d,%d,%d,%d,%d,%d,%d,50\n", 2998, channel2, channel3, channel4, channel5, channel6, channel7, channel8, channel9),
defaultPwmThrottleConfig, -1., 1., events.DriveMode_USER, false},
defaultPwmThrottleConfig, -1., 1., 0.01, events.DriveMode_USER, false},
{"Throttle: over down",
fmt.Sprintf("12420,%d,%d,%d,%d,%d,%d,%d,%d,%d,50\n", channel1, 99, channel3, channel4, channel5, channel6, channel7, channel8, channel9),
defaultPwmThrottleConfig, -1., -1., events.DriveMode_USER, false},
defaultPwmThrottleConfig, -1., -1., 0.01, events.DriveMode_USER, false},
{"Throttle: down",
fmt.Sprintf("12425,%d,%d,%d,%d,%d,%d,%d,%d,%d,50\n", channel1, 998, channel3, channel4, channel5, channel6, channel7, channel8, channel9),
defaultPwmThrottleConfig, -0.95, -1., events.DriveMode_USER, false},
defaultPwmThrottleConfig, -0.95, -1., 0.01, events.DriveMode_USER, false},
{"Throttle: stop",
fmt.Sprintf("12430,%d,%d,%d,%d,%d,%d,%d,%d,%d,50\n", channel1, 1450, channel3, channel4, channel5, channel6, channel7, channel8, channel9),
NewPWMConfig(1000, 1900), 0.0, -1., events.DriveMode_USER, false},
NewPWMConfig(1000, 1900), 0.0, -1., 0.01, events.DriveMode_USER, false},
{"Throttle: up",
fmt.Sprintf("12435,%d,%d,%d,%d,%d,%d,%d,%d,%d,50\n", channel1, 1948, channel3, channel4, channel5, channel6, channel7, channel8, channel9),
defaultPwmThrottleConfig, 0.99, -1., events.DriveMode_USER, false},
defaultPwmThrottleConfig, 0.99, -1., 0.01, events.DriveMode_USER, false},
{"Throttle: over up",
fmt.Sprintf("12440,%d,%d,%d,%d,%d,%d,%d,%d,%d,50\n", channel1, 2998, channel3, channel4, channel5, channel6, channel7, channel8, channel9),
defaultPwmThrottleConfig, 1., -1., events.DriveMode_USER, false},
defaultPwmThrottleConfig, 1., -1., 0.01, events.DriveMode_USER, false},
{"Throttle: zero not middle",
fmt.Sprintf("12440,%d,%d,%d,%d,%d,%d,%d,%d,%d,50\n", channel1, 1600, channel3, channel4, channel5, channel6, channel7, channel8, channel9),
&PWMConfig{1000, 1700, 1500},
0.5, -1., events.DriveMode_USER, false},
{"Use 2nd rc: use channels 7 and 8",
fmt.Sprintf("12440,%d,%d,%d,%d,%d,%d,%d,%d,%d,50\n", 1000, 1000, 1950, channel4, channel5, channel6, 2000, 2008, channel9),
defaultPwmThrottleConfig, 1., 1, events.DriveMode_USER, false},
0.5, -1., 0.01, events.DriveMode_USER, false},
{"MaxThrottleCtrl: Too low value",
fmt.Sprintf("12440,%d,%d,%d,%d,%d,%d,%d,%d,%d,50\n", channel1, channel2, 100, channel4, channel5, channel6, 2000, 2008, channel9),
defaultPwmThrottleConfig, -1., -1, 0., events.DriveMode_USER, false},
{"MaxThrottleCtrl: low value",
fmt.Sprintf("12440,%d,%d,%d,%d,%d,%d,%d,%d,%d,50\n", channel1, channel2, 1050, channel4, channel5, channel6, 2000, 2008, channel9),
defaultPwmThrottleConfig, -1., -1, 0.05, events.DriveMode_USER, false},
{"MaxThrottleCtrl: High value",
fmt.Sprintf("12440,%d,%d,%d,%d,%d,%d,%d,%d,%d,50\n", channel1, channel2, 1900, channel4, channel5, channel6, 2000, 2008, channel9),
defaultPwmThrottleConfig, -1., -1, 0.91, events.DriveMode_USER, false},
{"MaxThrottleCtrl: Too High value",
fmt.Sprintf("12440,%d,%d,%d,%d,%d,%d,%d,%d,%d,50\n", channel1, channel2, 4005, channel4, channel5, channel6, 2000, 2008, channel9),
defaultPwmThrottleConfig, -1., -1, 1, events.DriveMode_USER, false},
{"Drive Mode: user",
fmt.Sprintf("12430,%d,%d,%d,%d,%d,%d,%d,%d,%d,50\n", channel1, channel6, channel3, channel4, channel5, 900, channel7, channel8, channel9),
defaultPwmThrottleConfig, -1., -1., events.DriveMode_USER, false},
defaultPwmThrottleConfig, -1., -1., 0.01, events.DriveMode_USER, false},
{"Drive Mode: pilot",
fmt.Sprintf("12430,%d,%d,%d,%d,%d,%d,%d,%d,%d,50\n", channel1, channel6, channel3, channel4, channel5, 1950, channel7, channel8, channel9),
defaultPwmThrottleConfig, -1., -1., events.DriveMode_PILOT, false},
defaultPwmThrottleConfig, -1., -1., 0.01, events.DriveMode_PILOT, false},
{"Drive Mode: no value",
fmt.Sprintf("12430,%d,%d,%d,%d,%d,%d,%d,%d,%d,50\n", channel1, channel6, channel3, channel4, channel5, -1, channel7, channel8, channel9),
defaultPwmThrottleConfig, -1., -1., events.DriveMode_INVALID, false},
defaultPwmThrottleConfig, -1., -1., 0.01, events.DriveMode_INVALID, false},
}
for _, c := range cases {
@@ -195,6 +202,9 @@ func TestArduinoPart_Update(t *testing.T) {
if fmt.Sprintf("%0.2f", a.Steering()) != fmt.Sprintf("%0.2f", c.expectedSteering) {
t.Errorf("%s: bad steering value, expected: %0.2f, actual: %.2f", c.name, c.expectedSteering, a.Steering())
}
if fmt.Sprintf("%0.2f", a.MaxThrottleCtrl()) != fmt.Sprintf("%0.2f", c.expectedMaxThrottleCtrl) {
t.Errorf("%s: bad MaxThrottleCtrl value, expected: %0.2f, actual: %.2f", c.name, c.expectedMaxThrottleCtrl, a.MaxThrottleCtrl())
}
if a.DriveMode() != c.expectedDriveMode {
t.Errorf("%s: bad drive mode, expected: %v, actual:%v", c.name, c.expectedDriveMode, a.DriveMode())
}
@@ -241,6 +251,7 @@ func TestPublish(t *testing.T) {
serial: conn,
pubFrequency: pubFrequency,
throttleTopic: "car/part/arduino/throttle/target",
maxThrottleCtrlTopic: "car/part/arduino/throttle/max",
steeringTopic: "car/part/arduino/steering",
driveModeTopic: "car/part/arduino/drive_mode",
switchRecordTopic: "car/part/arduino/switch_record",
@@ -254,33 +265,38 @@ func TestPublish(t *testing.T) {
throttle, steering float32
driveMode events.DriveMode
throttleFeedback float32
maxThrottleCtrl float32
switchRecord bool
expectedThrottle events.ThrottleMessage
expectedSteering events.SteeringMessage
expectedDriveMode events.DriveModeMessage
expectedSwitchRecord events.SwitchRecordMessage
expectedThrottleFeedback events.ThrottleMessage
expectedMaxThrottleCtrl events.ThrottleMessage
}{
{-1, 1, events.DriveMode_USER, 0.3, true,
{-1, 1, events.DriveMode_USER, 0.3, 0.5, true,
events.ThrottleMessage{Throttle: -1., Confidence: 1.},
events.SteeringMessage{Steering: 1.0, Confidence: 1.},
events.DriveModeMessage{DriveMode: events.DriveMode_USER},
events.SwitchRecordMessage{Enabled: false},
events.ThrottleMessage{Throttle: 0.3, Confidence: 1.},
events.ThrottleMessage{Throttle: 0.5, Confidence: 1.},
},
{0, 0, events.DriveMode_PILOT, 0.4, false,
{0, 0, events.DriveMode_PILOT, 0.4, 0.5, false,
events.ThrottleMessage{Throttle: 0., Confidence: 1.},
events.SteeringMessage{Steering: 0., Confidence: 1.},
events.DriveModeMessage{DriveMode: events.DriveMode_PILOT},
events.SwitchRecordMessage{Enabled: true},
events.ThrottleMessage{Throttle: 0.4, Confidence: 1.},
events.ThrottleMessage{Throttle: 0.5, Confidence: 1.},
},
{0.87, -0.58, events.DriveMode_PILOT, 0.5, false,
{0.87, -0.58, events.DriveMode_PILOT, 0.5, 0.5, false,
events.ThrottleMessage{Throttle: 0.87, Confidence: 1.},
events.SteeringMessage{Steering: -0.58, Confidence: 1.},
events.DriveModeMessage{DriveMode: events.DriveMode_PILOT},
events.SwitchRecordMessage{Enabled: true},
events.ThrottleMessage{Throttle: 0.5, Confidence: 1.},
events.ThrottleMessage{Throttle: 0.5, Confidence: 1.},
},
}
@@ -288,6 +304,7 @@ func TestPublish(t *testing.T) {
a.mutex.Lock()
a.throttle = c.throttle
a.steering = c.steering
a.maxThrottleCtrl = c.maxThrottleCtrl
a.driveMode = c.driveMode
a.ctrlRecord = c.switchRecord
a.throttleFeedback = c.throttleFeedback
@@ -334,6 +351,14 @@ func TestPublish(t *testing.T) {
if throttleFeedbackMsg.String() != c.expectedThrottleFeedback.String() {
t.Errorf("msg(car/part/arduino/throttle/feedback): %v, wants %v", throttleFeedbackMsg.String(), c.expectedThrottleFeedback.String())
}
var maxThrottleCtrlMsg events.ThrottleMessage
muPublishedEvents.Lock()
unmarshalMsg(t, pulishedEvents["car/part/arduino/throttle/max"], &maxThrottleCtrlMsg)
muPublishedEvents.Unlock()
if maxThrottleCtrlMsg.String() != c.expectedMaxThrottleCtrl.String() {
t.Errorf("msg(car/part/arduino/throttle/max): %v, wants %v", maxThrottleCtrlMsg.String(), c.expectedMaxThrottleCtrl.String())
}
}
}
@@ -501,8 +526,8 @@ func Test_convertPwmSteeringToPercent(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := convertPwmSteeringToPercent(tt.args.value, tt.args.steeringConfig); got != tt.want {
t.Errorf("convertPwmSteeringToPercent() = %v, want %v", got, tt.want)
if got := convertPwmToPercent(tt.args.value, tt.args.steeringConfig); got != tt.want {
t.Errorf("convertPwmToPercent() = %v, want %v", got, tt.want)
}
})
}