Unlimited CNC Robot Blog #2 - The making of the line following robot.
For the first part of my project, I created the line follower. I felt it would be best to make a working line following robot first, the 2nd part is to add the CNC.
I did have to change one part of my robot and that was switching from a spindle CNC to a laser CNC. I found with the spindle that the torque of when it starts turns the robot almost 180 degrees. The chassis and wheel motors aren't strong enough to handle the spindle. I can't afford upgrading to more appropriate hardware at this time. Using a laser still accomplishes what I envisioned.
Below is the code and pictures of my project so far:
/**Unlimited CNC Robot**/
/**set control port - DFRobot L298P**/
const int E1Pin = 10;
const int M1Pin = 12;
const int E2Pin = 11;
const int M2Pin = 13;
/**inner definition**/
typedef struct {
byte enPin;
byte directionPin;
} MotorContrl;
const int M1 = 0;
const int M2 = 1;
const int MotorNum = 2;
const MotorContrl MotorPin[] = { {E1Pin, M1Pin}, {E2Pin, M2Pin} } ;
const int Forward = LOW;
const int Backward = HIGH;
//Sensor Connection
const int left_sensor_pin =A0;
const int right_sensor_pin =A1;
const int laser_on_sensor_pin =A2;
const int laser_off_sensor_pin =A3;
int left_sensor_state;
int right_sensor_state;
int laser_on_sensor_state;
int laser_off_sensor_state;
int turn_delay = 10;
/**program**/
void setup() {
initMotor();
}
void loop() {
int value;
left_sensor_state = analogRead(left_sensor_pin);
right_sensor_state = analogRead(right_sensor_pin);
laser_on_sensor_state = analogRead(left_sensor_pin);
laser_off_sensor_state = analogRead(right_sensor_pin);
if(laser_on_sensor_state < 500)
{
Serial.println("Laser On");
setMotorSpeed( M1, 0 );
setMotorSpeed( M2, 0 );
/**Code to turn laser on**/
delay(200);
setMotorDirection( M1, Forward );
setMotorSpeed( M1, 100 );
setMotorDirection( M2, Forward );
setMotorSpeed( M2, 100 );
delay(500);
}
if(laser_off_sensor_state < 500)
{
Serial.println("Laser Off");
setMotorSpeed( M1, 0 );
setMotorSpeed( M2, 0 );
/**Code ot turn laser off**/
delay(200);
setMotorDirection( M1, Forward );
setMotorSpeed( M1, 100 );
setMotorDirection( M2, Forward );
setMotorSpeed( M2, 100 );
delay(500);
}
if(right_sensor_state > 500 && left_sensor_state < 500)
{
Serial.println("turning right");
setMotorDirection( M2, Forward );
setMotorSpeed( M2, 100 );
setMotorSpeed( M1, 0 );
delay(turn_delay);
}
if(right_sensor_state < 500 && left_sensor_state > 500)
{
Serial.println("turning left");
setMotorDirection( M1, Forward );
setMotorSpeed( M1, 100 );
setMotorSpeed( M2, 0 );
delay(turn_delay);
}
if(right_sensor_state > 500 && left_sensor_state > 500)
{
Serial.println("going forward");
setMotorDirection( M1, Forward );
setMotorSpeed( M1, 100 );
setMotorDirection( M2, Forward );
setMotorSpeed( M2, 100 );
delay(turn_delay);
}
if(right_sensor_state < 500 && left_sensor_state < 500)
{
Serial.println("stop");
setMotorSpeed( M1, 0 );
setMotorSpeed( M2, 0 );
}
}
/**functions**/
void initMotor( ) {
int i;
for ( i = 0; i < MotorNum; i++ ) {
digitalWrite(MotorPin[i].enPin, LOW);
pinMode(MotorPin[i].enPin, OUTPUT);
pinMode(MotorPin[i].directionPin, OUTPUT);
}
}
/** motorNumber: M1, M2
direction: Forward, Backward **/
void setMotorDirection( int motorNumber, int direction ) {
digitalWrite( MotorPin[motorNumber].directionPin, direction);
}
/** speed: 0-100 * */
inline void setMotorSpeed( int motorNumber, int speed ) {
analogWrite(MotorPin[motorNumber].enPin, 255.0 * (speed / 100.0) ); //PWM
}
Thank you for reading my blog!! Any comments or suggestions are welcome.
As a note, the kit supplied is very generous!! Thank you very much to our sponsor TE Connectivity and Element14!!
Dale Winhold