arduino stuffs
bendn 2022-06-12
parent db483b8 · commit e8784f4
-rw-r--r--car_driver/arduino/arduino.ino31
-rw-r--r--car_driver/godot/Input.gd15
-rw-r--r--car_driver/godot/Main.tscn6
-rw-r--r--car_driver/godot/project.godot31
4 files changed, 58 insertions, 25 deletions
diff --git a/car_driver/arduino/arduino.ino b/car_driver/arduino/arduino.ino
index fee8153..98e1b2c 100644
--- a/car_driver/arduino/arduino.ino
+++ b/car_driver/arduino/arduino.ino
@@ -1,6 +1,5 @@
#define HEADER 'H'
-
-#define TOTAL_BYTES 3 // hmmm
+#define TOTAL_BYTES 3
const int MOVE_FORWARD[2] = {0, 2}; // 2 == -1 == up
const int MOVE_LEFT[2] = {2, 0}; // move left
@@ -12,6 +11,7 @@ void setup() {
Serial.begin(9600);
Serial.println("initialized");
begin();
+ processCommand(0, 2);
}
void loop() {
@@ -23,31 +23,15 @@ void loop() {
int x = (tmp - '0');
tmp = Serial.read();
int y = (tmp - '0');
- Serial.println(y, BIN);
- Serial.println(x, BIN);
- Serial.println("this is what I recived boss");
- if (is_tag_valid(x, y)) {
- processCommand(x, y); // process the command
- } else {
- Serial.print(x, BIN);
- Serial.print(y, BIN);
- Serial.println(": unknown tag");
- }
- } else {
- Serial.print("unknown header: ");
- Serial.println(header);
+ processCommand(x, y); // process the command
}
}
}
-bool is_tag_valid(const int x, const int y) {
- return x > 0 && x < 3 && y > 0 && y < 3;
-}
-
// @param cmd the thing that tells it what to do, usually a vector(01, 22).
void processCommand(const int x, const int y) {
const int cmd[] = {x, y};
- if (cmd == MOVE_FORWARD) {
+ if (arrayCmp(cmd, MOVE_FORWARD)) {
forward();
} else if (arrayCmp(cmd, MOVE_STOP)) {
stop();
@@ -58,12 +42,11 @@ void processCommand(const int x, const int y) {
} else if (arrayCmp(cmd, MOVE_BACK)) {
backward();
} else {
- Serial.print('[');
+ Serial.print("( ");
Serial.print(x);
- Serial.print(cmd[0]);
- Serial.print(cmd[1]);
+ Serial.print(", ");
Serial.print(y);
- Serial.println("] Ignored");
+ Serial.println(" ) Ignored");
}
}
diff --git a/car_driver/godot/Input.gd b/car_driver/godot/Input.gd
index 1bac942..4606f45 100644
--- a/car_driver/godot/Input.gd
+++ b/car_driver/godot/Input.gd
@@ -1,5 +1,6 @@
extends Node
+var input : Vector2
func prepare(v: Vector2) -> Vector2:
v = v.normalized()
@@ -7,4 +8,18 @@ func prepare(v: Vector2) -> Vector2:
v.x = 1 - v.x
if v.y < 0:
v.y = 1 - v.y
+ v=v.round()
return v
+
+
+func _physics_process(_delta):
+ var inp := get_input()
+ if input != inp:
+ print(inp)
+ input = inp
+ SerialIO.send("H%s%s" % [inp.x, inp.y])
+
+func get_input()->Vector2:
+ var x := Input.get_axis("ui_left", "ui_right")
+ var y := Input.get_axis("ui_up", "ui_down")
+ return prepare(Vector2(x, y))
diff --git a/car_driver/godot/Main.tscn b/car_driver/godot/Main.tscn
index e2283fc..f420f25 100644
--- a/car_driver/godot/Main.tscn
+++ b/car_driver/godot/Main.tscn
@@ -1,9 +1,13 @@
-[gd_scene load_steps=2 format=2]
+[gd_scene load_steps=3 format=2]
[ext_resource path="res://Console.tscn" type="PackedScene" id=1]
+[ext_resource path="res://Input.gd" type="Script" id=2]
[node name="Main" type="Control"]
anchor_right = 1.0
anchor_bottom = 1.0
[node name="ConsoleWindow" parent="." instance=ExtResource( 1 )]
+
+[node name="input" type="Node" parent="."]
+script = ExtResource( 2 )
diff --git a/car_driver/godot/project.godot b/car_driver/godot/project.godot
index 5d6d07c..c30ab58 100644
--- a/car_driver/godot/project.godot
+++ b/car_driver/godot/project.godot
@@ -39,6 +39,37 @@ window/size/height=360
window/stretch/mode="2d"
window/stretch/aspect="expand"
+[input]
+
+ui_left={
+"deadzone": 0.5,
+"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777231,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
+, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":14,"pressure":0.0,"pressed":false,"script":null)
+, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":0,"axis_value":-1.0,"script":null)
+ ]
+}
+ui_right={
+"deadzone": 0.5,
+"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777233,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
+, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":15,"pressure":0.0,"pressed":false,"script":null)
+, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":0,"axis_value":1.0,"script":null)
+ ]
+}
+ui_up={
+"deadzone": 0.5,
+"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777232,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
+, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":12,"pressure":0.0,"pressed":false,"script":null)
+, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":1,"axis_value":-1.0,"script":null)
+ ]
+}
+ui_down={
+"deadzone": 0.5,
+"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777234,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
+, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":13,"pressure":0.0,"pressed":false,"script":null)
+, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":1,"axis_value":1.0,"script":null)
+ ]
+}
+
[rendering]
quality/driver/driver_name="GLES2"