arduino stuffs
l1 and r1 controls
| -rw-r--r-- | car_driver/.vscode/arduino.json | 2 | ||||
| -rw-r--r-- | car_driver/.vscode/settings.json | 3 | ||||
| -rw-r--r-- | car_driver/arduino/arduino.ino | 66 | ||||
| -rw-r--r-- | car_driver/godot/Console.gd | 12 | ||||
| -rw-r--r-- | car_driver/godot/Input.gd | 30 | ||||
| -rw-r--r-- | car_driver/godot/Main.tscn | 9 | ||||
| -rw-r--r-- | car_driver/godot/bin/linux64/libGDSercomm.so | bin | 88760 -> 90280 bytes | |||
| -rw-r--r-- | car_driver/godot/project.godot | 20 | ||||
| -rw-r--r-- | car_driver/godot/serial.gd | 29 | ||||
| -rw-r--r-- | car_driver/processing/processing.pde | 45 |
10 files changed, 91 insertions, 125 deletions
diff --git a/car_driver/.vscode/arduino.json b/car_driver/.vscode/arduino.json index f6fba73..5ef71f2 100644 --- a/car_driver/.vscode/arduino.json +++ b/car_driver/.vscode/arduino.json @@ -1,6 +1,6 @@ { "board": "arduino:avr:uno", "sketch": "arduino/arduino.ino", - "port": "/dev/ttyACM0", + "port": "/dev/ttyACM1", "output": "/tmp/arduinologs" }
\ No newline at end of file diff --git a/car_driver/.vscode/settings.json b/car_driver/.vscode/settings.json index d3ba527..94de60a 100644 --- a/car_driver/.vscode/settings.json +++ b/car_driver/.vscode/settings.json @@ -1,4 +1,5 @@ { "C_Cpp.errorSquiggles": "Disabled", - "arduino.defaultBaudRate": 9600 + "arduino.defaultBaudRate": 9600, + "arduino.clearOutputOnBuild": true } diff --git a/car_driver/arduino/arduino.ino b/car_driver/arduino/arduino.ino index 98e1b2c..6c5f4e6 100644 --- a/car_driver/arduino/arduino.ino +++ b/car_driver/arduino/arduino.ino @@ -1,5 +1,4 @@ #define HEADER 'H' -#define TOTAL_BYTES 3 const int MOVE_FORWARD[2] = {0, 2}; // 2 == -1 == up const int MOVE_LEFT[2] = {2, 0}; // move left @@ -11,45 +10,50 @@ void setup() { Serial.begin(9600); Serial.println("initialized"); begin(); - processCommand(0, 2); } void loop() { - if (Serial.available() >= TOTAL_BYTES) { - // sample format: H11 or H02, etc. - char header = Serial.read(); - if (header == HEADER) { // done reading header, next number is a tag - char tmp = Serial.read(); - int x = (tmp - '0'); - tmp = Serial.read(); - int y = (tmp - '0'); - processCommand(x, y); // process the command + if (Serial.available() > 0) { + String string = Serial.readString(); + string.trim(); + char header = string[0]; + if (header == HEADER) { + int delimiter = string.indexOf(','); + int x = string.substring(1, delimiter).toInt(); + int y = string.substring(delimiter + 1).toInt(); + processCommand(x, y); + } else { + Serial.println("wrong header: " + String(header)); } } } +#define DEADZONE 10 // @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 (arrayCmp(cmd, MOVE_FORWARD)) { - forward(); - } else if (arrayCmp(cmd, MOVE_STOP)) { - stop(); - } else if (arrayCmp(cmd, MOVE_LEFT)) { - left(); - } else if (arrayCmp(cmd, MOVE_RIGHT)) { - right(); - } else if (arrayCmp(cmd, MOVE_BACK)) { - backward(); + if (x > 200 || x < 0 || y > 200 || y < 0) { + Serial.println("invalid command"); + } + if (x < DEADZONE) { + // apply brakes to motora + brakeMotorA(); + } else if (x > 100) { + motorABackward((x - 100) + 155); } else { - Serial.print("( "); - Serial.print(x); - Serial.print(", "); - Serial.print(y); - Serial.println(" ) Ignored"); + // apply speed to motora + motorAForward(x + 155); } -} - -bool arrayCmp(const int array[], const int array2[]) { - return array[0] == array2[0] && array[1] == array2[1]; + if (y < DEADZONE) { + // apply brakes to motorb + brakeMotorB(); + } else if (y > 100) { + motorBBackward((y - 100) + 155); + } else { + // apply speed to motorb + motorBForward(y + 155); + } + Serial.print("x: "); + Serial.print(String(x)); + Serial.print(" y: "); + Serial.println(String(y)); }
\ No newline at end of file diff --git a/car_driver/godot/Console.gd b/car_driver/godot/Console.gd index 29ac7a9..ce199cf 100644 --- a/car_driver/godot/Console.gd +++ b/car_driver/godot/Console.gd @@ -1,10 +1,12 @@ extends RichTextLabel class_name Console +var thread = Thread.new() -func _ready(): - SerialIO.connect("recieved", self, "add_text") +func _physics_process(_delta:=0.0)->void: + thread.start(self, "readstring") + thread.wait_to_finish() - -func add_text(new: String): - text += new +func readstring()->void: + for _i in range(SerialIO.Serial.get_available()): + text += str(SerialIO.Serial.read())
\ No newline at end of file diff --git a/car_driver/godot/Input.gd b/car_driver/godot/Input.gd index 4606f45..bbc4e49 100644 --- a/car_driver/godot/Input.gd +++ b/car_driver/godot/Input.gd @@ -1,6 +1,9 @@ extends Node -var input : Vector2 +var input: Vector2 + +onready var lb = $Label + func prepare(v: Vector2) -> Vector2: v = v.normalized() @@ -8,18 +11,21 @@ func prepare(v: Vector2) -> Vector2: v.x = 1 - v.x if v.y < 0: v.y = 1 - v.y - v=v.round() + 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)) + input = get_input() + lb.text =str(input) + SerialIO.send("H%s,%s" % [input.x, input.y]) + + +func get_input() -> Vector2: + var x := Input.get_action_strength("leftpaddle") + var y := Input.get_action_strength("rightpaddle") + if Input.is_action_pressed("lb"): + x = x + 1 if x != 0 else 1.01 + if Input.is_action_pressed("rb"): + y = y + 1 if y != 0 else 1.01 + return (Vector2(x, y) * 100).round() diff --git a/car_driver/godot/Main.tscn b/car_driver/godot/Main.tscn index f420f25..e223496 100644 --- a/car_driver/godot/Main.tscn +++ b/car_driver/godot/Main.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=3 format=2] +[gd_scene load_steps=4 format=2] [ext_resource path="res://Console.tscn" type="PackedScene" id=1] [ext_resource path="res://Input.gd" type="Script" id=2] +[ext_resource path="res://theme.theme" type="Theme" id=3] [node name="Main" type="Control"] anchor_right = 1.0 @@ -11,3 +12,9 @@ anchor_bottom = 1.0 [node name="input" type="Node" parent="."] script = ExtResource( 2 ) + +[node name="Label" type="Label" parent="input"] +margin_right = 40.0 +margin_bottom = 14.0 +theme = ExtResource( 3 ) +text = "input" diff --git a/car_driver/godot/bin/linux64/libGDSercomm.so b/car_driver/godot/bin/linux64/libGDSercomm.so Binary files differindex bf96e61..1922451 100644 --- a/car_driver/godot/bin/linux64/libGDSercomm.so +++ b/car_driver/godot/bin/linux64/libGDSercomm.so diff --git a/car_driver/godot/project.godot b/car_driver/godot/project.godot index c30ab58..52ee5a5 100644 --- a/car_driver/godot/project.godot +++ b/car_driver/godot/project.godot @@ -69,6 +69,26 @@ ui_down={ , Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":1,"axis_value":1.0,"script":null) ] } +leftpaddle={ +"deadzone": 0.5, +"events": [ Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":6,"axis_value":1.0,"script":null) + ] +} +rightpaddle={ +"deadzone": 0.5, +"events": [ Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":7,"axis_value":1.0,"script":null) + ] +} +lb={ +"deadzone": 0.5, +"events": [ Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":4,"pressure":0.0,"pressed":false,"script":null) + ] +} +rb={ +"deadzone": 0.5, +"events": [ Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":5,"pressure":0.0,"pressed":false,"script":null) + ] +} [rendering] diff --git a/car_driver/godot/serial.gd b/car_driver/godot/serial.gd index dfb3a0f..6a1ba34 100644 --- a/car_driver/godot/serial.gd +++ b/car_driver/godot/serial.gd @@ -5,35 +5,6 @@ onready var Serial = preload("res://bin/GDsercomm.gdns").new() const baud_rate := 9600 const endline := "\n" -signal recieved(text) - -#a readline function, just add the current Port (based on sercomm) -#and it will return a line, since sercomm always use a timeout, it should not lag - - -func readline(port): - if !port.has_method("read"): #to avoid problems - return "NOT A PORT" - var cho = "" - var chango = "" - while cho != endline: - cho = port.read() - if typeof(cho) == TYPE_STRING: - if cho != endline: - chango += cho - else: - chango = "FAILED" - break - return chango - - -func _physics_process(_delta: float): - var text = "" - for _i in range(Serial.get_available()): - text += str(Serial.read()) - if text: - emit_signal("recieved", text) - func send(text: String) -> void: #"please only use ascii" Serial.write(text) diff --git a/car_driver/processing/processing.pde b/car_driver/processing/processing.pde deleted file mode 100644 index 5e10e54..0000000 --- a/car_driver/processing/processing.pde +++ /dev/null @@ -1,45 +0,0 @@ -import processing.serial.*; - -private static char STOP = 'v'; -private static char HEADER = 'H'; - -char current = STOP; - -Serial port; - -void setup() { - frameRate(5); - printArray(Serial.list()); - port = new Serial(this, Serial.list()[0], 9600); -} - - -void draw() {} // Empty draw() needed to keep the program running - -void keyPressed() { - updateKeys(key, true); -} - -void keyReleased() { - updateKeys(key, false); -} - -boolean isValidKey(char key) { - return key == 'a' || key == 'w' || key == 's' || key == 'd'; -} - -void write(char key) { - char data[] = {HEADER, key}; - String towrite = new String(data); - println(towrite); - port.write(towrite); -} - -void updateKeys(char key, boolean on) { - if (isValidKey(key)) { - if (!on || key != current) { - current = on ? key : STOP; - write(current); - } - } -} |