Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit 8d1b515

Browse files
committed
🎉 Buzzer & DIP ID Change
1 parent b57e4b0 commit 8d1b515

2 files changed

Lines changed: 134 additions & 23 deletions

File tree

RACOON-Pi

330 KB
Binary file not shown.

main.go

Lines changed: 134 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"log"
88
"math"
99
"net"
10-
"strconv"
11-
"strings"
10+
"os"
11+
"os/signal"
1212
"time"
1313

1414
"github.com/Rione-SSL/RACOON-Pi/proto/pb_gen"
@@ -241,6 +241,12 @@ func RunSerial(chclient chan bool, MyID uint32) {
241241
}
242242
}
243243

244+
if kicker_enable {
245+
sendbytes[6] = kicker_val
246+
} else {
247+
sendbytes[6] = 0
248+
}
249+
244250
//それぞれのデータを表示
245251
log.Printf("VOLT: %f, BALLSENS: %t, IMUDEG: %d\n", float32(recvdata.Volt)*0.1, recvdata.IsHoldBall, recvdata.ImuDir)
246252

@@ -292,42 +298,120 @@ func RunGPIO(chgpio chan bool) {
292298
err := rpio.Open()
293299
CheckError(err)
294300

295-
//GPIO21をLED1に設定。出力
296-
led := rpio.Pin(21)
301+
//GPIO18をLED1に設定。出力
302+
led := rpio.Pin(18)
297303
led.Output()
298304

299-
//GPIO26をLED2に設定。出力
300-
led2 := rpio.Pin(26)
305+
//GPIO27をLED2に設定。出力
306+
led2 := rpio.Pin(27)
301307
led.Output()
302308

303-
//GPIO19をbutton1に設定。入力
304-
button1 := rpio.Pin(19)
309+
//GPIO22をbutton1に設定。入力(S2)
310+
button1 := rpio.Pin(22)
305311
button1.Input()
312+
button1.PullUp()
313+
314+
//GPIO 24をbutton2に設定。入力(S3)
315+
button2 := rpio.Pin(24)
316+
button2.Input()
317+
button2.PullUp()
318+
319+
//GPIO12をブザーPWMに設定。出力
320+
buzzer := rpio.Pin(12)
321+
buzzer.Mode(rpio.Pwm)
322+
buzzer.Freq(64000)
323+
buzzer.DutyCycle(0, 32)
324+
325+
// buzzer.DutyCycle(16, 32)
326+
// time.Sleep(time.Millisecond * 100)
327+
// buzzer.DutyCycle(0, 32)
328+
// time.Sleep(time.Millisecond * 100)
329+
330+
buzzer.Freq(1244 * 64)
331+
buzzer.DutyCycle(16, 32)
332+
time.Sleep(time.Millisecond * 100)
333+
buzzer.Freq(1108 * 64)
334+
time.Sleep(time.Millisecond * 100)
335+
buzzer.Freq(739 * 64)
336+
time.Sleep(time.Millisecond * 150)
337+
buzzer.DutyCycle(0, 32)
338+
time.Sleep(time.Millisecond * 100)
339+
340+
//1479Hzにする
341+
buzzer.Freq(1479 * 64)
342+
buzzer.DutyCycle(16, 32)
343+
time.Sleep(time.Millisecond * 100)
344+
buzzer.DutyCycle(0, 32)
345+
time.Sleep(time.Millisecond * 100)
346+
buzzer.DutyCycle(16, 32)
347+
time.Sleep(time.Millisecond * 100)
348+
buzzer.DutyCycle(0, 32)
349+
350+
//GPIO 6, 25, 4, 5 を DIP 1, 2, 3, 4 に設定。入力
351+
dip1 := rpio.Pin(6)
352+
dip1.Input()
353+
dip1.PullUp()
354+
dip2 := rpio.Pin(25)
355+
dip2.Input()
356+
dip2.PullUp()
357+
dip3 := rpio.Pin(4)
358+
dip3.Input()
359+
dip3.PullUp()
360+
dip4 := rpio.Pin(5)
361+
dip4.Input()
362+
dip4.PullUp()
363+
364+
// DIP1, 2, 3 ,4 の状態を出力
365+
fmt.Println("DIP1:", dip1.Read()^1)
366+
fmt.Println("DIP2:", dip2.Read()^1)
367+
fmt.Println("DIP3:", dip3.Read()^1)
368+
fmt.Println("DIP4:", dip4.Read()^1)
369+
370+
//DIP 1, 2, 3, 4からhexを作成
371+
hex := dip1.Read() ^ 1 + (dip2.Read()^1)*2 + (dip3.Read()^1)*4 + (dip4.Read()^1)*8
372+
373+
//hexを表示
374+
fmt.Println("HEX:", int(hex))
306375

307376
//Lチカ速度
308377
ledsec := 500 * time.Millisecond
309378
for {
310379
//電圧降下検知
311-
if recvdata.Volt <= 140 {
380+
if recvdata.Volt <= 155 {
381+
buzzer.Freq(1200 * 64)
382+
buzzer.DutyCycle(16, 32)
383+
312384
//高速チカチカ
313385
led2.High()
314386
time.Sleep(100 * time.Millisecond)
387+
388+
buzzer.Freq(760 * 64)
315389
led2.Low()
316-
time.Sleep(100 * time.Millisecond)
390+
time.Sleep(150 * time.Millisecond)
317391
} else {
318392
//通常チカチカ。ボタンが押されたら高速チカチカ
319393
//ボタンが押されたら、imuをリセットする
320394
time.Sleep(ledsec)
321395
led.Write(rpio.High)
322-
if button1.Read() == rpio.High {
396+
if button1.Read()^1 == rpio.High {
323397
imuReset = true
324398
ledsec = 100 * time.Millisecond
399+
buzzer.DutyCycle(16, 32)
325400
} else {
326401
imuReset = false
327402
ledsec = 500 * time.Millisecond
328403
}
404+
if button2.Read()^1 == rpio.High {
405+
//kickする
406+
buzzer.DutyCycle(16, 32)
407+
time.Sleep(500 * time.Millisecond)
408+
buzzer.DutyCycle(0, 32)
409+
kicker_enable = true
410+
kicker_val = 100
411+
}
329412
time.Sleep(ledsec)
330413
led.Write(rpio.Low)
414+
buzzer.DutyCycle(0, 32)
331415
}
332416
}
333417
}
@@ -368,6 +452,32 @@ func kickCheck(chkicker chan bool) {
368452

369453
func main() {
370454

455+
//GPIOの初期化
456+
if err := rpio.Open(); err != nil {
457+
fmt.Println(err)
458+
os.Exit(1)
459+
}
460+
defer rpio.Close()
461+
462+
//GPIO 6, 25, 4, 5 を DIP 1, 2, 3, 4 に設定。入力
463+
dip1 := rpio.Pin(6)
464+
dip1.Input()
465+
dip1.PullUp()
466+
dip2 := rpio.Pin(25)
467+
dip2.Input()
468+
dip2.PullUp()
469+
dip3 := rpio.Pin(4)
470+
dip3.Input()
471+
dip3.PullUp()
472+
dip4 := rpio.Pin(5)
473+
dip4.Input()
474+
dip4.PullUp()
475+
476+
// HEXの値を表示
477+
hex := dip1.Read() ^ 1 + (dip2.Read()^1)*2 + (dip3.Read()^1)*4 + (dip4.Read()^1)*8
478+
fmt.Println("GOT ID FROM DIP SW:", int(hex))
479+
diptoid := int(hex)
480+
371481
netInterfaceAddresses, _ := net.InterfaceAddrs()
372482

373483
ip := "0.0.0.0"
@@ -377,20 +487,21 @@ func main() {
377487
ip = networkIp.IP.String()
378488
}
379489
}
380-
//IPアドレスを表示
381-
fmt.Println("Resolved Host IP: " + ip)
382-
//IPアドレスの各数字部分を分解
383-
//例: 192.168.100.101 の場合、 192 が[0]、168 が[1]、100 が[2]、101 が[3]
384-
hostpart := strings.Split(ip, ".")
385-
//上記例の[3]なので、101の部分を取得
386-
iptoid, _ := strconv.Atoi(hostpart[3])
387-
// 100を引いてロボットIDを決定
388-
iptoid = iptoid - 100
389-
//上記推測の結果を表示
390-
fmt.Println("Estimated Robot ID: " + strconv.Itoa(iptoid))
490+
491+
//Ctrl+Cを押したときに終了するようにする
492+
c := make(chan os.Signal, 1)
493+
signal.Notify(c, os.Interrupt)
494+
go func() {
495+
for range c {
496+
//終了時にGPIOを解放する
497+
rpio.Close()
498+
log.Println("Bye")
499+
os.Exit(0)
500+
}
501+
}()
391502

392503
//MyIDで指定したロボットIDを取得
393-
var MyID uint32 = uint32(iptoid)
504+
var MyID uint32 = uint32(diptoid)
394505

395506
chclient := make(chan bool)
396507
chserver := make(chan bool)

0 commit comments

Comments
 (0)