This repository has been archived by the owner on Feb 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
174 lines (151 loc) · 3.79 KB
/
main.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
package main
import (
"fmt"
"log"
"net"
"os"
"os/exec"
"os/signal"
"time"
"github.com/stianeikeland/go-rpio/v4"
)
// キッカーパワーが入力された時に、値を固定する関数
// 並列での処理が行われる
func kickCheck(chkicker chan bool) {
for {
//ストレートキックが入力されたとき
if kicker_enable {
//500ミリ秒待つ
time.Sleep(500 * time.Millisecond)
//ストレートキックをオフにし、値を0にする
kicker_enable = false
kicker_val = 0
}
//チップキックが入力されたとき
if chip_enable {
//500ミリ秒待つ
time.Sleep(500 * time.Millisecond)
//チップキックをオフにし、値を0にする
chip_enable = false
chip_val = 0
}
if imuError {
time.Sleep(500 * time.Millisecond)
imuError = false
}
//ループを行うため、少し待機する
time.Sleep(16 * time.Millisecond)
}
}
func main() {
//自動アップデート
go confirmAndSelfUpdate()
//GPIOの初期化
if err := rpio.Open(); err != nil {
fmt.Println(err)
os.Exit(1)
}
defer rpio.Close()
//GPIO 6, 25, 4, 5 を DIP 1, 2, 3, 4 に設定。入力
dip1 := rpio.Pin(6)
dip1.Input()
dip1.PullUp()
dip2 := rpio.Pin(25)
dip2.Input()
dip2.PullUp()
dip3 := rpio.Pin(4)
dip3.Input()
dip3.PullUp()
dip4 := rpio.Pin(5)
dip4.Input()
dip4.PullUp()
// HEXの値を表示
hex := dip1.Read() ^ 1 + (dip2.Read()^1)*2 + (dip3.Read()^1)*4 + (dip4.Read()^1)*8
fmt.Println("GOT ID FROM DIP SW:", int(hex))
diptoid := int(hex)
netInterfaceAddresses, _ := net.InterfaceAddrs()
ip := "0.0.0.0"
for _, netInterfaceAddress := range netInterfaceAddresses {
networkIp, ok := netInterfaceAddress.(*net.IPNet)
if ok && !networkIp.IP.IsLoopback() && networkIp.IP.To4() != nil {
ip = networkIp.IP.String()
}
}
//Ctrl+Cを押したときに終了するようにする
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
//終了時にGPIOを解放する
rpio.Close()
log.Println("Bye")
os.Exit(0)
}
}()
//Hostnameを取得する
cmd := exec.Command("hostname")
out, err := cmd.Output()
if err != nil {
panic(err)
}
fmt.Println(string(out))
//もし初期値のraspberrypiだったら
if string(out) == "raspberrypi\n" {
//UNIX時間の下5桁を取得する
unixtime := time.Now().UnixNano()
log.Println("Unixtime is " + fmt.Sprintf("%d", unixtime))
unixtime = unixtime % 100000
//Hostnameをracoon-XXXXXに変更する
hostname := "racoon-" + fmt.Sprintf("%05d", unixtime)
log.Println("Change Hostname To " + hostname)
//Change Hostname
//hostnamectl set-hostname raspberrypi コマンド実行
cmd = exec.Command("hostnamectl", "set-hostname", hostname)
cmd.Run()
//再起動
log.Println("=====Reboot=====")
buzzer := rpio.Pin(12)
buzzer.Mode(rpio.Pwm)
buzzer.Freq(1175 * 64)
buzzer.DutyCycle(16, 32)
time.Sleep(500 * time.Millisecond)
buzzer.DutyCycle(0, 32)
buzzer.Freq(1396 * 64)
buzzer.DutyCycle(16, 32)
time.Sleep(500 * time.Millisecond)
buzzer.DutyCycle(0, 32)
buzzer.Freq(1760 * 64)
buzzer.DutyCycle(16, 32)
time.Sleep(500 * time.Millisecond)
buzzer.DutyCycle(0, 32)
//reboot コマンド実行
cmd = exec.Command("reboot")
cmd.Run()
}
//MyIDで指定したロボットIDを取得
var MyID uint32 = uint32(diptoid)
chclient := make(chan bool)
chserver := make(chan bool)
chserial := make(chan bool)
chkick := make(chan bool)
chgpio := make(chan bool)
chapi := make(chan bool)
//各並列処理部分
go RunClient(chclient, MyID, ip)
go RunServer(chserver, MyID)
go RunSerial(chserial, MyID)
go kickCheck(chkick)
go RunGPIO(chgpio)
go RunApi(chapi, MyID)
<-chclient
<-chserver
<-chserial
<-chkick
<-chgpio
<-chapi
}
func CheckError(err error) {
if err != nil {
log.Fatal("Error: ", err)
}
}