From a33c7a4da90a609064feb368a7e9097c7a9d8202 Mon Sep 17 00:00:00 2001 From: SudhakarKuma Date: Wed, 25 Oct 2017 15:59:35 +0530 Subject: Updated OSP-plugin --- Other Arduino Codes | 1 - Other_Arduino_Codes/Laser_PWM/Laser_PWM.ino | 19 + Other_Arduino_Codes/TwoNEMA17-2DM542Test/Readme | 12 + .../TwoNEMA17-2DM542Test/TwoNEMA17-2DM542Test.ino | 114 +++++ .../TwoNema17-2DM542-OSP-Plugin-Test/OSP.cpp | 495 +++++++++++++++++++++ .../TwoNema17-2DM542-OSP-Plugin-Test/OSP.h | 242 ++++++++++ .../TwoNema17-2DM542-OSP-Plugin-Test/Readme | 16 + .../TwoNema17-2DM542-OSP-Plugin-Test.ino | 276 ++++++++++++ .../TwoNema17-A4988-OSP-Plugin-Test/OSP.cpp | 491 ++++++++++++++++++++ .../TwoNema17-A4988-OSP-Plugin-Test/OSP.h | 242 ++++++++++ .../TwoNema17-A4988-OSP-Plugin-Test/Readme | 3 + .../TwoNema17-A4988-OSP-Plugin-Test.ino | 271 +++++++++++ 12 files changed, 2181 insertions(+), 1 deletion(-) delete mode 160000 Other Arduino Codes create mode 100644 Other_Arduino_Codes/Laser_PWM/Laser_PWM.ino create mode 100644 Other_Arduino_Codes/TwoNEMA17-2DM542Test/Readme create mode 100644 Other_Arduino_Codes/TwoNEMA17-2DM542Test/TwoNEMA17-2DM542Test.ino create mode 100644 Other_Arduino_Codes/TwoNema17-2DM542-OSP-Plugin-Test/OSP.cpp create mode 100644 Other_Arduino_Codes/TwoNema17-2DM542-OSP-Plugin-Test/OSP.h create mode 100644 Other_Arduino_Codes/TwoNema17-2DM542-OSP-Plugin-Test/Readme create mode 100644 Other_Arduino_Codes/TwoNema17-2DM542-OSP-Plugin-Test/TwoNema17-2DM542-OSP-Plugin-Test.ino create mode 100644 Other_Arduino_Codes/TwoNema17-A4988-OSP-Plugin-Test/OSP.cpp create mode 100644 Other_Arduino_Codes/TwoNema17-A4988-OSP-Plugin-Test/OSP.h create mode 100644 Other_Arduino_Codes/TwoNema17-A4988-OSP-Plugin-Test/Readme create mode 100644 Other_Arduino_Codes/TwoNema17-A4988-OSP-Plugin-Test/TwoNema17-A4988-OSP-Plugin-Test.ino diff --git a/Other Arduino Codes b/Other Arduino Codes deleted file mode 160000 index d81a906..0000000 --- a/Other Arduino Codes +++ /dev/null @@ -1 +0,0 @@ -Subproject commit d81a9066c2258cf1c354298fc09b616ae8e597fc diff --git a/Other_Arduino_Codes/Laser_PWM/Laser_PWM.ino b/Other_Arduino_Codes/Laser_PWM/Laser_PWM.ino new file mode 100644 index 0000000..08ba1dd --- /dev/null +++ b/Other_Arduino_Codes/Laser_PWM/Laser_PWM.ino @@ -0,0 +1,19 @@ +int pin=6; // Pin to which you connect the gate (G) of the MOSFET. + +void setup() { + // put your setup code here, to run once: +pinMode(pin,OUTPUT); +Serial.begin(9600); +} + +void loop() { + // put your main code here, to run repeatedly: +analogWrite(pin, 250); // High Intensity +delay(5000); +analogWrite(pin, 100); // Medium intensity +delay(5000); +analogWrite(pin, 50); // Low intensity +delay(5000); + +} + diff --git a/Other_Arduino_Codes/TwoNEMA17-2DM542Test/Readme b/Other_Arduino_Codes/TwoNEMA17-2DM542Test/Readme new file mode 100644 index 0000000..46a2a82 --- /dev/null +++ b/Other_Arduino_Codes/TwoNEMA17-2DM542Test/Readme @@ -0,0 +1,12 @@ +This code is meant to test NEMA 17 motors with 2DM542 (microstep driver). The connections are as under: + +1. The pins A+ A- B+ and B- are connected with the four pins of NEMA17. +2. V+ and GND are connected to 24V power supply terminals. GND is shorted with the GND of Arduino. +3. ENA- DIR- and PLS- are connected to GND of Arduino. +4. PLS+ --> D10 of Arduino. +5. DIR+ --> D11 of Arduino. +6. ENA+ --> D12 of Arduino. +7. RS232 pins were completely OFF. +8. The swicthes on the driver were adjusted for 3200 steps per revolution and 1A current supply. + +This code makes the two motors NEMA 17 run only once for 180 degrees (one after another). diff --git a/Other_Arduino_Codes/TwoNEMA17-2DM542Test/TwoNEMA17-2DM542Test.ino b/Other_Arduino_Codes/TwoNEMA17-2DM542Test/TwoNEMA17-2DM542Test.ino new file mode 100644 index 0000000..3f0af63 --- /dev/null +++ b/Other_Arduino_Codes/TwoNEMA17-2DM542Test/TwoNEMA17-2DM542Test.ino @@ -0,0 +1,114 @@ +int stepPin = 12; +int dirPin = 11; +int enblPin = 10; + +int LASER = 3; + +int stepPin1 = 9; +int dirPin1 = 8; +int enblPin1 = 7; +//int mosfet1=3; +//int mosfet2=5; +int delayy = 5; +int Relay1 = 2; +int Relay2 = 4; +int Relay3 = 5; +int Relay4 = 6; + +void setup() { + Serial.begin(9600); + //pinMode(mosfet1, OUTPUT); + //pinMode(mosfet2, OUTPUT); + //digitalWrite(mosfet1, LOW); + + //digitalWrite(mosfet2, LOW); +//delay(5000); + + //for(int i =0;i<255;i++){ + //analogWrite(mosfet1,i); + // analogWrite(mosfet2,50); + // delay(2000); + //} + //digitalWrite(mosfet1,HIGH); + //digitalWrite(mosfet2,HIGH); + + +pinMode(LASER, OUTPUT); +analogWrite(LASER, 100); + +pinMode(Relay1, OUTPUT); +pinMode(Relay2, OUTPUT); +pinMode(Relay3, OUTPUT); +pinMode(Relay4, OUTPUT); +pinMode(A1, OUTPUT); +digitalWrite(A1, LOW); +digitalWrite(Relay1, HIGH); +digitalWrite(Relay2, HIGH); +digitalWrite(Relay3, HIGH); +digitalWrite(Relay4, HIGH); +delay(2000); +digitalWrite(Relay1,LOW); +digitalWrite(Relay2,LOW); +digitalWrite(Relay3,LOW); +digitalWrite(Relay4,LOW); +digitalWrite(A1, HIGH); + + +pinMode (stepPin, OUTPUT); +pinMode (dirPin, OUTPUT); +pinMode (enblPin, OUTPUT); + +pinMode (stepPin1, OUTPUT); +pinMode (dirPin1, OUTPUT); +pinMode (enblPin1, OUTPUT); + +digitalWrite(stepPin, LOW); +digitalWrite(dirPin, LOW); +digitalWrite(enblPin, HIGH); + + +digitalWrite(stepPin1, LOW); +digitalWrite(dirPin1, LOW); +digitalWrite(enblPin1, HIGH); +delay(100); +digitalWrite(enblPin, LOW); +digitalWrite(enblPin1, LOW); + +} + +void loop() { +delay(2000); +digitalWrite(dirPin, HIGH); +for(int x=0; x<800; x++){ +digitalWrite(stepPin, HIGH); +delay(delayy); +digitalWrite(stepPin, LOW); +delay(delayy); +} +delay(2000); +digitalWrite(dirPin, LOW); +for(int x=0; x<800; x++){ +digitalWrite(stepPin, HIGH); +delay(delayy); +digitalWrite(stepPin, LOW); +delay(delayy); +} +delay(2000); +digitalWrite(dirPin1, HIGH); +for(int x=0; x<1300; x++){ +digitalWrite(stepPin1, HIGH); +delay(delayy); +digitalWrite(stepPin1, LOW); +delay(delayy); +} +delay(2000); +digitalWrite(dirPin1, LOW); +for(int x=0; x<1300; x++){ +digitalWrite(stepPin1, HIGH); +delay(delayy); +digitalWrite(stepPin1, LOW); +delay(delayy); +//exit(0); +} +} + diff --git a/Other_Arduino_Codes/TwoNema17-2DM542-OSP-Plugin-Test/OSP.cpp b/Other_Arduino_Codes/TwoNema17-2DM542-OSP-Plugin-Test/OSP.cpp new file mode 100644 index 0000000..48abd91 --- /dev/null +++ b/Other_Arduino_Codes/TwoNema17-2DM542-OSP-Plugin-Test/OSP.cpp @@ -0,0 +1,495 @@ +// #include "WProgram.h" // Arduino < 1.0 +#include //Arduino >= 1.0 +#include "OSP.h" + +//int steps; +int dir1, step1, dir2, step2; +float stepx; +float stepy; +//int low; +//int high; +int x_rev_set = 0; +int y_rev_set = 0; +float valx; +float valy; +float _pgrad_x; +float _pgrad_y; +//bool blind; +//int diff_x; +//int diff_y; +float stepx_new; +float stepy_new; +float degsH; +float degsV; +float degsH_new; +float degsV_new; +//int slow=50; +//int xcount; +//int ycount; + + +OSP::OSP(){ + stopper=0; +} + +void OSP::setMotorsPins(int step1, int dir1, int enable1, int step2, int dir2, int enable2, int ledPin, int ddir1, int ddir2, int motor1, int motor2, int sensor1, int sensor2) +{ + + pinMode(step1, OUTPUT); // pinMode(pulPin, OUTPUT); + pinMode(dir1, OUTPUT); // pinMode(dirPin, OUTPUT); + pinMode(enable1, OUTPUT); // pinMode(enblPin, OUTPUT); + pinMode(step2, OUTPUT); // pinMode(pulPin1, OUTPUT); + pinMode(dir2, OUTPUT);// pinMode(dirPin1, OUTPUT); + pinMode(enable2, OUTPUT); // pinMode(enblPin1, OUTPUT); + pinMode(ledPin, OUTPUT); // pinMode(ledPin, OUTPUT); + pinMode(sensor1, INPUT); // pinMode(sensor1, INPUT); + pinMode(sensor2, INPUT); // pinMode(sensor2, INPUT); + + digitalWrite(step1, LOW); // digitalWrite(pulPin, LOW); + digitalWrite(step2, LOW); // digitalWrite(pulPin1, LOW); + digitalWrite(ledPin, LOW); // digitalWrite(ledPin, LOW); + digitalWrite(enable1, LOW); // digitalWrite(enblPin, LOW); + digitalWrite(enable2, LOW); // digitalWrite(enblPin1, LOW); + digitalWrite(dir1, HIGH); // digitalWrite(dirPin, LOW); +digitalWrite(dir2, HIGH); // digitalWrite(dirPin1, LOW); + + + //Serial.begin(9600); + Serial.println("Setting up Motor pins..done"); + + digitalWrite(step1, HIGH); // digitalWrite(enblPin, HIGH); + delay(100); + digitalWrite(step1, LOW); // digitalWrite(enblPin, LOW); + digitalWrite(step2, HIGH); // digitalWrite(enblPin1, HIGH); + delay(100); + digitalWrite(step2, LOW); // digitalWrite(enblPin1, LOW); + + + motor1 = 10;//number of steps + motor2 = 20;//number of steps + + ddir1=1;//dir 1 for CW 0 for CCW + ddir2=1;//dir 1 for CW 0 for CCW + delay(1000); +} + + +//####################################### +void OSP::init() +{ + low = 0; + high = 3200;//Motor driver switch setting; + + _pgrad_x=(float)((float)(high-low)/360); + _pgrad_y=(float)((float)(high-low)/180); + + stepx=0; + stepy=0; + low = 0; +high = 0; +x_rev_set = 0; +y_rev_set = 0; +valx = 0; +valy = 0; +_pgrad_x = 0; +_pgrad_y = 0; +stepx_new = 0; +stepy_new = 0; +degsH = 0; +degsV = 0; +degsH_new = 0; +degsV_new = 0; + + //Serial.println(_pgrad_x); + } + +//#################################### + int OSP::getPX() //Steps per revolution on X axis + { + return x_rev_set = 3200;//Change this as per the switch setting on the driver + } +//####################################### + + int OSP::getPY() //Steps per revolution on Y axis + { + return y_rev_set = 3200;//Change this as per the switch setting on the driver + } + + //##################################### + bool OSP::movx(bool dir, int step1, int dir1) //Move on X axis // ovx(bool dir, int pulPin, int dirPin) + { + int bytes_recv = 0; + char comm[5]="nost"; + + comm[bytes_recv++] = Serial.read(); + //Serial.print("value of comm is "); + //Serial.println(comm); + while(strcmp(comm, "stop")!=0 || stopper) + { + //Serial.println("movx"); + //Serial.println(pulPin); + if(dir==1) + { + + if(stepx>=3200) + { + stepx=3200; + } + /*digitalWrite(dirPin, HIGH); + digitalWrite(pulPin, HIGH); + digitalWrite(pulPin, LOW); */ + else + { + digitalWrite(dir1, HIGH); + digitalWrite(step1, HIGH); + delay(10); + digitalWrite(step1, LOW); + delay(10); + + stepx++; + } + + //Serial.println(stepx); + + } + else + { + if(stepx<=0) + { + stepx = 0; + } + else { + /*digitalWrite(dirPin, LOW); + digitalWrite(pulPin, HIGH); + digitalWrite(pulPin, LOW);*/ + + digitalWrite(dir1, LOW); + digitalWrite(step1, HIGH); + delay(10); + digitalWrite(step1, LOW); + delay(10); + + stepx--; + + } + + //Serial.println(stepx); + } + float temp=stepx*360.0/3200.0; + Serial.println(temp); + //delay(50); + while(Serial.available() > 0) + comm[bytes_recv++] = Serial.read(); + //Serial.println(comm); + //Serial.println("inside second while"); + //Safeguard... + //Serial.println(stepx); + if(bytes_recv>4) + { + break; + + } + +} + + + return true; + } + + //##################################### + bool OSP::movy(bool dir, int step2, int dir2) //Move on Y axis + // bool OSP::movy(bool dir, int pulPin1, int dirPin1) + { + // Serial.print("stepx of axes "); Serial.println(stepx); + int bytes_recv = 0; + char comm[5]="nost"; + + comm[bytes_recv++] = Serial.read(); + //Serial.print("value of comm is "); + //Serial.println(comm); + while(strcmp(comm, "stop")!=0) + { + if(dir==0) + { + if(stepy>=3200/4) + { + stepy=3200/4; + } + else + { + digitalWrite(dir2, LOW); // digitalWrite(dirPin1, HIGH); + digitalWrite(step2, HIGH); // digitalWrite(pulPin1, HIGH); + delay(10); + digitalWrite(step2, LOW); // digitalWrite(pulPin1, LOW); + delay(10); + stepy++; + } + //Serial.println(stepy); + + } + else + { + if(stepy<=0) + { + stepy=0; + //Serial.println("stop"); + + } + else + { + digitalWrite(dir2, HIGH); // digitalWrite(dirPin1, LOW); + digitalWrite(step2, HIGH); // digitalWrite(pulPin1, HIGH); + delay(10); + digitalWrite(step2, LOW); // digitalWrite(pulPin1, LOW); + delay(10); + stepy--; + } + //Serial.println(stepy); + + } + float temp=stepy*360.0/3200.0; + Serial.println(temp); + + + //delay(50); + while(Serial.available() > 0) + comm[bytes_recv++] = Serial.read(); + //Serial.println(comm); + //Serial.println("inside second while"); + //Safeguard... + //Serial.println(stepy); + if(bytes_recv>4) + { + break; + // + } + +} + + + return true; + } + + //560 to 2420 << 590 2190 +//################################# + +float OSP::getX() +{ + /** + * Returns current position on X axis + * + * \return X position as radians to stellarium + */ + + //Serial.println(stepx); + float valx; + //valx = map(stepx, 0, 3200, 0, 360); + valx = stepx*(float)(360.00/3200.00); + valx = (float)(M_PI/180)*valx; + //vlax = _deg2rad(valx); + + return valx; +} + +float OSP::getY() +{ + /** + * Returns current position on Y axis + * + * \return Y position as radians to stellarium + */ + //Serial.println(stepy); + + float valy; + + valy = stepy*(float)(360.00/3200.00); +// valy = map(stepy, 0, 3200/2, 0, 180); + valy = (float)(M_PI/180)*valy; +// //vlay=_deg2rad(valy); + + return valy; +} + +int OSP::getPx() +{ + /** + * Return the number of steps per revolution of the Y axis + * + * \return Steps per revolution + */ + + return stepx; + +} + +int OSP::getPy() +{ + return stepy; +} + + +float OSP::_rad2deg(float rad){ +// return lrint( (float) (180.00/M_PI)*rad); + return (float) (180.00/M_PI)*rad; + +} + +float OSP::_deg2rad(float deg){ + return (float) (M_PI/180.00)*deg; +} + +/* +void AxesLib::goToRads(float rx, float ry){ + float degsH = (360.0 - _rad2deg(rx)); + if(degsH >= 360.0) + degsH = degsH - 360.0; + + _moveTo((float) degsH*_pgrad_x, (float) _rad2deg(ry)*_pgrad_y); +}*/ + +//################################################ + +//###################################### +void OSP::goToRads(float x_rad, float y_rad)//give this input for debugging> movem_-4.5434_-2.5254 +{ + + float degsH = _rad2deg(x_rad); + float degsV = _rad2deg(y_rad); + +//if((degsH_new==degsH) +//if(degsV_new==degsV)) +//if((degsH_new>degsH) +//{ +// } +//if(degsV_new>degsV)) +//if((degsH_newdegsH)||(degsV_new>degsV)) +// +//{ +// degsH = degsH_new; +// degsV = degsV_new; + //Serial.println(degsH); + //Serial.println(degsV); +_moveTo(degsH,degsV); +// } + + //degsH = degsH+180; + //degsV = 180-degsV; + + +} +//################################################## +void OSP::_moveTo(float x, float y, char* method) +{ + //stepx_new = map(abs(x), 0, 360, low, high); + //stepy_new = map(abs(y), 0, 180, low, high); + + + stepx_new = (3200*x)/360;//map to counts + stepy_new = (3200*y)/360;//map to counts + +// if(((stepx-stepx_new)==0)||((stepy-stepy_new)==0)) +// { +// stepx=0; +// stepy=0; +// } +// else +// { +// stepx=stepx_new-stepx; +// stepy=stepy_new-stepy; + //} + +//if((stepx_new>stepx)||(stepy_new>stepy)) +//{ +// Serial.println("Forward"); +// stepx=stepx_new-stepx; +// stepy=stepy_new-stepy; +// } +// else +// { Serial.println("Reverse"); +// +// stepx=stepx-stepx_new; +// stepy=stepy-stepy_new; +// } + +stepx=stepx_new-stepx; +stepy=stepy_new-stepy; + + + //Serial.println(stepx); + //Serial.println(stepy); + + //_moveXY(stepx, stepy); + //Serial.println("done_move"); + + + //Serial.println("done_move"); + + + + //Serial.println(low); + //Serial.println(high); + //Serial.println(stepx_new); + //Serial.println(stepy_new); + + //_moveDDA(stepx_new, stepy_new); + _moveXY(stepx, stepy,true); + stepx=stepx_new; +stepy=stepy_new; +// //_moveEqua(stepx_new, stepy_new); +// stepx=stepx_new; +// stepy=stepy_new; +} +//################################################## +void OSP::_moveXY(float x, float y, bool nodelay) +{ + + + if(y>3200.00/4) y = 3200.00/4; // This is to limit the Tilt Motor to 90 degree + + for(float count=0;count + #include + + + /** + * \brief Class that manages movements and the laser of the device + * + * Uses the stepper motors and sensors to positioning the device at a given horizontal coordinates, + * within a range of 360º degrees in horizontal, and 180º on vertical. + * To the movements uses the DDA algorithm (Digital Differential Algorithm). + */ + class OSP{ + public: + + // int pulPin, + // int pulPin, dirPin, enblPin, pulPin1, dirPin1, enblPin1, ledPin, ddir1, ddir2, motor1, motor2, sensor1,sensor2, _stPin_x, _stPin_y, _dirPin, _enable_x, _enable_y; + +int step1, dir1, enable1, step2, dir2, enable2, ledPin, ddir1, ddir2, motor1, motor2, sensor1,sensor2, _stPin_x, _stPin_y, _dirPin, _enable_x, _enable_y; + + float stepx, stepy; + int low; + int high; + int stopper; + float degsH; + float degsV; + + /** + * Pins to control the stepper motors + */ + + /** + * Steps per degree on each axis + */ + float _pgrad_x, _pgrad_y; + + /** + * Current position of each axis (in radians) + */ + float _rx, _ry; + + /** + * Auxiliary variables, maximum theoric values, and steps per revolution respectively + */ + int _x, _y, _X, _Y, _pv_x, _pv_y; + + /** + * Theoric central values of each axis + */ + int _revx, _topy; + + /** + * Indicates if X axis has been "reverted" (Y is between 90º and 180º) + */ + bool _x_rev; + + /** + * Sensor pins + */ + int _s0Pin_x, _s360Pin_x, _sbottomPin_y, _stopPin_y; + + /** + * Transforms radian to degrees + * + * \param rad Radians + * \return degrees + */ + float _rad2deg(float rad); + + /** + * Degrees to radians + * + * \param deg Degrees + * \return radians + */ + float _deg2rad(float deg); + + /** + * Moves one of the motors a given number of steps + * + * \param axis Pin of the motor to move + * \param dir Direction: True means clockwise direction on X, and upwards on Y + * \param steps Number of steps (if limit sensor is not reached) + * \param sensor Pin of the sensor that can be reached towards that direction + * \param nodelay Skip the initial delay (useful to DDA algorithm) + * \return Number of steps (distinct of the steps parameter if the sensor has been reached) + */ + //int _step(int axis, bool dir, int steps, int sensor, bool nodelay=false); + + /** + * Enables the motors power supply + */ + void _enableMotors(); + + /** + * Disables the motors power supply + */ + void _disableMotors(); + + /** + * Moves the device to the given position + * + * \param x Number of steps from 0 to the desired position on X axis + * \param y Number of steps from 0 to the desired position on Y axis + * \param method Algorithm selection: DDA or XY (first X axis, then Y), by default is DDA + */ + void _moveTo(float x, float y, char* method = "DDA"); + + /** + * Moves the device to the given position, first X axis and then Y axis + * + * \param x Number of steps from 0 to the desired position on X axis + * \param y Number of steps from 0 to the desired position on Y axis + * \param nodelay Omits the delay on changes of axis or direction + */ + void _moveXY(float x, float y, bool nodelay=false); + + /** + * Moves the device to the given position using DDA algorithm + * + * \param x Number of steps from 0 to the desired position on X axis + * \param y Number of steps from 0 to the desired position on Y axis + */ + void _moveDDA(int x, int y); + //void _moveDDA(int x, int y, float dirx, float diry); + + public: + /** + * Class constructor + */ + OSP(); + + /** + * Initializes the device + * + * Along the process obtains the number of steps on each axis, and calculates the steps + * per degree for positioning + */ + void init(); + + /** + * Returns current position on X axis + * + * \return X position as radians + */ + float getX(); + + /** + * Returns current position on Y axis + * + * \return Y position as radians + */ + float getY(); + + /** + * Return the number of steps per revolution of the X axis + * + * \return Steps per revolution + */ + int getPX(); + + /** + * Number of steps from 0º to current position on X axis + * + * \return Current position on X + */ + int getPx(); + + /** + * Return the number of steps per revolution of the Y axis + * + * \return Steps per revolution + */ + int getPY(); + + /** + * Number of steps from 0º to current position on Y axis + * + * \return Current position on Y + */ + int getPy(); + + /** + * Sets the pins to control the stepper motors + * + * \param stPin_x Pin to move the X axis + * \param stPin_y Pin to move the Y axis + * \param dirPin Direction: True means clockwise direction on X axis, and downwards on Y + * \param enable_x Turn On/Off power supply on X axis motor + * \param enable_y Turn On/Off power supply on Y axis motor + * + */ + //void _step(int steps, bool dir); + // void setMotorsPins(int pulPin, int dirPin, +// int enblPin, int pulPin1, int dirPin1, int enblPin1, int ledPin, int ddir1, int ddir2, int motor1, int motor2, int sensor1, int sensor2); + +void setMotorsPins(int step1, int dir1, int enable1, int step2, int dir2, int enable2, int ledPin, int ddir1, int ddir2, int motor1, int motor2, int sensor1, int sensor2); + + + /** + * Sets the pins connected to the sensors + * + * \param s0Pin_x Pin for the 0º limit sensor on X axis + * \param s360Pin_x Pir for the 360º limit sensor on X axis + * \param sbottomPin_y Pin for the 0º limit sensor on Y axis + * \param stopPin_y Sensor for the 90º limit sensor on Y axis + */ + void setSensorsPins(int s0Pin_x, int s360Pin_x, int sbottomPin_y, int stopPin_y); + + /** + * Points the device towards the given coordinates + * + * \param rx Radians for the X axis, on range of 0 - 2*Pi + * \param ry Radians for the Y axis: on range of 0 - Pi + */ + void goToRads(float rx, float ry); + + /** + * Accelerated movement for X axis + * + * The movement stops when a 'stop' command is received by the serial port + * + * \param dir Direction: True means clockwise direction + * \return Returns true in case of reaches a limit sensor + */ + // bool movx(bool dir, int pulPin, int dirPin); + bool movx(bool dir, int step1, int dir1); + /** + * Accelerated movement for Y axis + * + * The movement stops when a 'stop' command is received by the serial port + * + * \param dir Direction: True means upwards + * \return Returns true in case of reaches a limit sensor + */ + // bool movy(bool dir, int pulPin1, int dirPin1); + bool movy(bool dir, int step2, int dir2); + void OSP::_moveEqua(int x, int y); + }; +#endif diff --git a/Other_Arduino_Codes/TwoNema17-2DM542-OSP-Plugin-Test/Readme b/Other_Arduino_Codes/TwoNema17-2DM542-OSP-Plugin-Test/Readme new file mode 100644 index 0000000..98ad9e1 --- /dev/null +++ b/Other_Arduino_Codes/TwoNema17-2DM542-OSP-Plugin-Test/Readme @@ -0,0 +1,16 @@ +This code is used to test OSP plugin with NEMA17 motors and 2DM542 microstep driver. + +The connections for one motor are as under: +1. The pins A+ A- B+ and B- are connected with the four pins of NEMA17. +2. V+ and GND are connected to 24V power supply terminals. GND is shorted with the GND of Arduino. +3. ENA- DIR- and PLS- are connected to GND of Arduino. +4. PLS+ --> D10 of Arduino. +5. DIR+ --> D11 of Arduino. +6. ENA+ --> D12 of Arduino. +7. RS232 pins were completely OFF. +8. The swicthes on the drivers were adjusted for 3200 steps per revolution and 1A current supply. + +Both the drivers were powered by a single 24V DC supply. + + + diff --git a/Other_Arduino_Codes/TwoNema17-2DM542-OSP-Plugin-Test/TwoNema17-2DM542-OSP-Plugin-Test.ino b/Other_Arduino_Codes/TwoNema17-2DM542-OSP-Plugin-Test/TwoNema17-2DM542-OSP-Plugin-Test.ino new file mode 100644 index 0000000..e70195a --- /dev/null +++ b/Other_Arduino_Codes/TwoNema17-2DM542-OSP-Plugin-Test/TwoNema17-2DM542-OSP-Plugin-Test.ino @@ -0,0 +1,276 @@ +#include +#include +#include +//#include "CoordsLib.h" +#include "OSP.h" +// step dir enable +// 10 11 12 7 8 9 +// Declarations for stepper motor 1 (at the base) +#define dir1 11 +#define step1 10 +//int ms1_1=10; +//int ms1_2=11; +//int ms1_3=12; +#define enable1 12 + +// Declarations for stepper motor 2 (at the top holding the laser) +#define dir2 8 +#define step2 7 +//int ms2_1=7; +//int ms2_2=8; +//int ms2_3=9; +#define enable2 9 +//int ResolutionPin = 12; + +#define ledPin 13 // LED pin + +#define ddir1 0 +#define ddir2 0 +#define motor1 0 +#define motor2 0 + +#define sensor1 A2 +#define sensor2 A3 + +/* +#define pulPin 10 // step of stepper motor +#define dirPin 11 // direction of stepper motor +#define pulPin1 3 +#define dirPin1 4 +#define enblPin1 5 + +#define ledPin 13 +#define ddir1 0 +#define ddir2 0 +#define motor1 0 +#define motor2 0 + +#define sensor1 7 +#define sensor2 8 + +*/ + +/** + * Output pin to control the laser pointer + */ +int laserPin = 6; + + +/** + * Library for control the device: stepper motors, sensors, current position.. + */ +OSP Axes = OSP(); + +//Axes.pulPin =10; +//Axes.dirPin = 11; +//Axes.enblPin = 12; +//Axes.pulPin1 = 3; +//Axes.dirPin1 = 4; +//Axes.enblPin1 = 5; +//Axes.ledPin = 13; +//Axes.ddir1; +//Axes.ddir2; +// +//Axes.motor1; +//Axes.motor2; + +/** + * Initializes the serial port and sets pins to control the device + */ + String m_data,sac_data,salt_data; + void(* resetFunc) (void) = 0; +void setup(){ + Serial.begin(9600); + //myservo.attach(9,500,2100);///ATTACH servo + //Serial.println("init"); + + pinMode(laserPin, OUTPUT); + //pinMode(ResolutionPin, OUTPUT); + //digitalWrite(ResolutionPin,HIGH); + laserOff(); + + pinMode(sensor1, INPUT); + pinMode(sensor2, INPUT); + + Axes.setMotorsPins(dir1, step1, enable1, dir2, step2, enable2, ledPin, ddir1, ddir2, motor1, motor2, sensor1,sensor2); + + //Axes.setMotorsPins(pulPin, dirPin, enblPin, pulPin1, dirPin1, enblPin1, ledPin, ddir1, ddir2, motor1, motor2, sensor1,sensor2); + + } + +/** + * Turn the laser On + */ +void laserOn(int intensity){ +analogWrite(laserPin, intensity); +} + +/** + * Turn the laser Off + */ +void laserOff(){ + digitalWrite(laserPin, LOW); +} + +void getAcAlt(){ + char bytes[40]; + //char *m,*sac,*salt; + //String m_data,sac_data,salt_data; + int nbytes = 0; + bool recv=false; + while(!recv){ + //Serial.println(recv); + while(nbytes < 17){ + //Serial.println("Inside nbytes");Serial.println(nbytes); + if(Serial.available() > 0){ + bytes[nbytes] = Serial.read(); + nbytes++; + } + //Serial.println(bytes); + recv=true; + } + } + + + + ///*################################ + // movem_-4.5434_-2.5254 + //Serial.println(bytes); + +String data= String(bytes); +data.trim(); +int len=data.length(); +//Serial.println(data); +//Serial.println(len); +//data.remove(17,len-11); +//Serial.println("-----------"); +//Serial.println(data); +//delay(20); +m_data=data; +sac_data=data; +salt_data=data; + +m_data.remove(1,len-1); +//Serial.println(m_data); + +sac_data.remove(0,2); +len=sac_data.length(); +//Serial.println(len); +sac_data.remove(7,len-7); + + +len=salt_data.length(); +//Serial.println(len); + +salt_data=salt_data.substring(10,17); + + +} + + +void loop(){ + float ac, alt; + char comm[5]; + char dir; + int bytes_recv = 0; + bool mov_end; + int ret; //Added to check val of PX + comm[4]='\0'; + while(bytes_recv < 4){ + //Waiting for a command... + if (Serial.available() > 0) + comm[bytes_recv++] = Serial.read(); + + } + if(strcmp(comm, "move")==0){ + //delay(10); + //Serial.println("float"); + Serial.println("float"); + getAcAlt(); + Serial.println("done_move"); + Axes.goToRads(sac_data.toFloat(),salt_data.toFloat()); + } + if(strcmp(comm, "movl")==0){ + mov_end=Axes.movx(false,step1,dir1); // mov_end=Axes.movx(false,pulPin,dirPin); + if(mov_end) + Serial.println("done_movl"); + } + else if(strcmp(comm, "movr")==0){ + mov_end=Axes.movx(true,step1,dir1); // mov_end=Axes.movx(true,pulPin,dirPin); + if(mov_end) + Serial.println("done_movr"); + } + else if(strcmp(comm, "movu")==0){ + mov_end=Axes.movy(false,step2,dir2); // mov_end=Axes.movy(false,pulPin1,dirPin1); + if(mov_end) + Serial.println("done_movu"); + } + else if(strcmp(comm, "movd")==0){ + mov_end=Axes.movy(true,step2,dir2); // mov_end=Axes.movy(true,pulPin1,dirPin1); + if(mov_end)//==false) + Serial.println("done_movd"); + } + else if(strcmp(comm, "init")==0){ + Axes.init(); + Serial.println("done_init"); + }else if(strcmp(comm, "laon")==0){ + laserOn(50); + Serial.println("done_laon"); + }else if(strcmp(comm, "loff")==0){ + laserOff(); + Serial.println("done_loff"); + }else if(strcmp(comm, "post")==0){ + ac=Axes.getX(); + alt=Axes.getY(); + Serial.print("t_"); + Serial.print(ac,6); + Serial.print("_"); + Serial.println(alt,6); + }else if(strcmp(comm,"stop")==0){ + //TODO: IF not detected inside the movx function create a function to stop movement + Axes.stopper=1; + } + else if(strcmp(comm,"rest")==0){ + resetFunc(); + } + else if(strcmp(comm,"moff")==0){ + digitalWrite(A0, HIGH); + delay(100); + digitalWrite(A1, HIGH); + delay(100); + Serial.println("done_motor_off"); + + } + else if(strcmp(comm,"moon")==0){ + digitalWrite(A0, LOW); + delay(100); + digitalWrite(A1, LOW); + delay(100); + Serial.println("done_motor_on"); + } + else if(strcmp(comm,"lase")==0){ + Serial.println("done_lase"); + char bytes[40]; + int nbytes = 0; + bool recv=false; + while(!recv){ + //Serial.println(recv); + while(nbytes < 4){ + //Serial.println("Inside nbytes");Serial.println(nbytes); + if(Serial.available() > 0){ + bytes[nbytes] = Serial.read(); + nbytes++; + } + //Serial.println(bytes); + recv=true; + } + } + String data= String(bytes); + int intensity=data.toInt(); + laserOn(intensity); + } + else{ + //Serial.println(comm); + } +} + diff --git a/Other_Arduino_Codes/TwoNema17-A4988-OSP-Plugin-Test/OSP.cpp b/Other_Arduino_Codes/TwoNema17-A4988-OSP-Plugin-Test/OSP.cpp new file mode 100644 index 0000000..de77405 --- /dev/null +++ b/Other_Arduino_Codes/TwoNema17-A4988-OSP-Plugin-Test/OSP.cpp @@ -0,0 +1,491 @@ +// #include "WProgram.h" // Arduino < 1.0 +#include //Arduino >= 1.0 +#include "OSP.h" + +//int steps; +int dir1, step1, dir2, step2; +int stepx; +int stepy; +int low; +int high; +int x_rev_set = 0; +int y_rev_set = 0; +float valx; +float valy; +float _pgrad_x; +float _pgrad_y; +//bool blind; +//int diff_x; +//int diff_y; +int stepx_new; +int stepy_new; +float degsH; +float degsV; +float degsH_new; +float degsV_new; +//int slow=50; +//int xcount; +//int ycount; + + +OSP::OSP(){ + stopper=0; +} + +void OSP::setMotorsPins(int step1, int dir1, int enable1, int step2, int dir2, int enable2, int ledPin, int ddir1, int ddir2, int motor1, int motor2, int sensor1, int sensor2) +{ + + pinMode(step1, OUTPUT); // pinMode(pulPin, OUTPUT); + pinMode(dir1, OUTPUT); // pinMode(dirPin, OUTPUT); + pinMode(enable1, OUTPUT); // pinMode(enblPin, OUTPUT); + pinMode(step2, OUTPUT); // pinMode(pulPin1, OUTPUT); + pinMode(dir2, OUTPUT);// pinMode(dirPin1, OUTPUT); + pinMode(enable2, OUTPUT); // pinMode(enblPin1, OUTPUT); + pinMode(ledPin, OUTPUT); // pinMode(ledPin, OUTPUT); + pinMode(sensor1, INPUT); // pinMode(sensor1, INPUT); + pinMode(sensor2, INPUT); // pinMode(sensor2, INPUT); + + digitalWrite(step1, LOW); // digitalWrite(pulPin, LOW); + digitalWrite(step2, LOW); // digitalWrite(pulPin1, LOW); + digitalWrite(ledPin, LOW); // digitalWrite(ledPin, LOW); + digitalWrite(enable1, LOW); // digitalWrite(enblPin, LOW); + digitalWrite(enable2, LOW); // digitalWrite(enblPin1, LOW); + digitalWrite(dir1, HIGH); // digitalWrite(dirPin, LOW); +digitalWrite(dir2, HIGH); // digitalWrite(dirPin1, LOW); + + + //Serial.begin(9600); + Serial.println("Setting up Motor pins..done"); + + digitalWrite(step1, HIGH); // digitalWrite(enblPin, HIGH); + delay(100); + digitalWrite(step1, LOW); // digitalWrite(enblPin, LOW); + digitalWrite(step2, HIGH); // digitalWrite(enblPin1, HIGH); + delay(100); + digitalWrite(step2, LOW); // digitalWrite(enblPin1, LOW); + + + motor1 = 10;//number of steps + motor2 = 20;//number of steps + + ddir1=1;//dir 1 for CW 0 for CCW + ddir2=1;//dir 1 for CW 0 for CCW + delay(1000); +} + + +//####################################### +void OSP::init() +{ + low = 0; + high = 3200;//Motor driver switch setting; + + _pgrad_x=(float)((float)(high-low)/360); + _pgrad_y=(float)((float)(high-low)/180); + + stepx=0; + stepy=0; + low = 0; +high = 0; +x_rev_set = 0; +y_rev_set = 0; +valx = 0; +valy = 0; +_pgrad_x = 0; +_pgrad_y = 0; +stepx_new = 0; +stepy_new = 0; +degsH = 0; +degsV = 0; +degsH_new = 0; +degsV_new = 0; + + //Serial.println(_pgrad_x); + } + +//#################################### + int OSP::getPX() //Steps per revolution on X axis + { + return x_rev_set = 3200;//Change this as per the switch setting on the driver + } +//####################################### + + int OSP::getPY() //Steps per revolution on Y axis + { + return y_rev_set = 3200;//Change this as per the switch setting on the driver + } + + //##################################### + bool OSP::movx(bool dir, int step1, int dir1) //Move on X axis // ovx(bool dir, int pulPin, int dirPin) + { + int bytes_recv = 0; + char comm[5]="nost"; + + comm[bytes_recv++] = Serial.read(); + //Serial.print("value of comm is "); + //Serial.println(comm); + while(strcmp(comm, "stop")!=0 || stopper) + { + //Serial.println("movx"); + //Serial.println(pulPin); + if(dir==1) + { + + if(stepx>=3200) + { + stepx=3200; + } + /*digitalWrite(dirPin, HIGH); + digitalWrite(pulPin, HIGH); + digitalWrite(pulPin, LOW); */ + else + { + digitalWrite(dir1, LOW); + digitalWrite(step1, HIGH); + delay(10); + digitalWrite(step1, LOW); + delay(10); + + stepx++; + } + + //Serial.println(stepx); + + } + else + { + if(stepx<=0) + { + stepx = 0; + } + else { + /*digitalWrite(dirPin, LOW); + digitalWrite(pulPin, HIGH); + digitalWrite(pulPin, LOW);*/ + + digitalWrite(dir1, HIGH); + digitalWrite(step1, HIGH); + delay(10); + digitalWrite(step1, LOW); + delay(10); + + stepx--; + + } + + //Serial.println(stepx); + } + //delay(50); + while(Serial.available() > 0) + comm[bytes_recv++] = Serial.read(); + //Serial.println(comm); + //Serial.println("inside second while"); + //Safeguard... + //Serial.println(stepx); + if(bytes_recv>4) + { + break; + + } + +} + + + return true; + } + + //##################################### + bool OSP::movy(bool dir, int step2, int dir2) //Move on Y axis + // bool OSP::movy(bool dir, int pulPin1, int dirPin1) + { + // Serial.print("stepx of axes "); Serial.println(stepx); + int bytes_recv = 0; + char comm[5]="nost"; + + comm[bytes_recv++] = Serial.read(); + //Serial.print("value of comm is "); + //Serial.println(comm); + while(strcmp(comm, "stop")!=0) + { + if(dir==0) + { + if(stepy>=3200/4) + { + stepy=3200/4; + } + else + { + digitalWrite(dir2, LOW); // digitalWrite(dirPin1, HIGH); + digitalWrite(step2, HIGH); // digitalWrite(pulPin1, HIGH); + delay(10); + digitalWrite(step2, LOW); // digitalWrite(pulPin1, LOW); + delay(10); + stepy++; + } + //Serial.println(stepy); + + } + else + { + if(stepy<=0) + { + stepy=0; + //Serial.println("stop"); + + } + else + { + digitalWrite(dir2, HIGH); // digitalWrite(dirPin1, LOW); + digitalWrite(step2, HIGH); // digitalWrite(pulPin1, HIGH); + delay(10); + digitalWrite(step2, LOW); // digitalWrite(pulPin1, LOW); + delay(10); + stepy--; + } + //Serial.println(stepy); + + } + + + //delay(50); + while(Serial.available() > 0) + comm[bytes_recv++] = Serial.read(); + //Serial.println(comm); + //Serial.println("inside second while"); + //Safeguard... + //Serial.println(stepy); + if(bytes_recv>4) + { + break; + // + } + +} + + + return true; + } + + //560 to 2420 << 590 2190 +//################################# + +float OSP::getX() +{ + /** + * Returns current position on X axis + * + * \return X position as radians to stellarium + */ + + //Serial.println(stepx); + float valx; + //valx = map(stepx, 0, 3200, 0, 360); + valx = stepx*(float)(360.00/3200.00); + valx = (float)(M_PI/180)*valx; + //vlax = _deg2rad(valx); + + return valx; +} + +float OSP::getY() +{ + /** + * Returns current position on Y axis + * + * \return Y position as radians to stellarium + */ + //Serial.println(stepy); + + float valy; + + valy = stepy*(float)(360.00/3200.00); +// valy = map(stepy, 0, 3200/2, 0, 180); + valy = (float)(M_PI/180)*valy; +// //vlay=_deg2rad(valy); + + return valy; +} + +int OSP::getPx() +{ + /** + * Return the number of steps per revolution of the Y axis + * + * \return Steps per revolution + */ + + return stepx; + +} + +int OSP::getPy() +{ + return stepy; +} + + +float OSP::_rad2deg(float rad){ +// return lrint( (float) (180.00/M_PI)*rad); + return (float) (180.00/M_PI)*rad; + +} + +float OSP::_deg2rad(float deg){ + return (float) (M_PI/180.00)*deg; +} + +/* +void AxesLib::goToRads(float rx, float ry){ + float degsH = (360.0 - _rad2deg(rx)); + if(degsH >= 360.0) + degsH = degsH - 360.0; + + _moveTo((float) degsH*_pgrad_x, (float) _rad2deg(ry)*_pgrad_y); +}*/ + +//################################################ + +//###################################### +void OSP::goToRads(float x_rad, float y_rad)//give this input for debugging> movem_-4.5434_-2.5254 +{ + + float degsH = _rad2deg(x_rad); + float degsV = _rad2deg(y_rad); + +//if((degsH_new==degsH) +//if(degsV_new==degsV)) +//if((degsH_new>degsH) +//{ +// } +//if(degsV_new>degsV)) +//if((degsH_newdegsH)||(degsV_new>degsV)) +// +//{ +// degsH = degsH_new; +// degsV = degsV_new; + //Serial.println(degsH); + //Serial.println(degsV); +_moveTo(degsH,degsV); +// } + + //degsH = degsH+180; + //degsV = 180-degsV; + + +} +//################################################## +void OSP::_moveTo(float x, float y, char* method) +{ + //stepx_new = map(abs(x), 0, 360, low, high); + //stepy_new = map(abs(y), 0, 180, low, high); + + + stepx_new = (3200*x)/360;//map to counts + stepy_new = (3200*y)/360;//map to counts + +// if(((stepx-stepx_new)==0)||((stepy-stepy_new)==0)) +// { +// stepx=0; +// stepy=0; +// } +// else +// { +// stepx=stepx_new-stepx; +// stepy=stepy_new-stepy; + //} + +//if((stepx_new>stepx)||(stepy_new>stepy)) +//{ +// Serial.println("Forward"); +// stepx=stepx_new-stepx; +// stepy=stepy_new-stepy; +// } +// else +// { Serial.println("Reverse"); +// +// stepx=stepx-stepx_new; +// stepy=stepy-stepy_new; +// } + +stepx=stepx_new-stepx; +stepy=stepy_new-stepy; + + + //Serial.println(stepx); + //Serial.println(stepy); + + //_moveXY(stepx, stepy); + //Serial.println("done_move"); + + + //Serial.println("done_move"); + + + + //Serial.println(low); + //Serial.println(high); + //Serial.println(stepx_new); + //Serial.println(stepy_new); + + //_moveDDA(stepx_new, stepy_new); + _moveXY(stepx, stepy,true); + stepx=stepx_new; +stepy=stepy_new; +// //_moveEqua(stepx_new, stepy_new); +// stepx=stepx_new; +// stepy=stepy_new; +} +//################################################## +void OSP::_moveXY(int x, int y, bool nodelay) +{ + + + if(y>3200/4) y = 3200/4; // This is to limit the Tilt Motor to 90 degree + + for(int count=0;count + #include + + + /** + * \brief Class that manages movements and the laser of the device + * + * Uses the stepper motors and sensors to positioning the device at a given horizontal coordinates, + * within a range of 360º degrees in horizontal, and 180º on vertical. + * To the movements uses the DDA algorithm (Digital Differential Algorithm). + */ + class OSP{ + public: + + // int pulPin, + // int pulPin, dirPin, enblPin, pulPin1, dirPin1, enblPin1, ledPin, ddir1, ddir2, motor1, motor2, sensor1,sensor2, _stPin_x, _stPin_y, _dirPin, _enable_x, _enable_y; + +int step1, dir1, enable1, step2, dir2, enable2, ledPin, ddir1, ddir2, motor1, motor2, sensor1,sensor2, _stPin_x, _stPin_y, _dirPin, _enable_x, _enable_y; + + int stepx, stepy; + int low; + int high; + int stopper; + float degsH; + float degsV; + + /** + * Pins to control the stepper motors + */ + + /** + * Steps per degree on each axis + */ + float _pgrad_x, _pgrad_y; + + /** + * Current position of each axis (in radians) + */ + float _rx, _ry; + + /** + * Auxiliary variables, maximum theoric values, and steps per revolution respectively + */ + int _x, _y, _X, _Y, _pv_x, _pv_y; + + /** + * Theoric central values of each axis + */ + int _revx, _topy; + + /** + * Indicates if X axis has been "reverted" (Y is between 90º and 180º) + */ + bool _x_rev; + + /** + * Sensor pins + */ + int _s0Pin_x, _s360Pin_x, _sbottomPin_y, _stopPin_y; + + /** + * Transforms radian to degrees + * + * \param rad Radians + * \return degrees + */ + float _rad2deg(float rad); + + /** + * Degrees to radians + * + * \param deg Degrees + * \return radians + */ + float _deg2rad(float deg); + + /** + * Moves one of the motors a given number of steps + * + * \param axis Pin of the motor to move + * \param dir Direction: True means clockwise direction on X, and upwards on Y + * \param steps Number of steps (if limit sensor is not reached) + * \param sensor Pin of the sensor that can be reached towards that direction + * \param nodelay Skip the initial delay (useful to DDA algorithm) + * \return Number of steps (distinct of the steps parameter if the sensor has been reached) + */ + //int _step(int axis, bool dir, int steps, int sensor, bool nodelay=false); + + /** + * Enables the motors power supply + */ + void _enableMotors(); + + /** + * Disables the motors power supply + */ + void _disableMotors(); + + /** + * Moves the device to the given position + * + * \param x Number of steps from 0 to the desired position on X axis + * \param y Number of steps from 0 to the desired position on Y axis + * \param method Algorithm selection: DDA or XY (first X axis, then Y), by default is DDA + */ + void _moveTo(float x, float y, char* method = "DDA"); + + /** + * Moves the device to the given position, first X axis and then Y axis + * + * \param x Number of steps from 0 to the desired position on X axis + * \param y Number of steps from 0 to the desired position on Y axis + * \param nodelay Omits the delay on changes of axis or direction + */ + void _moveXY(int x, int y, bool nodelay=false); + + /** + * Moves the device to the given position using DDA algorithm + * + * \param x Number of steps from 0 to the desired position on X axis + * \param y Number of steps from 0 to the desired position on Y axis + */ + void _moveDDA(int x, int y); + //void _moveDDA(int x, int y, float dirx, float diry); + + public: + /** + * Class constructor + */ + OSP(); + + /** + * Initializes the device + * + * Along the process obtains the number of steps on each axis, and calculates the steps + * per degree for positioning + */ + void init(); + + /** + * Returns current position on X axis + * + * \return X position as radians + */ + float getX(); + + /** + * Returns current position on Y axis + * + * \return Y position as radians + */ + float getY(); + + /** + * Return the number of steps per revolution of the X axis + * + * \return Steps per revolution + */ + int getPX(); + + /** + * Number of steps from 0º to current position on X axis + * + * \return Current position on X + */ + int getPx(); + + /** + * Return the number of steps per revolution of the Y axis + * + * \return Steps per revolution + */ + int getPY(); + + /** + * Number of steps from 0º to current position on Y axis + * + * \return Current position on Y + */ + int getPy(); + + /** + * Sets the pins to control the stepper motors + * + * \param stPin_x Pin to move the X axis + * \param stPin_y Pin to move the Y axis + * \param dirPin Direction: True means clockwise direction on X axis, and downwards on Y + * \param enable_x Turn On/Off power supply on X axis motor + * \param enable_y Turn On/Off power supply on Y axis motor + * + */ + //void _step(int steps, bool dir); + // void setMotorsPins(int pulPin, int dirPin, +// int enblPin, int pulPin1, int dirPin1, int enblPin1, int ledPin, int ddir1, int ddir2, int motor1, int motor2, int sensor1, int sensor2); + +void setMotorsPins(int step1, int dir1, int enable1, int step2, int dir2, int enable2, int ledPin, int ddir1, int ddir2, int motor1, int motor2, int sensor1, int sensor2); + + + /** + * Sets the pins connected to the sensors + * + * \param s0Pin_x Pin for the 0º limit sensor on X axis + * \param s360Pin_x Pir for the 360º limit sensor on X axis + * \param sbottomPin_y Pin for the 0º limit sensor on Y axis + * \param stopPin_y Sensor for the 90º limit sensor on Y axis + */ + void setSensorsPins(int s0Pin_x, int s360Pin_x, int sbottomPin_y, int stopPin_y); + + /** + * Points the device towards the given coordinates + * + * \param rx Radians for the X axis, on range of 0 - 2*Pi + * \param ry Radians for the Y axis: on range of 0 - Pi + */ + void goToRads(float rx, float ry); + + /** + * Accelerated movement for X axis + * + * The movement stops when a 'stop' command is received by the serial port + * + * \param dir Direction: True means clockwise direction + * \return Returns true in case of reaches a limit sensor + */ + // bool movx(bool dir, int pulPin, int dirPin); + bool movx(bool dir, int step1, int dir1); + /** + * Accelerated movement for Y axis + * + * The movement stops when a 'stop' command is received by the serial port + * + * \param dir Direction: True means upwards + * \return Returns true in case of reaches a limit sensor + */ + // bool movy(bool dir, int pulPin1, int dirPin1); + bool movy(bool dir, int step2, int dir2); + void OSP::_moveEqua(int x, int y); + }; +#endif diff --git a/Other_Arduino_Codes/TwoNema17-A4988-OSP-Plugin-Test/Readme b/Other_Arduino_Codes/TwoNema17-A4988-OSP-Plugin-Test/Readme new file mode 100644 index 0000000..e30c26e --- /dev/null +++ b/Other_Arduino_Codes/TwoNema17-A4988-OSP-Plugin-Test/Readme @@ -0,0 +1,3 @@ +This code has been tested on NEMA17 and A4988 motor drivers with OSP plugin. + +All the resolutions pins on both of the drivers were connected with single digital pin. diff --git a/Other_Arduino_Codes/TwoNema17-A4988-OSP-Plugin-Test/TwoNema17-A4988-OSP-Plugin-Test.ino b/Other_Arduino_Codes/TwoNema17-A4988-OSP-Plugin-Test/TwoNema17-A4988-OSP-Plugin-Test.ino new file mode 100644 index 0000000..4ca6c6c --- /dev/null +++ b/Other_Arduino_Codes/TwoNema17-A4988-OSP-Plugin-Test/TwoNema17-A4988-OSP-Plugin-Test.ino @@ -0,0 +1,271 @@ +#include +#include +#include +//#include "CoordsLib.h" +#include "OSP.h" +// step dir enable +// 10 11 12 7 8 9 +// Declarations for stepper motor 1 (at the base) +#define dir1 4 +#define step1 3 +//int ms1_1=10; +//int ms1_2=11; +//int ms1_3=12; +#define enable1 A0 + +// Declarations for stepper motor 2 (at the top holding the laser) +#define dir2 5 +#define step2 6 +//int ms2_1=7; +//int ms2_2=8; +//int ms2_3=9; +#define enable2 A1 +int ResolutionPin = 12; + +#define ledPin 13 // LED pin + +#define ddir1 0 +#define ddir2 0 +#define motor1 0 +#define motor2 0 + +#define sensor1 A2 +#define sensor2 A3 + +/* +#define pulPin 10 // step of stepper motor +#define dirPin 11 // direction of stepper motor +#define pulPin1 3 +#define dirPin1 4 +#define enblPin1 5 + +#define ledPin 13 +#define ddir1 0 +#define ddir2 0 +#define motor1 0 +#define motor2 0 + +#define sensor1 7 +#define sensor2 8 + +*/ + +/** + * Output pin to control the laser pointer + */ +int laserPin1 = 7; +int laserPin2 = 8; +int laserPin3 = 9; +int laserPin4 = 10; +int laserPin5 = 11; + + +/** + * Library for control the device: stepper motors, sensors, current position.. + */ +OSP Axes = OSP(); + +//Axes.pulPin =10; +//Axes.dirPin = 11; +//Axes.enblPin = 12; +//Axes.pulPin1 = 3; +//Axes.dirPin1 = 4; +//Axes.enblPin1 = 5; +//Axes.ledPin = 13; +//Axes.ddir1; +//Axes.ddir2; +// +//Axes.motor1; +//Axes.motor2; + +/** + * Initializes the serial port and sets pins to control the device + */ + String m_data,sac_data,salt_data; + void(* resetFunc) (void) = 0; +void setup(){ + Serial.begin(9600); + //myservo.attach(9,500,2100);///ATTACH servo + //Serial.println("init"); + + pinMode(laserPin1, OUTPUT); + pinMode(laserPin2, OUTPUT); + pinMode(laserPin3, OUTPUT); + pinMode(laserPin4, OUTPUT); + pinMode(laserPin5, OUTPUT); + pinMode(ResolutionPin, OUTPUT); + digitalWrite(ResolutionPin,HIGH); + laserOff(); + + pinMode(sensor1, INPUT); + pinMode(sensor2, INPUT); + + Axes.setMotorsPins(dir1, step1, enable1, dir2, step2, enable2, ledPin, ddir1, ddir2, motor1, motor2, sensor1,sensor2); + + //Axes.setMotorsPins(pulPin, dirPin, enblPin, pulPin1, dirPin1, enblPin1, ledPin, ddir1, ddir2, motor1, motor2, sensor1,sensor2); + + } + +/** + * Turn the laser On + */ +void laserOn(){ + digitalWrite(laserPin1, HIGH); + digitalWrite(laserPin2, HIGH); + digitalWrite(laserPin3, HIGH); + digitalWrite(laserPin4, HIGH); + digitalWrite(laserPin5, HIGH); +} + +/** + * Turn the laser Off + */ +void laserOff(){ + digitalWrite(laserPin1, LOW); + digitalWrite(laserPin2, LOW); + digitalWrite(laserPin3, LOW); + digitalWrite(laserPin4, LOW); + digitalWrite(laserPin5, LOW); +} + +void getAcAlt(){ + char bytes[40]; + //char *m,*sac,*salt; + //String m_data,sac_data,salt_data; + int nbytes = 0; + bool recv=false; + while(!recv){ + //Serial.println(recv); + while(nbytes < 17){ + //Serial.println("Inside nbytes");Serial.println(nbytes); + if(Serial.available() > 0){ + bytes[nbytes] = Serial.read(); + nbytes++; + } + //Serial.println(bytes); + recv=true; + } + } + + + + ///*################################ + // movem_-4.5434_-2.5254 + //Serial.println(bytes); + +String data= String(bytes); +data.trim(); +int len=data.length(); +//Serial.println(data); +//Serial.println(len); +//data.remove(17,len-11); +//Serial.println("-----------"); +//Serial.println(data); +//delay(20); +m_data=data; +sac_data=data; +salt_data=data; + +m_data.remove(1,len-1); +//Serial.println(m_data); + +sac_data.remove(0,2); +len=sac_data.length(); +//Serial.println(len); +sac_data.remove(7,len-7); + + +len=salt_data.length(); +//Serial.println(len); + +salt_data=salt_data.substring(10,17); + + +} + + +void loop(){ + float ac, alt; + char comm[5]; + char dir; + int bytes_recv = 0; + bool mov_end; + int ret; //Added to check val of PX + comm[4]='\0'; + while(bytes_recv < 4){ + //Waiting for a command... + if (Serial.available() > 0) + comm[bytes_recv++] = Serial.read(); + + } + if(strcmp(comm, "move")==0){ + //delay(10); + //Serial.println("float"); + Serial.println("float"); + getAcAlt(); + Serial.println("done_move"); + Axes.goToRads(sac_data.toFloat(),salt_data.toFloat()); + } + if(strcmp(comm, "movl")==0){ + mov_end=Axes.movx(false,step1,dir1); // mov_end=Axes.movx(false,pulPin,dirPin); + if(mov_end) + Serial.println("done_movl"); + } + else if(strcmp(comm, "movr")==0){ + mov_end=Axes.movx(true,step1,dir1); // mov_end=Axes.movx(true,pulPin,dirPin); + if(mov_end) + Serial.println("done_movr"); + } + else if(strcmp(comm, "movu")==0){ + mov_end=Axes.movy(false,step2,dir2); // mov_end=Axes.movy(false,pulPin1,dirPin1); + if(mov_end) + Serial.println("done_movu"); + } + else if(strcmp(comm, "movd")==0){ + mov_end=Axes.movy(true,step2,dir2); // mov_end=Axes.movy(true,pulPin1,dirPin1); + if(mov_end)//==false) + Serial.println("done_movd"); + } + else if(strcmp(comm, "init")==0){ + Axes.init(); + Serial.println("done_init"); + }else if(strcmp(comm, "laon")==0){ + laserOn(); + Serial.println("done_laon"); + }else if(strcmp(comm, "loff")==0){ + laserOff(); + Serial.println("done_loff"); + }else if(strcmp(comm, "post")==0){ + ac=Axes.getX(); + alt=Axes.getY(); + Serial.print("t_"); + Serial.print(ac,6); + Serial.print("_"); + Serial.println(alt,6); + }else if(strcmp(comm,"stop")==0){ + //TODO: IF not detected inside the movx function create a function to stop movement + Axes.stopper=1; + } + else if(strcmp(comm,"rest")==0){ + resetFunc(); + } + else if(strcmp(comm,"moff")==0){ + digitalWrite(A0, HIGH); + delay(100); + digitalWrite(A1, HIGH); + delay(100); + Serial.println("done_motor_off"); + + } + else if(strcmp(comm,"moon")==0){ + digitalWrite(A0, LOW); + delay(100); + digitalWrite(A1, LOW); + delay(100); + Serial.println("done_motor_on"); + } + else{ + //Serial.println(comm); + } +} + -- cgit