-
Notifications
You must be signed in to change notification settings - Fork 0
/
movement.go
116 lines (104 loc) · 2.38 KB
/
movement.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
package main
import (
"fmt"
"log"
"github.com/knei-knurow/roverctl/requests"
"github.com/urfave/cli/v2"
)
var goCommand = cli.Command{
Name: "go",
Usage: "move rover forward or backward",
OnUsageError: func(context *cli.Context, err error, isSubcommand bool) error {
log.Println("error:", err)
return nil
},
Flags: []cli.Flag{
&cli.IntFlag{
Name: "speed",
Aliases: []string{"s"},
Value: 0,
Usage: "some speed parameter",
},
},
Subcommands: []*cli.Command{
{
Name: "forward",
Usage: "move the rover forward",
OnUsageError: func(context *cli.Context, err error, isSubcommand bool) error {
log.Println("error:", err)
return nil
},
Action: func(c *cli.Context) error {
body := map[string]interface{}{
"type": "go",
"direction": "forward",
"speed": c.Int("speed"),
}
err := requests.PostRequest(addr+"/move", body)
if err != nil {
return fmt.Errorf("make request: %v", err)
}
return nil
},
},
{
Name: "backward",
Usage: "move the rover backward",
Action: func(c *cli.Context) error {
body := map[string]interface{}{
"type": "go",
"direction": "backward",
"speed": c.Int("speed"),
}
err := requests.PostRequest(addr+"/move", body)
if err != nil {
return fmt.Errorf("make request: %v", err)
}
return nil
},
},
{
Name: "stop",
Usage: "stop the rover",
Action: func(c *cli.Context) error {
body := map[string]interface{}{
"type": "go",
"direction": "stop",
"speed": 255,
}
err := requests.PostRequest(addr+"/move", body)
if err != nil {
return fmt.Errorf("make request: %v", err)
}
return nil
},
},
},
Action: func(c *cli.Context) error {
log.Printf("no direction passed (forward/backward)")
return nil
},
}
var turnCommand = cli.Command{
Name: "turn",
Usage: "turn motors using mounted servos. Bear in mind that the rover should be moving while turning",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "degrees",
Aliases: []string{"d"},
Value: 0,
Usage: "degrees",
},
},
Action: func(c *cli.Context) error {
body := map[string]interface{}{
"type": "turn",
"degrees": c.Int("degrees"),
}
err := requests.PostRequest(addr+"/move", body)
if err != nil {
return fmt.Errorf("make request: %v", err)
}
return nil
},
}