summaryrefslogtreecommitdiff
path: root/CodesArduino-for-plugin/TwoNema17-2DM542-OSP-Plugin-Test/OSP.cpp
blob: 3238c9b9b644f1e729e3c31e5472ca50b9683d41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// #include "WProgram.h" // Arduino < 1.0
#include <Arduino.h> //Arduino >= 1.0
#include "OSP.h"


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;

int stepx_new;
int stepy_new;
float degsH;
float degsV;
float degsH_new;
float degsV_new;


OSP::OSP(){
    stopper=0;
}

void OSP::setMotorsPins(int step1, int dir1, int enable1, int step2, int dir2, int enable2)
{
	pinMode(step1, OUTPUT);  
  pinMode(dir1, OUTPUT); 
  pinMode(enable1, OUTPUT); 
  pinMode(step2, OUTPUT); 
  pinMode(dir2, OUTPUT);
  pinMode(enable2, OUTPUT); 
  
  digitalWrite(step1, LOW); 
  digitalWrite(step2, LOW); 
  digitalWrite(enable1, LOW); 
  digitalWrite(enable2, LOW); 
  delayMicroseconds(50); 
  digitalWrite(dir1, HIGH); 
  digitalWrite(dir2, HIGH); 

  
  Serial.println("Setting up Motor pins..done");
  
  digitalWrite(enable1, HIGH); 
  delay(100);
  digitalWrite(enable1, LOW); 
  digitalWrite(enable2, HIGH); 
  delay(100);
  digitalWrite(enable2, LOW); 
  delay(1000);
}


void OSP::init()
{
  low = 0;
  high = 25600; //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;
}

int OSP::getPX() //Steps per revolution on X axis
{
  return x_rev_set = 25600; //Change this as per the switch setting on the driver
}


int OSP::getPY() //Steps per revolution on Y axis
{
  return y_rev_set = 25600; //Change this as per the switch setting on the driver
}
     
bool OSP::movx(bool dir, int step1, int dir1) 
  {
    int bytes_recv = 0;        
    char comm[5]="nost";
    comm[bytes_recv++] = Serial.read();
    while(strcmp(comm, "stop")!=0 || stopper)
  {
    if(dir==1)
    {      
      if(stepx>=25600)
        stepx=25600;
        
      else {
        digitalWrite(dir1, HIGH);
        digitalWrite(step1, HIGH);
        delay(delayValue); 
        digitalWrite(step1, LOW);
        delay(delayValue);
        stepx++;
        }
    }
    else
    {
      if(stepx<=0)
        stepx = 0;
        else {
         digitalWrite(dir1, LOW);
         digitalWrite(step1, HIGH);
         delay(delayValue); 
         digitalWrite(step1, LOW);
         delay(delayValue);
         stepx--;  
        }
     }
   while(Serial.available() > 0)
      comm[bytes_recv++] = Serial.read();
    if(bytes_recv>4)
      break;   
}
    return true;
}
  
bool OSP::movy(bool dir,  int step2, int dir2) 
{
    int bytes_recv = 0;        
    char comm[5]="nost";
 
    comm[bytes_recv++] = Serial.read();
    while(strcmp(comm, "stop")!=0)
    {
    if(dir==0)
    {
      if(stepy>=25600/4)
        stepy=25600/4;
      else {
        digitalWrite(dir2, LOW); 
        digitalWrite(step2, HIGH); 
        delay(delayValue);
        digitalWrite(step2, LOW);  
        delay(delayValue); 
        stepy++; 
      }
    }
    else
    {
      if(stepy<=0)
        stepy=0;
        else {
          digitalWrite(dir2, HIGH); 
          digitalWrite(step2, HIGH); 
          delay(delayValue); 
          digitalWrite(step2, LOW);  
          delay(delayValue); 
          stepy--; 
        }
      }

   while(Serial.available() > 0)
      comm[bytes_recv++] = Serial.read();
    if(bytes_recv>4)
      break;  
}   
  return true;
}

float OSP::getX()
{
  float valx;
  valx = stepx*(float)(360.00/25600.00);
  valx = (float)(M_PI/180)*valx;
  return valx;
}


float OSP::getY()
{
  float valy;
  valy = stepy*(float)(360.00/25600.00);
  valy = (float)(M_PI/180)*valy;
  return valy;
}


int OSP::getPx()
{
  return stepx; 
}


int OSP::getPy()
{
  return stepy;
}


float OSP::_rad2deg(float rad){
  return  (float) (180.00/M_PI)*rad;
}

float OSP::_deg2rad(float deg){
  return (float) (M_PI/180.00)*deg;
}

void OSP::goToRads(float x_rad, float y_rad) 
{
  
  float degsH = _rad2deg(x_rad);
  float degsV = _rad2deg(y_rad);

_moveTo(degsH,degsV);
  
}

void OSP::_moveTo(float x, float y, char* method)
{
  stepx_new = (25600*x)/360; //map to counts
  stepy_new = (25600*y)/360; //map to counts
  
  stepx=stepx_new-stepx;
  stepy=stepy_new-stepy;
  
  _moveXY(stepx,stepy,true);
  stepx=stepx_new;
  stepy=stepy_new;  
}

void OSP::_moveXY(int x, int y, bool nodelay)
{
   if(y>25600/4) 
      y = 25600/4; // This is to limit the tilt Motor to 90 degrees. 
  
  for(int count=0;count<abs(x);count++)
  {
    if(x<0)
      digitalWrite(11, LOW);      
    else
      digitalWrite(11, HIGH); 
        
    digitalWrite(12, HIGH); 
    delay(10); 
    digitalWrite(12, LOW);  
    delay(10);
  }

  for(int count=0;count<abs(y);count++)
  {
    if(y<0)
      digitalWrite(8, HIGH);
    else
      digitalWrite(8, LOW); 
    
    digitalWrite(9, HIGH); 
    delay(10); 
    digitalWrite(9, LOW); 
    delay(10);
  }

  }

void OSP::_moveEqua(int x, int y)
  {
    
  }