-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvehicle.go
More file actions
77 lines (62 loc) · 1.84 KB
/
vehicle.go
File metadata and controls
77 lines (62 loc) · 1.84 KB
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
package mdh_tinygo_extensions
// Vehicle struct - a vehicle is both ports on a motor controller
type Vehicle struct {
M0 Motor
M1 Motor
}
// Vehicle Functions
// Vehicle function to turn right - assumes m0 is left motor and m1 is right motor
func (v Vehicle) TurnRight() {
v.M0.SetDirection(v.M0.ForwardDirection)
v.M1.SetDirection(!v.M1.ForwardDirection)
}
// Vehicle function to turn right - assumes m0 is left motor and m1 is right motor
func (v Vehicle) TurnLeft() {
v.M0.SetDirection(!v.M0.ForwardDirection)
v.M1.SetDirection(v.M1.ForwardDirection)
}
// Vehicle automagical configuration function
func (v Vehicle) ConfigureEverything() error {
if err := v.M0.ConfigureEverything(); err != nil {
return err
}
if err := v.M1.ConfigureEverything(); err != nil {
return err
}
return nil
}
// Vehicle function to set direction for all motors
func (v Vehicle) SetDirectionAll(direction bool) {
v.M0.SetDirection(direction)
v.M1.SetDirection(direction)
}
// Vehicle function to set direction for each motor individually
func (v Vehicle) SetDirection(direction0 bool, direction1 bool) {
v.M0.SetDirection(direction0)
v.M1.SetDirection(direction1)
}
// Vehicle function to move a pair of motors in the forwards direction
func (v Vehicle) GoForwards() {
v.SetSpeed(255)
v.SetDirection(v.M0.ForwardDirection, v.M1.ForwardDirection)
}
// Vehicle function to move a pair of motors in the backwards direction
func (v Vehicle) GoBackwards() {
v.SetSpeed(255)
v.SetDirection(!v.M0.ForwardDirection, !v.M1.ForwardDirection)
}
// Vehicle function to stop a pair of motors
func (v Vehicle) Stop() {
v.M0.Stop()
v.M1.Stop()
}
// Vehicle function to start a pair of stopped motors
func (v Vehicle) Start() {
v.M0.Start()
v.M1.Start()
}
// Vehicle function to set speed
func (v Vehicle) SetSpeed(speed uint32) {
v.M0.SetSpeed(speed)
v.M1.SetSpeed(speed)
}