-
Notifications
You must be signed in to change notification settings - Fork 8
/
client.go
181 lines (156 loc) · 5.02 KB
/
client.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package sdk
import (
"fmt"
"io/ioutil"
"net"
"os"
"github.com/brunoga/robomaster/sdk/modules/event"
"github.com/brunoga/robomaster/sdk/modules/push"
"github.com/brunoga/robomaster/sdk/modules/armor"
"github.com/brunoga/robomaster/sdk/modules/blaster"
"github.com/brunoga/robomaster/sdk/modules/chassis"
"github.com/brunoga/robomaster/sdk/modules/control"
"github.com/brunoga/robomaster/sdk/modules/finder"
"github.com/brunoga/robomaster/sdk/modules/gimbal"
"github.com/brunoga/robomaster/sdk/modules/robot"
"github.com/brunoga/robomaster/sdk/modules/sound"
"github.com/brunoga/robomaster/sdk/modules/video"
"github.com/brunoga/robomaster/sdk/support/logger"
)
// Client enables controlling a RoboMaster robot through the plain-text SDK
// API (https://robomaster-dev.readthedocs.io/en/latest/).
type Client struct {
logger *logger.Logger
finderModule *finder.Finder
controlModule *control.Control
pushModule *push.Push
eventModule *event.Event
robotModule *robot.Robot
gimbalModule *gimbal.Gimbal
chassisModule *chassis.Chassis
videoModule *video.Video
armorModule *armor.Armor
blasterModule *blaster.Blaster
soundModule *sound.Sound
}
// NewClient returns a new client instance associated with the given ip. If ip
// is nil, the Client will try to detect a robot broadcasting its ip in the
// network.
func NewClient(ip net.IP) (*Client, error) {
// Setup logging.
l := logger.New(ioutil.Discard, os.Stdout, os.Stdout, os.Stderr)
// Initialize all modules.
finderModule := finder.New(l)
if ip != nil {
finderModule.SetIP(ip)
}
controlModule, err := control.New(finderModule, l)
if err != nil {
return nil, fmt.Errorf("error creating control module: %w", err)
}
pushModule, err := push.NewPush(controlModule)
if err != nil {
return nil, fmt.Errorf("error creating push module: %w", err)
}
eventModule, err := event.NewEvent(controlModule)
if err != nil {
return nil, fmt.Errorf("error creating event module: %w", err)
}
robotModule := robot.New(controlModule)
gimbalModule := gimbal.New(controlModule, pushModule)
chassisModule := chassis.New(controlModule, pushModule)
armorModule := armor.New(controlModule, eventModule)
blasterModule := blaster.New(controlModule)
soundModule := sound.New(eventModule)
videoModule, err := video.New(controlModule)
if err != nil {
panic(err)
}
return &Client{
l,
finderModule,
controlModule,
pushModule,
eventModule,
robotModule,
gimbalModule,
chassisModule,
videoModule,
armorModule,
blasterModule,
soundModule,
}, nil
}
// NewClientUSB creates a Client that tries to connect to the default USB
// connection ip.
func NewClientUSB() (*Client, error) {
return NewClient(net.ParseIP("192.168.42.2"))
}
// NewClientWifiDirect creates a Client that tries to connect to the default
// WiFi Direct connection ip.
func NewClientWifiDirect() (*Client, error) {
return NewClient(net.ParseIP("192.168.2.1"))
}
// Open opens the Client connection to the robot and enters SDK mode. Returns a
// nil error on success and a non-nil error on failure.
func (c *Client) Open() error {
err := c.controlModule.Open()
if err != nil {
return fmt.Errorf("error starting control module: %w", err)
}
// Enter SDK mode.
err = c.controlModule.SendDataExpectOk("command;")
if err != nil {
return fmt.Errorf("error entering to sdk mode: %w", err)
}
return nil
}
// Close exits SDk mode and closes the Client connection to the robot. Returns
// a nil error on success and a non-nil error on failure.
func (c *Client) Close() error {
// Leave SDK mode.
err := c.controlModule.SendDataExpectOk("quit;")
if err != nil {
return fmt.Errorf("error leaving sdk mode: %w", err)
}
err = c.controlModule.Close()
if err != nil {
return fmt.Errorf("error stopping control module")
}
return nil
}
// RobotModule returns a pointer to the associated Robot module. Used for
// doing generic robot-related operations.
func (c *Client) RobotModule() *robot.Robot {
return c.robotModule
}
// GimbalModule returns a pointer to the associated Gimbal module. Used for
// doing gimbal-related operations.
func (c *Client) GimbalModule() *gimbal.Gimbal {
return c.gimbalModule
}
// ChassisModule returns a pointer to the associated Chassis module. Used for
// doing chassis-related operations.
func (c *Client) ChassisModule() *chassis.Chassis {
return c.chassisModule
}
// VideoModule returns a pointer to the associated Video module. Used for
// doing video-related operations.
func (c *Client) VideoModule() *video.Video {
return c.videoModule
}
// ArmorModule returns a pointer to the associated Armor module. Used for
// setting/getting hit sensitivity and detecting hits.
func (c *Client) ArmorModule() *armor.Armor {
return c.armorModule
}
// BlasterModule returns a pointer to the associated Video module. Used for
// firing beads.
func (c *Client) BlasterModule() *blaster.Blaster {
return c.blasterModule
}
// SoundModule returns a pointer to the associated Video module. Used for
// detecting applause (clapping) sounds.
func (c *Client) SoundModule() *sound.Sound {
return c.soundModule
}