diff --git a/docs/Assignments/week_12_machine_building/machine_building.md b/docs/Assignments/week_12_machine_building/machine_building.md index a0f74cd..59ecf32 100644 --- a/docs/Assignments/week_12_machine_building/machine_building.md +++ b/docs/Assignments/week_12_machine_building/machine_building.md @@ -307,196 +307,196 @@ We removed everything down to the ultrasonic sensor readings and only then the m So the main MCU would do all the thinking and readings from the sensors and the arduino will now act as a stepper motor controller. This is the code currently for the arduino. ??? Code - ```cpp - /* - This example shows how to take simple range measurements with the VL53L1X. The - range readings are in units of mm. - */ - #include + ```cpp + /* + This example shows how to take simple range measurements with the VL53L1X. The + range readings are in units of mm. + */ + #include - //=============================================================================================// - // Stepper motor pins - //=============================================================================================// + //=============================================================================================// + // Stepper motor pins + //=============================================================================================// - #define MOTOR_X_ENABLE_PIN 8 - #define MOTOR_X_STEP_PIN 2 - #define MOTOR_X_DIR_PIN 5 + #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_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_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 + #define MOTOR_A_ENABLE_PIN 8 + #define MOTOR_A_STEP_PIN 12 + #define MOTOR_A_DIR_PIN 13 - //=============================================================================================// - // Global object creations - //=============================================================================================// + //=============================================================================================// + // 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); + 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 - //=============================================================================================// + //=============================================================================================// + // Function declarations + //=============================================================================================// - void stepperSetup(); - void processCommand(String command); - String receivedMessage = ""; + void stepperSetup(); + void processCommand(String command); + String receivedMessage = ""; - void setup() - { - Serial.begin(115200); - stepperSetup(); - - } - - void loop() - { - // Process any available serial data - while (Serial.available()) + void setup() { - char incomingChar = Serial.read(); + Serial.begin(115200); + stepperSetup(); + + } - if (incomingChar == '\n') + void loop() + { + // Process any available serial data + while (Serial.available()) + { + char incomingChar = Serial.read(); + + if (incomingChar == '\n') + { + processCommand(receivedMessage); + Serial.println(receivedMessage); + receivedMessage = ""; + } + else + { + // Add the character to the message until the message is complete + receivedMessage += incomingChar; + } + } + + // Run the motors (non-blocking) + motorX.run(); + motorY.run(); + motorZ.run(); + motorA.run(); + } + + // Process incoming commands in format "motor#,speed" + void processCommand(String command) + { + // Find the comma position + int commaIndex = command.indexOf(','); + + // Make sure the command has a comma + if (commaIndex < 0) { - processCommand(receivedMessage); - Serial.println(receivedMessage); receivedMessage = ""; + return; } - else + + // Seperate the motor number and speed value + int motorNum = command.substring(0, commaIndex).toInt(); + int motorSpeed = command.substring(commaIndex + 1).toInt(); + + // number 1-4 for motors + switch (motorNum) { - // Add the character to the message until the message is complete - receivedMessage += incomingChar; + case 1: + motorX.setSpeed(motorSpeed); + if (motorSpeed == 0) + { + motorX.stop(); + } + else + { + motorX.move(motorSpeed * 100); // just keep moving until told otherwise and the * 100 is so it rotates the correct direction + motorX.setMaxSpeed(motorSpeed); + } + break; + + case 2: + motorY.setSpeed(motorSpeed); + if (motorSpeed == 0) + { + motorY.stop(); + } + else + { + motorY.move(motorSpeed * 100); + motorY.setMaxSpeed(motorSpeed); + } + break; + + case 3: + motorZ.setSpeed(motorSpeed); + if (motorSpeed == 0) + { + motorZ.stop(); + } + else + { + motorZ.move(motorSpeed * 100); + motorZ.setMaxSpeed(motorSpeed); + } + break; + + case 4: + motorA.setSpeed(motorSpeed); + if (motorSpeed == 0) + { + motorA.stop(); + } + else + { + motorA.move(motorSpeed * 100); + motorA.setMaxSpeed(motorSpeed); + } + break; + + default: + break; } } - // Run the motors (non-blocking) - motorX.run(); - motorY.run(); - motorZ.run(); - motorA.run(); - } - - // Process incoming commands in format "motor#,speed" - void processCommand(String command) - { - // Find the comma position - int commaIndex = command.indexOf(','); - - // Make sure the command has a comma - if (commaIndex < 0) + void stepperSetup() { - receivedMessage = ""; - return; + 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(800); + motorX.setMaxSpeed(800); + motorX.setSpeed(800); + motorX.enableOutputs(); + + motorY.setEnablePin(MOTOR_Y_ENABLE_PIN); + motorY.setPinsInverted(false, false, true); + motorY.setAcceleration(800); + motorY.setMaxSpeed(800); + motorY.setSpeed(800); + motorY.enableOutputs(); + + motorZ.setEnablePin(MOTOR_Z_ENABLE_PIN); + motorZ.setPinsInverted(false, false, true); + motorZ.setAcceleration(800); + motorZ.setMaxSpeed(800); + motorZ.setSpeed(800); + motorZ.enableOutputs(); + + motorA.setEnablePin(MOTOR_Z_ENABLE_PIN); + motorA.setPinsInverted(false, false, true); + motorA.setAcceleration(800); + motorA.setMaxSpeed(800); + motorA.setSpeed(800); + motorA.enableOutputs(); } - - // Seperate the motor number and speed value - int motorNum = command.substring(0, commaIndex).toInt(); - int motorSpeed = command.substring(commaIndex + 1).toInt(); - - // number 1-4 for motors - switch (motorNum) - { - case 1: - motorX.setSpeed(motorSpeed); - if (motorSpeed == 0) - { - motorX.stop(); - } - else - { - motorX.move(motorSpeed * 100); // just keep moving until told otherwise and the * 100 is so it rotates the correct direction - motorX.setMaxSpeed(motorSpeed); - } - break; - - case 2: - motorY.setSpeed(motorSpeed); - if (motorSpeed == 0) - { - motorY.stop(); - } - else - { - motorY.move(motorSpeed * 100); - motorY.setMaxSpeed(motorSpeed); - } - break; - - case 3: - motorZ.setSpeed(motorSpeed); - if (motorSpeed == 0) - { - motorZ.stop(); - } - else - { - motorZ.move(motorSpeed * 100); - motorZ.setMaxSpeed(motorSpeed); - } - break; - - case 4: - motorA.setSpeed(motorSpeed); - if (motorSpeed == 0) - { - motorA.stop(); - } - else - { - motorA.move(motorSpeed * 100); - motorA.setMaxSpeed(motorSpeed); - } - break; - - default: - break; - } - } - - 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(800); - motorX.setMaxSpeed(800); - motorX.setSpeed(800); - motorX.enableOutputs(); - - motorY.setEnablePin(MOTOR_Y_ENABLE_PIN); - motorY.setPinsInverted(false, false, true); - motorY.setAcceleration(800); - motorY.setMaxSpeed(800); - motorY.setSpeed(800); - motorY.enableOutputs(); - - motorZ.setEnablePin(MOTOR_Z_ENABLE_PIN); - motorZ.setPinsInverted(false, false, true); - motorZ.setAcceleration(800); - motorZ.setMaxSpeed(800); - motorZ.setSpeed(800); - motorZ.enableOutputs(); - - motorA.setEnablePin(MOTOR_Z_ENABLE_PIN); - motorA.setPinsInverted(false, false, true); - motorA.setAcceleration(800); - motorA.setMaxSpeed(800); - motorA.setSpeed(800); - motorA.enableOutputs(); - } - ``` + ``` So now I could send commands to the motors over Serial and see the motors spin in realtime. The command is structured like this. `1,500` Where 1 is the motor number and 500 is the speed of the motor. So like this I could control every motor separately. \ No newline at end of file