]> totsipaki.net Git - Scripts.git/blob - education/robotics/wedo.sh
Αρχικό ανέβασμα φακέλων και άδειας + script wedo 2.0
[Scripts.git] / education / robotics / wedo.sh
1 #!/bin/bash
2 #script για τον έλεγχο του κινητήρα του wedo 2.0
3 #Copyright Parafestas Nikos 2025 totsipaki.net
4
5 #This program is free software: you can redistribute it and/or modify
6 #it under the terms of the GNU General Public License as published by
7 #the Free Software Foundation, either version 3 of the License, or
8 #(at your option) any later version.
9
10 #This program is distributed in the hope that it will be useful,
11 #but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 #GNU General Public License for more details.
14
15 #You should have received a copy of the GNU General Public License
16 #along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 WEDO_MAC="XX:XX:XX:XX:XX:XX" #find it with bluetoothctl --> scan on --> devices
19 HANDLE="0x003d"
20 KEEP_ALIVE_INTERVAL=1
21
22 # Συνάρτηση καθαρισμού
23 cleanup() {
24 stop_motor
25 if [ -n "$MOTOR_PID" ]; then
26 kill $MOTOR_PID 2>/dev/null
27 fi
28 echo -e "\nΤερματισμός προγράμματος..."
29 exit 0
30 }
31
32 trap cleanup EXIT INT
33
34 # Έλεγχος κινητήρα
35 stop_motor() {
36 gatttool -b "$WEDO_MAC" --char-write-req -a "$HANDLE" -n 01010100 >/dev/null 2>&1
37 echo "Ο κινητήρας σταμάτησε"
38 }
39
40 start_motor() {
41 while true; do
42 gatttool -b "$WEDO_MAC" --char-write-req -a "$HANDLE" -n 010101$1 >/dev/null 2>&1
43 sleep $KEEP_ALIVE_INTERVAL
44 done
45 }
46
47 # Εμφάνιση οδηγιών
48 clear
49 echo "========================================"
50 echo " Έλεγχος Κινητήρα LEGO WeDo 2.0"
51 echo "========================================"
52 echo -e "Χρησιμοποιήστε:\n -> Δεξί Βέλος: Κίνηση Εμπρός\n <- Αριστερό Βέλος: Κίνηση Πίσω\n X: Διακοπή κινητήρα\n Ctrl+C: Έξοδος\n"
53
54 # Ανάγνωση πλήκτρων
55 read_key() {
56 IFS= read -rsn1 key
57 if [[ $key == $'\x1b' ]]; then
58 read -rsn2 key
59 case $key in
60 "[C") echo "right" ;;
61 "[D") echo "left" ;;
62 esac
63 else
64 [[ $key == "x" ]] && echo "stop" || echo ""
65 fi
66 }
67
68 # Κύρια λειτουργία
69 while true; do
70 case $(read_key) in
71 "right")
72 [ -n "$MOTOR_PID" ] && kill $MOTOR_PID 2>/dev/null
73 start_motor 64 &
74 MOTOR_PID=$!
75 echo "Κίνηση Εμπρός"
76 ;;
77 "left")
78 [ -n "$MOTOR_PID" ] && kill $MOTOR_PID 2>/dev/null
79 start_motor 9C &
80 MOTOR_PID=$!
81 echo "Κίνηση Πίσω"
82 ;;
83 "stop")
84 stop_motor
85 [ -n "$MOTOR_PID" ] && kill $MOTOR_PID 2>/dev/null
86 MOTOR_PID=""
87 ;;
88 esac
89 done