8.9 KiB
Machine building week
For this week we needed to build a machine. We started brainstorming and came up with a few ideas.
Custom boards
Quick multiplexer breakout board design for rapid prototyping.
When I tried connecting it into my breadboard the power connectors snapped loose because the breadboard was too stiff. So I didn't test the board and went straight to a next prototype.
With this board I wanted to make a breakout for 5 I2C devices. So I could hook up 5 time of flight sensors for example. The first attempt I gave up. I also asked henk for some feedback on how to do the connectors. He recommended using UDPI connectors and making breakout boards for every ToF sensor so I could connect them properly.
So my schematic wen't to this.
make because of all the bridges I needed to add in
This is the most recent design for now. It was a pain to make all the bridges and swapping back and forth.
This is the design for the breakout board. We are using pin headers since we already have soldered ToF sensors
Chapter 2 Ultrasonic sensors
We didn't have enough ToF sensors for our robot. I asked Henk if I could order some for myself and use them in the project. But he told me no and to use ultrasonic sensors. And he told me to use the current board for that. But I said that wouldn't work because only one multiplexer pin goes to each breakout because I designed it for the ToF so now im going to do some solder hacking and try to wire it together myself.
??? Failure ```cpp int readUltrasonicSensor(int trig, int echo) { long duration, cm;
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
ioex.write(PCA95x5::Port::P06, PCA95x5::Level::L);
delayMicroseconds(5);
ioex.write(PCA95x5::Port::P06, PCA95x5::Level::H);
delayMicroseconds(10);
ioex.write(PCA95x5::Port::P06, PCA95x5::Level::L);
ioex.direction(PCA95x5::Port::P07, PCA95x5::Direction::IN);
duration = pulseIn(ioex.read(PCA95x5::Port::P07), HIGH);
// Convert the time into a distance
cm = (duration / 2) / 29.1; // Divide by 29.1 or multiply by 0.0343
Serial.print("Distance: ");
Serial.print(cm);
return (cm);
// https://randomnerdtutorials.com/complete-guide-for-ultrasonic-sensor-hc-sr04/
}
```
Here I tried measuring the signal myself but I didn't receive anything back from the multiplexer ??? Failure ```cpp // A0 trigger, A1 Echo int readUltrasonicSensor(int trig, int echo) {
long duration, cm;
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
ioex.write(PCA95x5::Port::P06, PCA95x5::Level::L);
delayMicroseconds(5);
ioex.write(PCA95x5::Port::P06, PCA95x5::Level::H);
delayMicroseconds(10);
long startTime = micros();
ioex.write(PCA95x5::Port::P06, PCA95x5::Level::L);
while (PCA95x5::Level::H == ioex.read(PCA95x5::Port::P07))
{
duration = micros() - startTime;
}
// Convert the time into a distance
cm = (duration / 2) / 29.1; // Divide by 29.1 or multiply by 0.0343
Serial.print("Distance: ");
Serial.print(cm);
return (cm);
// https://randomnerdtutorials.com/complete-guide-for-ultrasonic-sensor-hc-sr04/
}
```
Sort of works but it works super innacurate. I can move it around and it will display different values depending on how far it is from the table
For now I will plug the trigger into the multiplexer and the Echo pin into the arduino. Then I can measure the length of the signal to measure the distance.
Optimization
We had the issue that the arduino didn't run fast enough to read from the sensors and to control the motors simultaneously even when shrinking down the code to this. ??? Old code ```cpp #include <AccelStepper.h>
//=============================================================================================//
//Stepper motor pins
//=============================================================================================//
#define MOTOR_X_ENABLE_PIN 8
#define MOTOR_X_STEP_PIN 2
#define MOTOR_X_DIR_PIN 5
#define MOTOR_Y_ENABLE_PIN 8
#define MOTOR_Y_STEP_PIN 3
#define MOTOR_Y_DIR_PIN 6
#define MOTOR_Z_ENABLE_PIN 8
#define MOTOR_Z_STEP_PIN 4
#define MOTOR_Z_DIR_PIN 7
#define MOTOR_A_ENABLE_PIN 8
#define MOTOR_A_STEP_PIN 12
#define MOTOR_A_DIR_PIN 13
//=============================================================================================//
//Global object creations
//=============================================================================================//
AccelStepper motorX(1, MOTOR_X_STEP_PIN, MOTOR_X_DIR_PIN);
AccelStepper motorY(1, MOTOR_Y_STEP_PIN, MOTOR_Y_DIR_PIN);
AccelStepper motorZ(1, MOTOR_Z_STEP_PIN, MOTOR_Z_DIR_PIN);
AccelStepper motorA(1, MOTOR_A_STEP_PIN, MOTOR_A_DIR_PIN);
//=============================================================================================//
// Function declarations
//=============================================================================================//
void moveForw(int speed);
void moveBack(int speed);
void moveStop();
int readUltrasonicSensor(int trigPin, int echoPin);
void rotateLeft();
void stepperSetup();
int mm = 0;
void setup()
{
Serial.begin(9600);
stepperSetup();
pinMode(A2, INPUT);
}
void loop()
{
mm = readUltrasonicSensor(A1, A0) * 10; // Convert cm to mm
Serial.println(mm); // Print distance in mm
// Define distance thresholds
const int SAFE_DISTANCE_FRONT = 2500; // 500mm
const int WARNING_DISTANCE_FRONT = 1250; // 250mm
const int DANGER_DISTANCE_FRONT = 800; // 100mm
const int SAFE_DISTANCE_BACK = 400; // 40cm
const int DANGER_DISTANCE_BACK = 200; // 20cm
moveForw(0);
}
void moveForw(int stepDelay)
{
motorX.move(3000);
motorX.run();
motorY.move(3000);
motorY.run();
motorZ.move(3000);
motorZ.run();
motorA.move(3000);
motorA.run();
}
void moveBack(int speed)
{
motorX.move(-3000);
motorX.run();
motorY.move(-3000);
motorY.run();
motorZ.move(-3000);
motorZ.run();
motorA.move(-3000);
motorA.run();
}
void moveStop()
{
motorX.move(0);
motorX.run();
motorY.move(0);
motorY.run();
motorZ.move(0);
motorZ.run();
motorA.move(0);
motorA.run();
}
void rotateLeft()
{
motorX.move(3000);
motorX.run();
motorY.move(1500);
motorY.run();
motorZ.move(1500);
motorZ.run();
motorA.move(1500);
motorA.run();
}
// A0 trigger, A1 Echo
int readUltrasonicSensor(int trigPin, int echoPin) {
int duration, cm;
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// Convert the time into a distance
cm = (duration/2) / 29.1; // Divide by 29.1 or multiply by 0.0343
return cm;
}
void stepperSetup(){
pinMode(MOTOR_X_ENABLE_PIN, OUTPUT);
pinMode(MOTOR_Y_ENABLE_PIN, OUTPUT);
pinMode(MOTOR_Z_ENABLE_PIN, OUTPUT);
pinMode(MOTOR_A_ENABLE_PIN, OUTPUT);
motorX.setEnablePin(MOTOR_X_ENABLE_PIN);
motorX.setPinsInverted(false, false, true);
motorX.setAcceleration(100);
motorX.setMaxSpeed(100);
motorX.setSpeed(100);
motorX.enableOutputs();
motorY.setEnablePin(MOTOR_Y_ENABLE_PIN);
motorY.setPinsInverted(false, false, true);
motorY.setAcceleration(100);
motorY.setMaxSpeed(100);
motorY.setSpeed(100);
motorY.enableOutputs();
motorZ.setEnablePin(MOTOR_Z_ENABLE_PIN);
motorZ.setPinsInverted(false, false, true);
motorZ.setAcceleration(100);
motorZ.setMaxSpeed(100);
motorZ.setSpeed(100);
motorZ.enableOutputs();
motorA.setEnablePin(MOTOR_Z_ENABLE_PIN);
motorA.setPinsInverted(false, false, true);
motorA.setAcceleration(100);
motorA.setMaxSpeed(100);
motorA.setSpeed(100);
motorA.enableOutputs();
}
```
Issue: The motors didn't have enough power
Solution find the maximum current for the motors and the stepsticks in the datasheet.