summaryrefslogtreecommitdiff
path: root/CodesArduino-for-plugin/TwoNema17-2DM542-OSP-Plugin-Test/TwoNema17-2DM542-OSP-Plugin-Test.ino
blob: 17c7a08595f1f9b50c756cccc5387450af0b4f03 (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
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "OSP.h"

/** 
 * Declarations for stepper motor 1 (at the base)
 * All the pins below are connected to digital pins of Arduino Nano.
 */
 
#define dir1 11 
#define step1 12 
#define enable1 10 

#define relay1 2
#define relay2 4
#define relay3 5
#define relay4 6

/**
 * Declarations for stepper motor 2 (at top holding LASER)
 */
#define dir2 8 
#define step2 9 
#define enable2 7 

/**
 * Digital pin to control the LASER 
 * This pin is connected to gate (G) of IRF540
 */
int laserPin = 3;

 
/**
 * Library for control the device. 
 */
OSP Axes = OSP();
String m_data, sac_data, salt_data; // to store LASER coorinates 

void setup(){
	Serial.begin(9600);	
  pinMode(relay1,OUTPUT);
  pinMode(relay2,OUTPUT);
  pinMode(relay3,OUTPUT);
  pinMode(relay4,OUTPUT);
  
  digitalWrite(relay1, HIGH);
  digitalWrite(relay2, HIGH);
  digitalWrite(relay3, HIGH);
  digitalWrite(relay4, HIGH);
  pinMode(laserPin, OUTPUT);
	laserOff();
  Axes.setMotorsPins(dir1, step1, enable1, dir2, step2, enable2);
	Axes.delayValue = 30; // to adjust motor speed as Coarse and Fine 
 
	}
 

/**
 * Turn LASER on at given intensity 
 */
void laserOn(int intensity){
  analogWrite(laserPin, intensity);
  }

/**
 * Turn LASER Off
 */
void laserOff(){
	digitalWrite(laserPin, LOW);
}


void getAcAlt(){
  char bytes[40];
  int nbytes = 0;
  bool recv=false;
  while(!recv){
  while(nbytes < 17){
    if(Serial.available() > 0){
      bytes[nbytes] = Serial.read();
      nbytes++;
    }
    recv=true;
  }
  }

 
/**
 * As LASER coordinates, we receive string like movem_-4.5434_-2.5254.
 * This string is chunked to extract ac and alt values. 
 */

  String data= String(bytes);
  data.trim();
  int len=data.length();
  m_data=data;
  sac_data=data;
  salt_data=data;
  m_data.remove(1,len-1);
  sac_data.remove(0,2);
  len=sac_data.length();
  sac_data.remove(7,len-7);
  len=salt_data.length();
  salt_data=salt_data.substring(10,17);    
}


void loop(){

 
  float ac, alt;
  char comm[5];
  int bytes_recv = 0;
  bool mov_end;                                                     
  comm[4]='\0';
  while(bytes_recv < 4){
    //Waiting for a command from serial monitor 
    if (Serial.available() > 0)
      comm[bytes_recv++] = Serial.read();
      //Serial.println("here");
      //Serial.println(analogRead(smps));        
  }
  
    
  if(strcmp(comm, "move")==0){
    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); 
    if(mov_end)
      Serial.println("done_movl");
  }	
  
  else if(strcmp(comm, "movr")==0){
    mov_end=Axes.movx(true,step1,dir1); 
    if(mov_end)
      Serial.println("done_movr");
  } 
     
  else if(strcmp(comm, "movu")==0){
    mov_end=Axes.movy(false,step2,dir2); 
    if(mov_end)
      Serial.println("done_movu");
  }
  
  else if(strcmp(comm, "movd")==0){
    mov_end=Axes.movy(true,step2,dir2); 
    if(mov_end)
      Serial.println("done_movd");
  }
  
  else if(strcmp(comm, "init")==0){
    delay(1000);
   digitalWrite(relay1,LOW);
   digitalWrite(relay2,LOW);
   digitalWrite(relay3,LOW);
   digitalWrite(relay4,LOW);
    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){
   Axes.stopper=1;
  }
     
  else if(strcmp(comm,"coad")==0){
    Axes.delayValue = 3; 
    Serial.println("done_high_speed"); 
  }
  
  else if(strcmp(comm,"fiad")==0){
    Axes.delayValue = 30; 
    Serial.println("done_low_speed");
  }
  else if(strcmp(comm,"clos")==0){
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);
    digitalWrite(relay3, HIGH);
    digitalWrite(relay4, HIGH);
    laserOff(); 
    Serial.println("done_close");
    
  }
  else if(strcmp(comm,"lase")==0){
    Serial.println("done_lase");
    char bytes[40];
    int nbytes = 0;
    bool recv=false;
    while(!recv){
      while(nbytes < 4){
        if(Serial.available() > 0){
          bytes[nbytes] = Serial.read();
          nbytes++;
    }
      recv=true;
  }
  }
  String data= String(bytes);
  int intensity=data.toInt();
  laserOn(intensity);
  }
  
  else{ 
  }
}