-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths42d_rs485_test.go
111 lines (91 loc) · 2.64 KB
/
s42d_rs485_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package mks42d
import (
"fmt"
"testing"
)
func checkPayloadAgainstExpected(expected []byte, received []byte) bool {
if len(expected) != len(received) {
fmt.Println("payloads are not equivalent")
return false
}
// Iterate over the slices and compare each element
for i := 0; i < len(expected); i++ {
fmt.Printf("comparing expected %02X to received %02X\n", expected[i], received[i])
if expected[i] != received[i] {
fmt.Println("received payload did not match expected payload")
return false
}
}
return true
}
func printByteArray(printMe []byte) {
for _, b := range printMe {
fmt.Printf("%02X ", b)
}
fmt.Println()
}
// Send “FA 01 F6 01 40 02”, the motor rotates forward at acc=2, speed=320RPM
func TestCreateSpeedModePayload1(t *testing.T) {
direction := CLOCKWISE
var speed uint16 = 320
var acceleration uint8 = 2
payload, err := createSpeedModePayload(direction, speed, acceleration)
if err != nil {
fmt.Println(err.Error())
t.FailNow()
}
fmt.Println("received")
printByteArray(payload)
expectedPayload := []byte{0xFA, 0x01, 0xF6, 0x81, 0x40, 0x02}
if !checkPayloadAgainstExpected(expectedPayload, payload) {
t.FailNow()
}
}
// Send “FA 01 F6 81 40 02”, the motor reverses at acc=2, speed=320RPM
func TestCreateSpeedModePayload2(t *testing.T) {
direction := COUNTERCLOCKWISE
var speed uint16 = 320
var acceleration uint8 = 2
payload, err := createSpeedModePayload(direction, speed, acceleration)
if err != nil {
fmt.Println(err.Error())
t.FailNow()
}
printByteArray(payload)
expectedPayload := []byte{0xFA, 0x01, 0xF6, 0x01, 0x40, 0x02}
if !checkPayloadAgainstExpected(expectedPayload, payload) {
t.FailNow()
}
}
func TestCreateSpeedPayloadBadSpeed(t *testing.T) {
direction := COUNTERCLOCKWISE
var speed uint16 = 6000
var acceleration uint8 = 2
_, err := createSpeedModePayload(direction, speed, acceleration)
if err == nil {
fmt.Println("expected error for negative speed, but no error")
t.FailNow()
}
speed = 4000
_, err = createSpeedModePayload(direction, speed, acceleration)
if err == nil {
fmt.Println("expected error for out of bounds speed, but no error")
t.FailNow()
}
}
func TestCreatePositionModePayload(t *testing.T) {
direction := COUNTERCLOCKWISE
var speed uint16 = 320
var acceleration uint8 = 2
var numberOfPulses uint32 = 64000
payload, err := createPositionMode1Payload(direction, speed, acceleration, numberOfPulses)
if err != nil {
fmt.Println(err.Error())
t.FailNow()
}
printByteArray(payload)
expectedPayload := []byte{0xFA, 0x01, 0xFD, 0x01, 0x40, 0x02, 0x00, 0x00, 0xFA, 00}
if !checkPayloadAgainstExpected(expectedPayload, payload) {
t.FailNow()
}
}