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/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/arms/arm_servos/arm_servos.ino b/electrical_code/arms/arm_servos/arm_servos.ino new file mode 100644 index 0000000..38bd94c --- /dev/null +++ b/electrical_code/arms/arm_servos/arm_servos.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; + } +} 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/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/drive/movement_2motors/movement_2motors.ino b/electrical_code/drive/movement_2motors/movement_2motors.ino new file mode 100644 index 0000000..11e3767 --- /dev/null +++ b/electrical_code/drive/movement_2motors/movement_2motors.ino @@ -0,0 +1,105 @@ +// 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); + //attachInterrupt(digitalPinToInterrupt(inputd3), toggleA7, CHANGE); // Attach interrupt to pin D3 + +} + +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); + +} + + diff --git a/electrical_code/interrupt_test/interrupt_test.ino b/electrical_code/interrupt_test/interrupt_test.ino new file mode 100644 index 0000000..2628e99 --- /dev/null +++ b/electrical_code/interrupt_test/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 + +} + 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..4ed9993 --- /dev/null +++ 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 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