-
Notifications
You must be signed in to change notification settings - Fork 2
/
targetshooter.ino
142 lines (134 loc) · 3.33 KB
/
targetshooter.ino
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
#include<math.h>
#include<Servo.h>
#define pi 3.14
#define servo_pin 9
#define baud_rate 9600
const int steppin=10;
const int dirpin=11;
Servo s;
Servo st;
int len,height,depth;
int pos_step,no_step,pos_servo;
void coordinate_angle(int x,int y,int z);
void trigger();
void motor();
void setup() {
// put your setup code here, to run once:
//attaching servo pins to arduino
s.attach(servo_pin);
//attaching stepper pins with arduino
pinMode(steppin,OUTPUT);
pinMode(dirpin,OUTPUT);
pinMode(12,OUTPUT);
Serial.begin(baud_rate);
s.write(90);
st.attach(8);
pos_step=0;
no_step=0;
/* Serial.println("Enter x:");
l=Serial.read();//inputing x coordinate
Serial.println("Enter y:");
h=Serial.read();//inputing y coordinate
Serial.println("Enter z:");
d=Serial.read();//inputing z coordinate*/
}
void loop() {
// put your main code here, to run repeatedly:
while(Serial.available()>0)
{
len=Serial.read(); //reading the length from serial port directed from pyserial
height=Serial.read(); //reading the length from serial port directed from pyserial
depth=Serial.read();
//reading the length from serial port directed from pyserial
coordinate_angle(len,height,depth);
}
}
void coordinate_angle(int x,int y,int z) //function to convert 3d coordinate to spherical coordinates
{
float r;
int i,phi,theta;
r=sqrt(x*x+y*y+z*z);
phi=(atan(y/x)*180)/pi;//angle on the x y plane(servo)
theta=(atan(sqrt(x*x+y*y)/z)*180)/pi; //angle with the z plane(stepper)
pos_servo=s.read();
Serial.println("Servo Initial Position");
Serial.println(pos_servo);
if(pos_servo<phi)
{
for(i=pos_servo;i<=phi;i++)
{
s.write(i);
delay(20);
}
}
else
{
for(i=pos_servo;i>=phi;i--)
{
s.write(i);
delay(20);
}
}
Serial.println("Servo Final position");
Serial.println(pos_servo);
Serial.println("Stepper Initial Position");
Serial.println(pos_step);
/*if(pos_step<theta)
{
digitalWrite(dirpin,HIGH);
for(i=no_step;i<=(theta/1.8);i++)
{
digitalWrite(steppin,HIGH);
delayMicroseconds(500);
digitalWrite(steppin,LOW);
delayMicroseconds(500);
}
pos_step=theta;
no_step=theta/1.8;
}
else
{
digitalWrite(dirpin,LOW);
for(i=no_step;i>=(theta/1.8);i--)
{
digitalWrite(steppin,HIGH);
delayMicroseconds(500);
digitalWrite(steppin,LOW);
delayMicroseconds(500);
}
pos_step=theta;
no_step=theta/1.8;
}*/
motor(theta);
Serial.println("Stepper Final positon");
Serial.println(pos_step);
trigger(); //for triggering the gun
}
void trigger()
{
int i;
for(i= 120;i>=1;i-=1) // command to move from 180 degrees to 0 degrees
{
st.write(i); //command to rotate the servo to the specified angle
delay(10);
}
}
void motor(int angle)
{
int t=(angle*1000)/360;
if(pos_step<angle)
{
digitalWrite(10,HIGH);
digitalWrite(11,LOW);
analogWrite(12,255);
delay(t);
}
else
{
digitalWrite(11,HIGH);
digitalWrite(10,LOW);
analogWrite(12,255);
delay(t);
}
pos_step=angle;
}