Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiger9406 committed Apr 9, 2024
2 parents f9eea8d + 7209035 commit 03ced92
Show file tree
Hide file tree
Showing 9 changed files with 776 additions and 0 deletions.
100 changes: 100 additions & 0 deletions electrical_code/arduino_nano/arduino_nano.ino
Original file line number Diff line number Diff line change
@@ -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);

}
122 changes: 122 additions & 0 deletions electrical_code/arduino_uno/arduino_uno.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
Arduino Uno handles Lidar data
*/

#include <SoftwareSerial.h>

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;
}
35 changes: 35 additions & 0 deletions electrical_code/arms/arm_servos/arm_servos.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <Servo.h>

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;
}
}
86 changes: 86 additions & 0 deletions electrical_code/drive/motor_driver_test/motor_driver_test.ino
Original file line number Diff line number Diff line change
@@ -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);

}

Loading

0 comments on commit 03ced92

Please sign in to comment.