Skip to content

Commit

Permalink
Added message.Decode method
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Shiverick committed Mar 25, 2021
1 parent 51c1b50 commit 36ce676
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
42 changes: 41 additions & 1 deletion pkg/descriptor/message.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package descriptor

import "time"
import (
"time"

"go.einride.tech/can"
)

// Message describes a CAN message.
type Message struct {
Expand Down Expand Up @@ -35,3 +39,39 @@ func (m *Message) MultiplexerSignal() (*Signal, bool) {
}
return nil, false
}

// Decode decodes a can Payload into a decoded signal array
func (m *Message) Decode(p *can.Payload) []DecodedSignal {

var data can.Data
if m.Length <= 8 {
copy(data[:], p.Data)
}

numSignals := len(m.Signals)

signals := make([]DecodedSignal, numSignals)
for i, signal := range m.Signals {

var valueDesc string
var value float64
if m.Length > 8 {
valueDesc, _ = signal.UnmarshalValueDescriptionPayload(p)
value = signal.UnmarshalPhysicalPayload(p)
} else {
valueDesc, _ = signal.UnmarshalValueDescription(data)
value = signal.UnmarshalPhysical(data)
}

s := DecodedSignal{
Value: value,
Description: valueDesc,
Signal: signal,
}

signals[i] = s

}
return signals

}
9 changes: 9 additions & 0 deletions pkg/descriptor/signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ type Signal struct {
DefaultValue int
}

type DecodedSignal struct {
// Value is the physical value of a decoded signal
Value float64
// Description physical descritpion of a decoded signal
Description string
// Signal is a pointer to the dbc signal
Signal *Signal
}

// ValueDescription returns the value description for the provided value.
func (s *Signal) ValueDescription(value int) (string, bool) {
for _, vd := range s.ValueDescriptions {
Expand Down
41 changes: 41 additions & 0 deletions tests/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,20 @@ func TestDecodeEACVariables(t *testing.T) {
t.Errorf("signal[%s] value = %s ; expected: %s", name, valueDesc, expectedMap[name].description)
}
}

for _, signal := range message.Decode(&payload) {
value := signal.Value
valueDesc := signal.Description
name := signal.Signal.Name

if value != expectedMap[name].value {
t.Errorf("signal[%s] value = %f ; expected: %f", name, value, expectedMap[name].value)
}

if valueDesc != expectedMap[name].description {
t.Errorf("signal[%s] value = %s ; expected: %s", name, valueDesc, expectedMap[name].description)
}
}
}

func TestDecodeDisconnectState(t *testing.T) {
Expand Down Expand Up @@ -345,6 +359,33 @@ func TestDecodeSensorSonarsData(t *testing.T) {
}
}

func TestMessageDecodeSensorSonarsData(t *testing.T) {

data := can.Data{0x01, 0x01, 0x01, 0x02, 0x01, 0x00}
payload := can.Payload{Data: data[:]}

message, _ := db.Message(uint32(500))
fmt.Println(message.Length)

decodedSignals := message.Decode(&payload)
for _, signal := range decodedSignals {
value := signal.Value
valueDesc := signal.Description
name := signal.Signal.Name

valueFromData := signal.Signal.UnmarshalPhysical(data)
descFromData, _ := signal.Signal.UnmarshalValueDescription(data)

if value != valueFromData {
t.Errorf("signal[%s] value = %f ; expected: %f", name, value, valueFromData)
}

if valueDesc != descFromData {
t.Errorf("signal[%s] value = %s ; expected: %s", name, valueDesc, descFromData)
}
}
}

func BenchmarkDecodeData(b *testing.B) {

message, _ := db.Message(uint32(500))
Expand Down

0 comments on commit 36ce676

Please sign in to comment.