-
Notifications
You must be signed in to change notification settings - Fork 110
/
imuhwt905.go
132 lines (114 loc) · 2.91 KB
/
imuhwt905.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Package imuwit implements wit imu
package imuwit
/*
Sensor Manufacturer: Wit-motion
Supported Sensor Models: HWT905
Supported OS: Linux
This driver only supports HWT905-TTL model of Wit imu.
Tested Sensor Models and User Manuals:
HWT905 TTL: https://drive.google.com/file/d/1RV7j8yzZjPsPmvQY--1UHr_FhBzc2YwO/view
*/
import (
"bufio"
"context"
"errors"
"time"
slib "github.com/jacobsa/go-serial/serial"
"go.viam.com/rdk/components/movementsensor"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/utils"
)
var model905 = resource.DefaultModelFamily.WithModel("imu-wit-hwt905")
func init() {
resource.RegisterComponent(movementsensor.API, model905, resource.Registration[movementsensor.MovementSensor, *Config]{
Constructor: newWit905,
})
}
// newWit creates a new Wit IMU.
func newWit905(
ctx context.Context,
deps resource.Dependencies,
conf resource.Config,
logger logging.Logger,
) (movementsensor.MovementSensor, error) {
newConf, err := resource.NativeConfig[*Config](conf)
if err != nil {
return nil, err
}
i := wit{
Named: conf.ResourceName().AsNamed(),
logger: logger,
err: movementsensor.NewLastError(1, 1),
baudRate: newConf.BaudRate,
serialPath: newConf.Port,
}
options := slib.OpenOptions{
PortName: i.serialPath,
BaudRate: i.baudRate,
DataBits: 8,
StopBits: 1,
MinimumReadSize: 1,
}
if err := i.Reconfigure(ctx, deps, conf); err != nil {
return nil, err
}
logger.Debugf("initializing wit serial connection with parameters: %+v", options)
i.port, err = slib.Open(options)
if err != nil {
return nil, err
}
portReader := bufio.NewReader(i.port)
i.start905UpdateLoop(portReader, logger)
return &i, nil
}
func (imu *wit) start905UpdateLoop(portReader *bufio.Reader, logger logging.Logger) {
imu.hasMagnetometer = false
imu.workers = utils.NewStoppableWorkers(func(cancelCtx context.Context) {
for {
if cancelCtx.Err() != nil {
return
}
select {
case <-cancelCtx.Done():
return
case <-time.After(10 * time.Second):
logger.Warn("ReadString timeout exceeded")
return
default:
line, err := readWithTimeout(portReader, 'U')
if err != nil {
logger.Error(err)
continue
}
switch {
case len(line) != 11:
imu.numBadReadings++
default:
imu.err.Set(imu.parseWIT(line))
}
}
}
})
}
// readWithTimeout tries to read from the buffer until the delimiter is found or timeout occurs.
func readWithTimeout(r *bufio.Reader, delim byte) (string, error) {
lineChan := make(chan string)
errChan := make(chan error)
go func() {
line, err := r.ReadString(delim)
if err != nil {
errChan <- err
return
}
lineChan <- line
}()
select {
case line := <-lineChan:
return line, nil
case err := <-errChan:
return "", err
case <-time.After(10 * time.Second):
return "", errors.New("timeout exceeded while reading from serial port")
}
}