Upgraded steps & removed end courses

This commit is contained in:
chakiral
2021-08-24 10:52:43 +02:00
parent 3035477b83
commit 0d14c0b1ef
2 changed files with 958 additions and 948 deletions

View File

@@ -1,178 +1,188 @@
// DRIVER : 4 MICROSTEPS FOR BENCH // DRIVER : 4 MICROSTEPS FOR BENCH
// include the library code: // include the library code:
#include "rgb_lcd.h" #include "rgb_lcd.h"
#include <Adafruit_NeoPixel.h> #include <Adafruit_NeoPixel.h>
// Joystick to motor integer // Joystick to motor integer
int SpeedDelay; int SpeedDelay;
int motorHigh = 190; int motorHigh = 190;
int motorLow = 900; int motorLow = 900;
int steps = 10; int steps = 10;
int farleft = 240; int farleft = 240;
int centerleft = 485; int centerleft = 485;
int farright = 775; int farright = 775;
int centerright = 525; int centerright = 525;
// --- GLOBAL STRUCTS --- // --- GLOBAL STRUCTS ---
// Represent a pin and associated state // Represent a pin and associated state
struct pin_state struct pin_state
{ {
int pin; int pin;
int state; int state;
}; };
// --- COMMANDS and INPUTS --- // --- COMMANDS and INPUTS ---
// Struct definition // Struct definition
// Struct containing all commands buttons, inputs and states // Struct containing all commands buttons, inputs and states
struct commands struct commands
{ {
// joystick position, x axis // joystick position, x axis
pin_state joystick_x; pin_state joystick_x;
// joystick position, y axis // joystick position, y axis
pin_state joystick_y; pin_state joystick_y;
}; };
// Create command struct // Create command struct
struct commands cmd = {{A1, 0}, {A0, 0}}; struct commands cmd = {{A1, 0}, {A0, 0}};
// --- MOTORS --- // --- MOTORS ---
// Structs definitions // Structs definitions
// Struct containing input pullups attached to motor // Struct containing input pullups attached to motor
struct pullups struct pullups
{ {
// home pullup // home pullup
pin_state home; pin_state home;
// end pullup // end pullup
pin_state end; pin_state end;
}; };
// Struct containing all pins and delay associated with a motor // Struct containing all pins and delay associated with a motor
struct motor struct motor
{ {
// pins // pins
int dir; int dir;
int step; int step;
int enable; int enable;
// delay // delay
int delay; int delay;
// pullups associated // pullups associated
pullups pups; pullups pups;
// in position or not (mainly used by burner motor) // in position or not (mainly used by burner motor)
bool in_pos; bool in_pos;
}; };
// Structs creation // Structs creation
// Create struct containing bench motor stuff // Create struct containing bench motor stuff
struct motor bench_motor = {33, 35, 31, motorHigh, {2, 0, 4, 0}, false}; struct motor bench_motor = {33, 35, 31, motorHigh, {2, 0, 4, 0}, false};
// Array containing signals instructions // Array containing signals instructions
// forward move // forward move
int forward[4] = {HIGH, HIGH, HIGH, LOW}; int forward[4] = {HIGH, HIGH, HIGH, LOW};
// backward move // backward move
int backward[4] = {LOW, HIGH, HIGH, LOW}; int backward[4] = {LOW, HIGH, HIGH, LOW};
// move_motor is used to move motor forward or backward // move_motor is used to move motor forward or backward
// arg motor: motor to activate is passed a parameter // arg motor: motor to activate is passed a parameter
// arg signals: array of int used to specify backward or forward mode // arg signals: array of int used to specify backward or forward mode
void move_motor(motor motor, int *signals) void move_motor(motor motor, int *signals)
{ {
digitalWrite(motor.dir, signals[0]); digitalWrite(motor.dir, signals[0]);
digitalWrite(motor.enable, signals[1]); digitalWrite(motor.enable, signals[1]);
digitalWrite(motor.step, signals[2]); digitalWrite(motor.step, signals[2]);
delayMicroseconds(motor.delay); delayMicroseconds(motor.delay);
digitalWrite(motor.step, signals[3]); digitalWrite(motor.step, signals[3]);
delayMicroseconds(motor.delay); delayMicroseconds(motor.delay);
} }
// home_motors is used to get all motors to home position // home_motors is used to get all motors to home position
void home_motors() // This function is disabled since no more end course
{
bench_motor.delay = motorHigh+30; void home_motors()
while (bench_motor.pups.home.state == LOW) {
{ // bench_motor.delay = motorHigh+30;
move_motor(bench_motor, forward); // Remove End course
bench_motor.pups.home.state = digitalRead(bench_motor.pups.home.pin); // while (bench_motor.pups.home.state == LOW)
} // {
refresh_motors_states(); // move_motor(bench_motor, forward);
} // bench_motor.pups.home.state = digitalRead(bench_motor.pups.home.pin);
// }
// refresh all motors pullups states refresh_motors_states();
void refresh_motors_states() }
{
bench_motor.pups.home.state = digitalRead(bench_motor.pups.home.pin); // refresh all motors pullups states
bench_motor.pups.end.state = digitalRead(bench_motor.pups.end.pin); // This function is disabled since no more end course
} void refresh_motors_states()
{
// move_using_joystick is used to move motors depending on joystick position // bench_motor.pups.home.state = digitalRead(bench_motor.pups.home.pin);
void move_using_joystick() // bench_motor.pups.end.state = digitalRead(bench_motor.pups.end.pin);
{ }
// fetch joystick position
cmd.joystick_x.state = analogRead(cmd.joystick_x.pin); // move_using_joystick is used to move motors depending on joystick position
cmd.joystick_y.state = analogRead(cmd.joystick_y.pin); void move_using_joystick()
// Check if Joystick button is pressed {
if (cmd.joystick_y.state > 1000 && bench_motor.pups.end.state == LOW) // fetch joystick position
{ cmd.joystick_x.state = analogRead(cmd.joystick_x.pin);
home_motors(); cmd.joystick_y.state = analogRead(cmd.joystick_y.pin);
} // Check if Joystick button is pressed - Remove end course
// if forward, go forward // if (cmd.joystick_y.state > 1000 && bench_motor.pups.end.state == LOW)
while (cmd.joystick_x.state > centerright && bench_motor.pups.end.state == LOW) if (cmd.joystick_y.state > 1000)
{ {
SpeedDelay = map(cmd.joystick_x.state , centerright, farright, motorLow, motorHigh); home_motors();
bench_motor.delay = SpeedDelay; }
for (int i = 0; i < steps; i++) // if forward, go forward - Remove end course
move_motor(bench_motor, forward); // while (cmd.joystick_x.state > centerright && bench_motor.pups.end.state == LOW)
bench_motor.pups.end.state = digitalRead(bench_motor.pups.end.pin); while (cmd.joystick_x.state > centerright)
cmd.joystick_x.state = analogRead(cmd.joystick_x.pin); {
} SpeedDelay = map(cmd.joystick_x.state , centerright, farright, motorLow, motorHigh);
bench_motor.delay = SpeedDelay;
// if backward, go backward for (int i = 0; i < steps; i++)
while (cmd.joystick_x.state < centerleft && bench_motor.pups.home.state == LOW) move_motor(bench_motor, forward);
{ // Remove End course
SpeedDelay = map(cmd.joystick_x.state , centerleft, farleft, motorLow, motorHigh); // bench_motor.pups.end.state = digitalRead(bench_motor.pups.end.pin);
bench_motor.delay = SpeedDelay; cmd.joystick_x.state = analogRead(cmd.joystick_x.pin);
for (int i = 0; i < steps; i++) }
move_motor(bench_motor, backward);
bench_motor.pups.home.state = digitalRead(bench_motor.pups.home.pin); // if backward, go backward - Remove end course
cmd.joystick_x.state = analogRead(cmd.joystick_x.pin); // while (cmd.joystick_x.state < centerleft && bench_motor.pups.home.state == LOW)
} while (cmd.joystick_x.state < centerleft)
} {
// setup_motors assign all pin to output and input for motors SpeedDelay = map(cmd.joystick_x.state , centerleft, farleft, motorLow, motorHigh);
void setup_motors() bench_motor.delay = SpeedDelay;
{ for (int i = 0; i < steps; i++)
// bench motor setup move_motor(bench_motor, backward);
pinMode(bench_motor.step, OUTPUT); // Remove end course
pinMode(bench_motor.dir, OUTPUT); // bench_motor.pups.home.state = digitalRead(bench_motor.pups.home.pin);
pinMode(bench_motor.enable, OUTPUT); cmd.joystick_x.state = analogRead(cmd.joystick_x.pin);
pinMode(bench_motor.pups.home.pin, INPUT_PULLUP); }
pinMode(bench_motor.pups.end.pin, INPUT_PULLUP); }
} // setup_motors assign all pin to output and input for motors
void setup_motors()
// -- Setup function, needed by Arduino {
void setup() // bench motor setup
{ pinMode(bench_motor.step, OUTPUT);
setup_motors(); pinMode(bench_motor.dir, OUTPUT);
} pinMode(bench_motor.enable, OUTPUT);
// pinMode(bench_motor.pups.home.pin, INPUT_PULLUP);
// --- MAIN --- // pinMode(bench_motor.pups.end.pin, INPUT_PULLUP);
}
// refresh_state is a wrapup off all refresh function
void refresh_state() // -- Setup function, needed by Arduino
{ void setup()
// motors states {
refresh_motors_states(); setup_motors();
}
}
// --- MAIN ---
// --- MAIN LOOP ---
void loop() // refresh_state is a wrapup off all refresh function
{ void refresh_state()
{
// refresh states from all inputs // motors states
refresh_state(); refresh_motors_states();
// move joystick if neeed }
move_using_joystick();
// --- MAIN LOOP ---
} void loop()
{
// refresh states from all inputs
// This function is disabled since no more end course
// refresh_state();
// move joystick if needed
move_using_joystick();
}

File diff suppressed because it is too large Load Diff