From a56cf64fef896887b23b29dc793a4fd59cc3ea51 Mon Sep 17 00:00:00 2001 From: mxhng Date: Thu, 29 Feb 2024 15:50:53 -0500 Subject: [PATCH 1/8] electrical team files --- Electrical | 1 + 1 file changed, 1 insertion(+) create mode 160000 Electrical diff --git a/Electrical b/Electrical new file mode 160000 index 0000000..af07d26 --- /dev/null +++ b/Electrical @@ -0,0 +1 @@ +Subproject commit af07d26c9998bf3bb97f6a503b7f1cc42157f604 From a392b0670d7ed8105a5ce30875f057994dd07b8d Mon Sep 17 00:00:00 2001 From: arossaadhikary <148636324+arossaadhikary@users.noreply.github.com> Date: Thu, 7 Mar 2024 14:29:39 -0500 Subject: [PATCH 2/8] added servo scripts for arms --- Electrical | 1 - electrical_code/arms/motor_angle_movement.ino | 35 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) delete mode 160000 Electrical create mode 100644 electrical_code/arms/motor_angle_movement.ino diff --git a/Electrical b/Electrical deleted file mode 160000 index af07d26..0000000 --- a/Electrical +++ /dev/null @@ -1 +0,0 @@ -Subproject commit af07d26c9998bf3bb97f6a503b7f1cc42157f604 diff --git a/electrical_code/arms/motor_angle_movement.ino b/electrical_code/arms/motor_angle_movement.ino new file mode 100644 index 0000000..38bd94c --- /dev/null +++ b/electrical_code/arms/motor_angle_movement.ino @@ -0,0 +1,35 @@ +#include + +Servo myservo; // Create a servo object +Servo myservo2; +int lastAngleValue = 90; // Initial angle value + +void setup() { + myservo.attach(9); // Attach the servo to digital pin 9 + myservo2.attach(8); + Serial.begin(9600); +} + +int returnAngleValue() { + delay(1000); + + // Receiving Data from Computer + if (Serial.available() > 0) { + String inputString = Serial.readStringUntil('\n'); + int value = inputString.toInt(); + return value; + } + // Return the last angle value if no new value is received + return lastAngleValue; +} + +void loop() { + int newAngleValue = returnAngleValue(); + + // Update servo position only if a new angle value is received + if (newAngleValue != lastAngleValue) { + myservo.write(newAngleValue); + myservo2.write(newAngleValue); + lastAngleValue = newAngleValue; + } +} From 36adbf79766b62c50133f728fcd20e887c61ff33 Mon Sep 17 00:00:00 2001 From: mxhng Date: Thu, 7 Mar 2024 15:05:49 -0500 Subject: [PATCH 3/8] electrical code restructure --- .../arm_servos.ino} | 0 electrical_code/drive/movement/movement.ino | 155 ++++++++++++++++++ .../lidar/lidar_data/lidar_data.ino | 0 3 files changed, 155 insertions(+) rename electrical_code/arms/{motor_angle_movement.ino => arm_servos/arm_servos.ino} (100%) create mode 100644 electrical_code/drive/movement/movement.ino create mode 100644 electrical_code/lidar/lidar_data/lidar_data.ino diff --git a/electrical_code/arms/motor_angle_movement.ino b/electrical_code/arms/arm_servos/arm_servos.ino similarity index 100% rename from electrical_code/arms/motor_angle_movement.ino rename to electrical_code/arms/arm_servos/arm_servos.ino diff --git a/electrical_code/drive/movement/movement.ino b/electrical_code/drive/movement/movement.ino new file mode 100644 index 0000000..575404a --- /dev/null +++ b/electrical_code/drive/movement/movement.ino @@ -0,0 +1,155 @@ +// Motor 1 (Left Front) +int motor1Pin1 = 6; +int motor1Pin2 = 7; + +// Motor 2 (Left Rear) +int motor2Pin1 = 4; +int motor2Pin2 = 5; + +// Motor 3 (Right Front) +int motor3Pin1 = 9; +int motor3Pin2 = 10; + +// Motor 4 (Right Rear) +int motor4Pin1 = 11; +int motor4Pin2 = 12; + +void goForward(int time_number_of_loop) { + // Set both left and right motors to move forward + digitalWrite(motor1Pin1, HIGH); + digitalWrite(motor1Pin2, LOW); + digitalWrite(motor2Pin1, HIGH); + digitalWrite(motor2Pin2, LOW); + digitalWrite(motor3Pin1, HIGH); + digitalWrite(motor3Pin2, LOW); + digitalWrite(motor4Pin1, HIGH); + digitalWrite(motor4Pin2, LOW); + delay(time_number_of_loop); +digitalWrite(motor1Pin1, LOW); + digitalWrite(motor1Pin2, LOW); + digitalWrite(motor2Pin1, LOW); + digitalWrite(motor2Pin2, LOW); + digitalWrite(motor3Pin1, LOW); + digitalWrite(motor3Pin2, LOW); + digitalWrite(motor4Pin1, LOW); + digitalWrite(motor4Pin2, LOW); +} + +void goBackward(int time_number_of_loop ) { + digitalWrite(motor1Pin1, LOW); + digitalWrite(motor1Pin2, HIGH); + digitalWrite(motor2Pin1, LOW); + digitalWrite(motor2Pin2, HIGH); + digitalWrite(motor3Pin1, LOW); + digitalWrite(motor3Pin2, HIGH); + digitalWrite(motor4Pin1, LOW); + digitalWrite(motor4Pin2, HIGH); + delay(time_number_of_loop); + digitalWrite(motor1Pin1, LOW); + digitalWrite(motor1Pin2, LOW); + digitalWrite(motor2Pin1, LOW); + digitalWrite(motor2Pin2, LOW); + digitalWrite(motor3Pin1, LOW); + digitalWrite(motor3Pin2, LOW); + digitalWrite(motor4Pin1, LOW); + digitalWrite(motor4Pin2, LOW); + +} + +void turnLeft(int time_number_of_loop) { + // Set left motors backward and right motors forward for turning left + digitalWrite(motor1Pin1, LOW); + digitalWrite(motor1Pin2, HIGH); + digitalWrite(motor2Pin1, LOW); + digitalWrite(motor2Pin2, HIGH); + + digitalWrite(motor3Pin1, HIGH); + digitalWrite(motor3Pin2, LOW); + digitalWrite(motor4Pin1, HIGH); + digitalWrite(motor4Pin2, LOW); + delay(time_number_of_loop); + digitalWrite(motor1Pin1, LOW); + digitalWrite(motor1Pin2, LOW); + digitalWrite(motor2Pin1, LOW); + digitalWrite(motor2Pin2, LOW); + digitalWrite(motor3Pin1, LOW); + digitalWrite(motor3Pin2, LOW); + digitalWrite(motor4Pin1, LOW); + digitalWrite(motor4Pin2, LOW); + +} + +void turnRight(int time_number_of_loop) { + // Set left motors forward and right motors backward for turning right + digitalWrite(motor1Pin1, HIGH); + digitalWrite(motor1Pin2, LOW); + digitalWrite(motor2Pin1, HIGH); + digitalWrite(motor2Pin2, LOW); + digitalWrite(motor3Pin1, LOW); + digitalWrite(motor3Pin2, HIGH); + digitalWrite(motor4Pin1, LOW); + digitalWrite(motor4Pin2, HIGH); + delay(time_number_of_loop); + digitalWrite(motor1Pin1, LOW); + digitalWrite(motor1Pin2, LOW); + digitalWrite(motor2Pin1, LOW); + digitalWrite(motor2Pin2, LOW); + digitalWrite(motor3Pin1, LOW); + digitalWrite(motor3Pin2, LOW); + digitalWrite(motor4Pin1, LOW); + digitalWrite(motor4Pin2, LOW); +} + +void setup() { + Serial.begin(9600); + // Initialize all motor pins as outputs + pinMode(motor1Pin1, OUTPUT); + pinMode(motor1Pin2, OUTPUT); + pinMode(motor2Pin1, OUTPUT); + pinMode(motor2Pin2, OUTPUT); + pinMode(motor3Pin1, OUTPUT); + pinMode(motor3Pin2, OUTPUT); + pinMode(motor4Pin1, OUTPUT); + pinMode(motor4Pin2, OUTPUT); +} +void loop(){ + // int x = 3000; + // goForward(x); + // goBackward(x); + // turnLeft(x); + // turnRight(x); + delay(1000); // Wait for 1 second + + // Receiving Data from Computer + if (Serial.available() > 0) { + // Read the incoming string until newline character + String inputString = Serial.readStringUntil('\n'); + + // Find the index of the space character + int spaceIndex = inputString.indexOf(' '); + + // Extract the command character and the number from the string + char command = inputString.substring(0, spaceIndex).charAt(0); + int value = inputString.substring(spaceIndex + 1).toInt(); + + if( command == 'w'){ + goForward(value); + + } + else if( command == 'd'){ + turnRight(value); + + } + else if( command == 'a'){ + turnLeft(value); + + } + else if( command == 's'){ + goBackward(value); + + } + + command = " "; + value = 0; +}} + diff --git a/electrical_code/lidar/lidar_data/lidar_data.ino b/electrical_code/lidar/lidar_data/lidar_data.ino new file mode 100644 index 0000000..e69de29 From 0ac797a8ddf0e45a68e2cd318751f6251aa2e7f6 Mon Sep 17 00:00:00 2001 From: mxhng Date: Thu, 28 Mar 2024 19:07:07 -0400 Subject: [PATCH 4/8] lidar code --- electrical_code/arduino_uno/arduino_uno.ino | 122 ++++++++++++++++++ .../lidar/lidar_data/lidar_data.ino | 118 +++++++++++++++++ 2 files changed, 240 insertions(+) create mode 100644 electrical_code/arduino_uno/arduino_uno.ino diff --git a/electrical_code/arduino_uno/arduino_uno.ino b/electrical_code/arduino_uno/arduino_uno.ino new file mode 100644 index 0000000..9bfc493 --- /dev/null +++ b/electrical_code/arduino_uno/arduino_uno.ino @@ -0,0 +1,122 @@ +/* +Arduino Uno handles Lidar data +*/ + +#include + +const int stepPin = 4; +const int enPin = 2; +const int dirPin = 3; +int totalSteps = 0; +float currentAngle = 0; +const int STEPS = 1600; // resolution || 1600 steps per rev (on motor driver) +const float angle_per_step = 360.0 / float(STEPS); + +const int HEADER = 0x59; +// Variables +int TF01_pix; +int dist, strength; +int a, b, c, d, e, f, check, i; + +const byte rxPin = 10; +const byte txPin = 11; +SoftwareSerial mySerial (rxPin, txPin); + +void setup() { + pinMode(stepPin, OUTPUT); + pinMode(dirPin, OUTPUT); + pinMode(enPin, OUTPUT); + digitalWrite(enPin, LOW); + digitalWrite(dirPin, LOW); // default counter clockwise + + Serial.begin(115200); + Serial.print("serial started"); + mySerial.begin(115200); + setAngle(0); +} + +void loop() { + // rotate a step + rotate(1); + + // get distance + Serial.println("dist : " + String(getDist())); + + // serial print angle + if (currentAngle >= 360.0) { + currentAngle = currentAngle - 360.0; + } + + Serial.println("angle : " + String(currentAngle)); +} + +int getDist(){ + delay(1); + if (mySerial.available() >= 9) + { + // Check for first header byte + if(mySerial.read() == HEADER) + { + // Check for second header byte + if(mySerial.read() == HEADER) + { + // Read all 6 data bytes + a = mySerial.read(); + b = mySerial.read(); + c = mySerial.read(); + d = mySerial.read(); + e = mySerial.read(); + f = mySerial.read(); + + // Read checksum byte + check = (a + b + c + d + e + f + HEADER + HEADER); + // Compare lower 8 bytes of checksum + if(mySerial.read() == (check & 0xff)) + { + // Calculate distance + dist = (a + (b * 256)); + // return result + return dist; + } + } + } + } + else{return 0;} +} + +// 1600 is one full rotation +void setAngle(int angleDegrees) { + currentAngle = totalSteps/STEPS *360; + + int moveSteps = (currentAngle - angleDegrees)*STEPS/360; + if(moveSteps > 0) { + // clockwise + digitalWrite(dirPin, LOW); + } + else { + digitalWrite(dirPin, HIGH); + } + for(int x = 0; x < abs(moveSteps); x++) { + digitalWrite(stepPin, HIGH); + delayMicroseconds(500); + digitalWrite(stepPin, LOW); + delayMicroseconds(500); + } + totalSteps += moveSteps; +} + +void rotate(float moveSteps) { + // rotate by moveSteps + for (int x = 0; x < abs(moveSteps); x++) { + digitalWrite(stepPin, HIGH); + delayMicroseconds(500); + digitalWrite(stepPin, LOW); + delayMicroseconds(500); + } + + // increment total steps + // reference angle + totalSteps += int(moveSteps); + totalSteps %= STEPS; // capping @ # steps per rev + currentAngle += moveSteps * angle_per_step; +} \ No newline at end of file diff --git a/electrical_code/lidar/lidar_data/lidar_data.ino b/electrical_code/lidar/lidar_data/lidar_data.ino index e69de29..4ed9993 100644 --- a/electrical_code/lidar/lidar_data/lidar_data.ino +++ b/electrical_code/lidar/lidar_data/lidar_data.ino @@ -0,0 +1,118 @@ +#include + +const int stepPin = 4; +const int enPin = 2; +const int dirPin = 3; +int totalSteps = 0; +float currentAngle = 0; +const int STEPS = 1600; // resolution || 1600 steps per rev (on motor driver) +const float angle_per_step = 360.0 / float(STEPS); + +const int HEADER = 0x59; +// Variables +int TF01_pix; +int dist, strength; +int a, b, c, d, e, f, check, i; + +const byte rxPin = 10; +const byte txPin = 11; +SoftwareSerial mySerial (rxPin, txPin); + +void setup() { + pinMode(stepPin, OUTPUT); + pinMode(dirPin, OUTPUT); + pinMode(enPin, OUTPUT); + digitalWrite(enPin, LOW); + digitalWrite(dirPin, LOW); // default counter clockwise + + Serial.begin(115200); + Serial.print("serial started"); + mySerial.begin(115200); + setAngle(0); +} + +void loop() { + // rotate a step + rotate(1); + + // get distance + Serial.println("dist : " + String(getDist())); + + // serial print angle + if (currentAngle >= 360.0) { + currentAngle = currentAngle - 360.0; + } + + Serial.println("angle : " + String(currentAngle)); +} + +int getDist(){ + delay(1); + if (mySerial.available() >= 9) + { + // Check for first header byte + if(mySerial.read() == HEADER) + { + // Check for second header byte + if(mySerial.read() == HEADER) + { + // Read all 6 data bytes + a = mySerial.read(); + b = mySerial.read(); + c = mySerial.read(); + d = mySerial.read(); + e = mySerial.read(); + f = mySerial.read(); + + // Read checksum byte + check = (a + b + c + d + e + f + HEADER + HEADER); + // Compare lower 8 bytes of checksum + if(mySerial.read() == (check & 0xff)) + { + // Calculate distance + dist = (a + (b * 256)); + // return result + return dist; + } + } + } + } + else{return 0;} +} + +// 1600 is one full rotation +void setAngle(int angleDegrees) { + currentAngle = totalSteps/STEPS *360; + + int moveSteps = (currentAngle - angleDegrees)*STEPS/360; + if(moveSteps > 0) { + // clockwise + digitalWrite(dirPin, LOW); + } + else { + digitalWrite(dirPin, HIGH); + } + for(int x = 0; x < abs(moveSteps); x++) { + digitalWrite(stepPin, HIGH); + delayMicroseconds(500); + digitalWrite(stepPin, LOW); + delayMicroseconds(500); + } + totalSteps += moveSteps; +} + +void rotate(float moveSteps) { + // rotate by moveSteps + for (int x = 0; x < abs(moveSteps); x++) { + digitalWrite(stepPin, HIGH); + delayMicroseconds(500); + digitalWrite(stepPin, LOW); + delayMicroseconds(500); + } + + // increment total steps + // reference angle + totalSteps += int(moveSteps); + totalSteps %= STEPS; // capping @ # steps per rev + currentAngle += moveSteps * angle_per_step; +} \ No newline at end of file From 5a4e147e6c1e2c068763533e1c89d08012ce2be1 Mon Sep 17 00:00:00 2001 From: tmei005 Date: Thu, 28 Mar 2024 20:08:57 -0400 Subject: [PATCH 5/8] blink led using interrupt --- .../sketch_mar28a/interrupt_test.ino | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 electrical_code/interrupt_test/sketch_mar28a/interrupt_test.ino diff --git a/electrical_code/interrupt_test/sketch_mar28a/interrupt_test.ino b/electrical_code/interrupt_test/sketch_mar28a/interrupt_test.ino new file mode 100644 index 0000000..2628e99 --- /dev/null +++ b/electrical_code/interrupt_test/sketch_mar28a/interrupt_test.ino @@ -0,0 +1,27 @@ +const int inputd3 = 3; // Define pin D3 as input +const int outputd7 = 7; // Define pin D7 as output +const int inputa1 = A1; // Define analog pin A7 as input + +volatile bool d3State = LOW; // Variable to store the state of pin D3 + +void setup() { + pinMode(outputd7, OUTPUT); // Set pin D7 as output + pinMode(inputd3, INPUT); // Set pin D3 as input + pinMode(inputa1, OUTPUT); // Set pin A7 as input + attachInterrupt(digitalPinToInterrupt(inputd3), toggleA7, CHANGE); // Attach interrupt to pin D3 +} + +void loop() { + // d7 toggle from high to low every 2 seconds + digitalWrite(outputd7, HIGH); // Set pin D7 HIGH + delay(2000); // Wait for 2 seconds + digitalWrite(outputd7, LOW); // Set pin D7 LOW + delay(2000); // Wait for 2 seconds +} + +void toggleA7() { + + digitalWrite(inputa1, !digitalRead(inputa1)); // Toggle pin A7 state + +} + From 136988a1f41643d3cb5d45f1f581dac52c393055 Mon Sep 17 00:00:00 2001 From: tmei005 Date: Thu, 28 Mar 2024 20:11:40 -0400 Subject: [PATCH 6/8] lan anh code --- .../movement_2motors/movement_2motors.ino | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 electrical_code/drive/movement_2motors/movement_2motors.ino diff --git a/electrical_code/drive/movement_2motors/movement_2motors.ino b/electrical_code/drive/movement_2motors/movement_2motors.ino new file mode 100644 index 0000000..d193427 --- /dev/null +++ b/electrical_code/drive/movement_2motors/movement_2motors.ino @@ -0,0 +1,102 @@ +// Motor 1 (Left Front) +int motor1Pin1 = 9; +int motor1Pin2 = 10; + +// Motor 2 (Left Rear) +int motor2Pin1 = 11; +int motor2Pin2 = 12; + +int angle; +int dist; +String inputString; + + + +void goForward(int time_number_of_loop) { + // Set both left and right motors to move forward + digitalWrite(motor1Pin1, HIGH); + digitalWrite(motor1Pin2, LOW); + digitalWrite(motor2Pin1, HIGH); + digitalWrite(motor2Pin2, LOW); + + delay(time_number_of_loop); + digitalWrite(motor1Pin1, LOW); + digitalWrite(motor1Pin2, LOW); + digitalWrite(motor2Pin1, LOW); + digitalWrite(motor2Pin2, LOW); + +} + +void goBackward(int time_number_of_loop ) { + digitalWrite(motor1Pin1, LOW); + digitalWrite(motor1Pin2, HIGH); + digitalWrite(motor2Pin1, LOW); + digitalWrite(motor2Pin2, HIGH); + + delay(time_number_of_loop); + digitalWrite(motor1Pin1, LOW); + digitalWrite(motor1Pin2, LOW); + digitalWrite(motor2Pin1, LOW); + digitalWrite(motor2Pin2, LOW); + +} + +void turnLeft(int time_number_of_loop) { + // Set left motors backward and right motors forward for turning left + digitalWrite(motor1Pin1, LOW); + digitalWrite(motor1Pin2, HIGH); + digitalWrite(motor2Pin1, LOW); + digitalWrite(motor2Pin2, HIGH); + + delay(time_number_of_loop); + digitalWrite(motor1Pin1, LOW); + digitalWrite(motor1Pin2, LOW); + digitalWrite(motor2Pin1, LOW); + digitalWrite(motor2Pin2, LOW); + +} + +void turnRight(int time_number_of_loop) { + // Set left motors forward and right motors backward for turning right + digitalWrite(motor1Pin1, HIGH); + digitalWrite(motor1Pin2, LOW); + digitalWrite(motor2Pin1, HIGH); + digitalWrite(motor2Pin2, LOW); + + delay(time_number_of_loop); + digitalWrite(motor1Pin1, LOW); + digitalWrite(motor1Pin2, LOW); + digitalWrite(motor2Pin1, LOW); + digitalWrite(motor2Pin2, LOW); + +} + +void setup() { + Serial.begin(115200); + // Initialize all motor pins as outputs + pinMode(motor1Pin1, OUTPUT); + pinMode(motor1Pin2, OUTPUT); + pinMode(motor2Pin1, OUTPUT); + pinMode(motor2Pin2, OUTPUT); + attachInterrupt(digitalPinToInterrupt(inputd3), toggleA7, CHANGE); // Attach interrupt to pin D3 + +} + +void loop() { + while (!Serial.available()); + + // Receiving Data from Computer + if (Serial.available() > 0) { + String inputString = Serial.readStringUntil('\n'); + } + // 3 characters - angle + // 3 characters - distance + angle = inputString.substring(0, 3).toInt(); + dist = inputString.substring(3, 7).toInt(); + + turnRight(angle * 100); + goForward(dist * 100); + +} + + From c6e9456a7af0b169963c342a69724cc1c7aa8fc1 Mon Sep 17 00:00:00 2001 From: mxhng Date: Tue, 9 Apr 2024 19:00:19 -0400 Subject: [PATCH 7/8] restructure + fixed 2 motor drive error --- electrical_code/arduino_nano/arduino_nano.ino | 100 ++++++++++++++++ .../motor_driver_test/motor_driver_test.ino | 86 ++++++++++++++ .../movement_2motors/movement_2motors.ino | 111 +++++++++--------- .../{sketch_mar28a => }/interrupt_test.ino | 0 4 files changed, 243 insertions(+), 54 deletions(-) create mode 100644 electrical_code/arduino_nano/arduino_nano.ino create mode 100644 electrical_code/drive/motor_driver_test/motor_driver_test.ino rename electrical_code/interrupt_test/{sketch_mar28a => }/interrupt_test.ino (100%) diff --git a/electrical_code/arduino_nano/arduino_nano.ino b/electrical_code/arduino_nano/arduino_nano.ino new file mode 100644 index 0000000..5a5e932 --- /dev/null +++ b/electrical_code/arduino_nano/arduino_nano.ino @@ -0,0 +1,100 @@ +// Motor 1 - Left +int leftMotor1 = 8; +int leftMotor2 = 7; + +// Motor 2 - Right +int rightMotor1 = 6; +int rightMotor2 = 5; + +int angle; +int dist; +String inputString; + +// forward = pin 1 high + +void goForward(int delay_time) { + // Set both left and right motors to move forward + digitalWrite(leftMotor1, HIGH); + digitalWrite(leftMotor2, LOW); + digitalWrite(rightMotor1, HIGH); + digitalWrite(rightMotor2, LOW); + + delay(delay_time); + digitalWrite(leftMotor1, LOW); + digitalWrite(leftMotor2, LOW); + digitalWrite(rightMotor1, LOW); + digitalWrite(rightMotor2, LOW); + +} + +// backward = pin 2 high +void goBackward(int delay_time ) { + digitalWrite(leftMotor1, LOW); + digitalWrite(leftMotor2, HIGH); + digitalWrite(rightMotor1, LOW); + digitalWrite(rightMotor2, HIGH); + + delay(delay_time); + digitalWrite(leftMotor1, LOW); + digitalWrite(leftMotor2, LOW); + digitalWrite(rightMotor1, LOW); + digitalWrite(rightMotor2, LOW); + +} + +void turnLeft(int delay_time) { + // Set left motor backward and right motor forward for turning left + digitalWrite(leftMotor1, LOW); + digitalWrite(leftMotor2, HIGH); + digitalWrite(rightMotor1, HIGH); + digitalWrite(rightMotor2, LOW); + + delay(delay_time); + digitalWrite(leftMotor1, LOW); + digitalWrite(leftMotor2, LOW); + digitalWrite(rightMotor1, LOW); + digitalWrite(rightMotor2, LOW); + +} + +void turnRight(int delay_time) { + // Set left motor forward and right motor backward for turning right + digitalWrite(leftMotor1, HIGH); + digitalWrite(leftMotor2, LOW); + digitalWrite(rightMotor1, LOW); + digitalWrite(rightMotor2, HIGH); + + delay(delay_time); + digitalWrite(leftMotor1, LOW); + digitalWrite(leftMotor2, LOW); + digitalWrite(rightMotor1, LOW); + digitalWrite(rightMotor2, LOW); + +} + +void setup() { + Serial.begin(115200); + // Initialize all motor pins as outputs + pinMode(leftMotor1, OUTPUT); + pinMode(leftMotor2, OUTPUT); + pinMode(rightMotor1, OUTPUT); + pinMode(rightMotor2, OUTPUT); + +} + +void loop() { + while (!Serial.available()); + + // Receiving Data from Computer + if (Serial.available() > 0) { + inputString = Serial.readStringUntil('\n'); + } + // 3 characters - angle + // 3 characters - distance + angle = inputString.substring(0, 3).toInt(); + dist = inputString.substring(3, 7).toInt(); + + turnRight(angle * 100); + goForward(dist * 100); + +} \ No newline at end of file diff --git a/electrical_code/drive/motor_driver_test/motor_driver_test.ino b/electrical_code/drive/motor_driver_test/motor_driver_test.ino new file mode 100644 index 0000000..d675434 --- /dev/null +++ b/electrical_code/drive/motor_driver_test/motor_driver_test.ino @@ -0,0 +1,86 @@ +// Motor 1 - Left +int leftMotor1 = 8; +int leftMotor2 = 7; + +// Motor 2 - Right +int rightMotor1 = 6; +int rightMotor2 = 5; + +void goForward(int delay_time) { + // Set both left and right motors to move forward + digitalWrite(leftMotor1, HIGH); + digitalWrite(leftMotor2, LOW); + digitalWrite(rightMotor1, HIGH); + digitalWrite(rightMotor2, LOW); + + delay(delay_time); + digitalWrite(leftMotor1, LOW); + digitalWrite(leftMotor2, LOW); + digitalWrite(rightMotor1, LOW); + digitalWrite(rightMotor2, LOW); + +} + +// backward = pin 2 high +void goBackward(int delay_time ) { + digitalWrite(leftMotor1, LOW); + digitalWrite(leftMotor2, HIGH); + digitalWrite(rightMotor1, LOW); + digitalWrite(rightMotor2, HIGH); + + delay(delay_time); + digitalWrite(leftMotor1, LOW); + digitalWrite(leftMotor2, LOW); + digitalWrite(rightMotor1, LOW); + digitalWrite(rightMotor2, LOW); + +} + +void turnLeft(int delay_time) { + // Set left motor backward and right motor forward for turning left + digitalWrite(leftMotor1, LOW); + digitalWrite(leftMotor2, HIGH); + digitalWrite(rightMotor1, HIGH); + digitalWrite(rightMotor2, LOW); + + delay(delay_time); + digitalWrite(leftMotor1, LOW); + digitalWrite(leftMotor2, LOW); + digitalWrite(rightMotor1, LOW); + digitalWrite(rightMotor2, LOW); + +} + +void turnRight(int delay_time) { + // Set left motor forward and right motor backward for turning right + digitalWrite(leftMotor1, HIGH); + digitalWrite(leftMotor2, LOW); + digitalWrite(rightMotor1, LOW); + digitalWrite(rightMotor2, HIGH); + + delay(delay_time); + digitalWrite(leftMotor1, LOW); + digitalWrite(leftMotor2, LOW); + digitalWrite(rightMotor1, LOW); + digitalWrite(rightMotor2, LOW); + +} + +void setup() { + Serial.begin(115200); + // Initialize all motor pins as outputs + pinMode(leftMotor1, OUTPUT); + pinMode(leftMotor2, OUTPUT); + pinMode(rightMotor1, OUTPUT); + pinMode(rightMotor2, OUTPUT); + //attachInterrupt(digitalPinToInterrupt(inputd3), toggleA7, CHANGE); // Attach interrupt to pin D3 + +} +void loop(){ + + goForward(1000); + delay(1000); // Wait for 1 second + goBackward(1000); + +} + diff --git a/electrical_code/drive/movement_2motors/movement_2motors.ino b/electrical_code/drive/movement_2motors/movement_2motors.ino index d193427..11e3767 100644 --- a/electrical_code/drive/movement_2motors/movement_2motors.ino +++ b/electrical_code/drive/movement_2motors/movement_2motors.ino @@ -1,10 +1,10 @@ -// Motor 1 (Left Front) -int motor1Pin1 = 9; -int motor1Pin2 = 10; +// Motor 1 - Left +int leftMotor1 = 8; +int leftMotor2 = 7; -// Motor 2 (Left Rear) -int motor2Pin1 = 11; -int motor2Pin2 = 12; +// Motor 2 - Right +int rightMotor1 = 6; +int rightMotor2 = 5; int angle; int dist; @@ -12,73 +12,76 @@ String inputString; -void goForward(int time_number_of_loop) { +// forward = pin 1 high + +void goForward(int delay_time) { // Set both left and right motors to move forward - digitalWrite(motor1Pin1, HIGH); - digitalWrite(motor1Pin2, LOW); - digitalWrite(motor2Pin1, HIGH); - digitalWrite(motor2Pin2, LOW); + digitalWrite(leftMotor1, HIGH); + digitalWrite(leftMotor2, LOW); + digitalWrite(rightMotor1, HIGH); + digitalWrite(rightMotor2, LOW); - delay(time_number_of_loop); - digitalWrite(motor1Pin1, LOW); - digitalWrite(motor1Pin2, LOW); - digitalWrite(motor2Pin1, LOW); - digitalWrite(motor2Pin2, LOW); + delay(delay_time); + digitalWrite(leftMotor1, LOW); + digitalWrite(leftMotor2, LOW); + digitalWrite(rightMotor1, LOW); + digitalWrite(rightMotor2, LOW); } -void goBackward(int time_number_of_loop ) { - digitalWrite(motor1Pin1, LOW); - digitalWrite(motor1Pin2, HIGH); - digitalWrite(motor2Pin1, LOW); - digitalWrite(motor2Pin2, HIGH); +// backward = pin 2 high +void goBackward(int delay_time ) { + digitalWrite(leftMotor1, LOW); + digitalWrite(leftMotor2, HIGH); + digitalWrite(rightMotor1, LOW); + digitalWrite(rightMotor2, HIGH); - delay(time_number_of_loop); - digitalWrite(motor1Pin1, LOW); - digitalWrite(motor1Pin2, LOW); - digitalWrite(motor2Pin1, LOW); - digitalWrite(motor2Pin2, LOW); + delay(delay_time); + digitalWrite(leftMotor1, LOW); + digitalWrite(leftMotor2, LOW); + digitalWrite(rightMotor1, LOW); + digitalWrite(rightMotor2, LOW); } -void turnLeft(int time_number_of_loop) { - // Set left motors backward and right motors forward for turning left - digitalWrite(motor1Pin1, LOW); - digitalWrite(motor1Pin2, HIGH); - digitalWrite(motor2Pin1, LOW); - digitalWrite(motor2Pin2, HIGH); +void turnLeft(int delay_time) { + // Set left motor backward and right motor forward for turning left + digitalWrite(leftMotor1, LOW); + digitalWrite(leftMotor2, HIGH); + digitalWrite(rightMotor1, HIGH); + digitalWrite(rightMotor2, LOW); - delay(time_number_of_loop); - digitalWrite(motor1Pin1, LOW); - digitalWrite(motor1Pin2, LOW); - digitalWrite(motor2Pin1, LOW); - digitalWrite(motor2Pin2, LOW); + delay(delay_time); + digitalWrite(leftMotor1, LOW); + digitalWrite(leftMotor2, LOW); + digitalWrite(rightMotor1, LOW); + digitalWrite(rightMotor2, LOW); } -void turnRight(int time_number_of_loop) { - // Set left motors forward and right motors backward for turning right - digitalWrite(motor1Pin1, HIGH); - digitalWrite(motor1Pin2, LOW); - digitalWrite(motor2Pin1, HIGH); - digitalWrite(motor2Pin2, LOW); +void turnRight(int delay_time) { + // Set left motor forward and right motor backward for turning right + digitalWrite(leftMotor1, HIGH); + digitalWrite(leftMotor2, LOW); + digitalWrite(rightMotor1, LOW); + digitalWrite(rightMotor2, HIGH); - delay(time_number_of_loop); - digitalWrite(motor1Pin1, LOW); - digitalWrite(motor1Pin2, LOW); - digitalWrite(motor2Pin1, LOW); - digitalWrite(motor2Pin2, LOW); + delay(delay_time); + digitalWrite(leftMotor1, LOW); + digitalWrite(leftMotor2, LOW); + digitalWrite(rightMotor1, LOW); + digitalWrite(rightMotor2, LOW); } void setup() { Serial.begin(115200); // Initialize all motor pins as outputs - pinMode(motor1Pin1, OUTPUT); - pinMode(motor1Pin2, OUTPUT); - pinMode(motor2Pin1, OUTPUT); - pinMode(motor2Pin2, OUTPUT); - attachInterrupt(digitalPinToInterrupt(inputd3), toggleA7, CHANGE); // Attach interrupt to pin D3 + pinMode(leftMotor1, OUTPUT); + pinMode(leftMotor2, OUTPUT); + pinMode(rightMotor1, OUTPUT); + pinMode(rightMotor2, OUTPUT); + //attachInterrupt(digitalPinToInterrupt(inputd3), toggleA7, CHANGE); // Attach interrupt to pin D3 } @@ -87,7 +90,7 @@ void loop() { // Receiving Data from Computer if (Serial.available() > 0) { - String inputString = Serial.readStringUntil('\n'); + inputString = Serial.readStringUntil('\n'); } // 3 characters - angle // 3 characters - distance diff --git a/electrical_code/interrupt_test/sketch_mar28a/interrupt_test.ino b/electrical_code/interrupt_test/interrupt_test.ino similarity index 100% rename from electrical_code/interrupt_test/sketch_mar28a/interrupt_test.ino rename to electrical_code/interrupt_test/interrupt_test.ino From 720903567ab980a4310aba7fa8a27788041c67d3 Mon Sep 17 00:00:00 2001 From: mxhng Date: Tue, 9 Apr 2024 19:15:53 -0400 Subject: [PATCH 8/8] serial script --- electrical_code/serialSend.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 electrical_code/serialSend.py diff --git a/electrical_code/serialSend.py b/electrical_code/serialSend.py new file mode 100644 index 0000000..71bb31d --- /dev/null +++ b/electrical_code/serialSend.py @@ -0,0 +1,28 @@ +import serial +import time +import sys + +arduino = serial.Serial(port='COM5', baudrate=115200, timeout=.1) +def write_read(x): + arduino.write(bytes(x, 'utf-8')) + time.sleep(0.05) + #data = arduino.readline() + #return data + +def move(angle, dist): + dist, angle = str(dist), str(angle) + angle = '0' * (3 - len(angle)) + angle + dist = '0' * (3 - len(dist)) + dist + command = angle + dist + write_read(command) + #print(command) + +while True: + move(100, 431) +''' + +while True: + command = sys.stdin.readline() # Taking input from user + comm1 = write_read(command) + print(command) # printing the value + ''' \ No newline at end of file