-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulator.pde
More file actions
148 lines (126 loc) · 4.33 KB
/
Simulator.pde
File metadata and controls
148 lines (126 loc) · 4.33 KB
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
class Simulator{
boolean arduinoConnected;
private Serial arduino;
public float xPosition;
public float yPosition;
public float zPosition;
private PShape cncBody;
private PShape xSlider;
private PShape ySlider;
private PShape zSlider;
private PShape nema17;
private PShape shaft;
//real measurements
private int feedRate = 100; //how many miliseconds does it take to move one step
//private float stepsPerSec = 600;
private float rotationPerStep = 1.8; //in degrees
private float distancePerDegree = 1.f/12.f; //in centimeters
private float xMax = 52; //distances in cm
private float yMax = 47;//47
private float zMax = 30;//30
private float xRotation = 0;
private float yRotation = 0;
private float zRotation = 0;
//model distances
private float modelxMax = 8; //arbitrary numbers i measured on the models
private float modelyMax = 10.3; //being the range that the sliders can travel
private float modelzMax = 3.5;
public Simulator(Serial arduino){
if(arduino == null)arduinoConnected = false;
else{
this.arduino = arduino;
arduinoConnected = true;
}
cncBody = loadShape("Models/MainBody.obj");
xSlider = loadShape("Models/XSlider.obj");
ySlider = loadShape("Models/YSlider.obj");
zSlider = loadShape("Models/ZSlider.obj");
nema17 = loadShape("Models/Nema17.obj");
shaft = loadShape("Models/Shaft.obj");
xPosition = 0;
yPosition = 0;
zPosition = 0;
}
private void cncView(){ //helper function to position objects
float fov = PI/3.0;
float cameraZ = (height/2.0) / tan(fov/2.0);
perspective(fov, float(width)/float(height),
cameraZ/10.0, cameraZ*10.0);
translate(width/2,height*8/11,0);// magic nubers for positioning
scale(17);
rotateZ(PI);
rotateY(-PI/2);
}
public void drawCNC(){
stroke(255);
lights();
pushMatrix();
cncView();
shape(cncBody);
popMatrix();
pushMatrix();
cncView();
positionSlider('x');
shape(xSlider);
popMatrix();
pushMatrix();
cncView();
positionSlider('y');
shape(ySlider);
popMatrix();
pushMatrix();
cncView();
positionSlider('z');
shape(zSlider);
popMatrix();
}
private void positionSlider(char axis){
if(axis == 'x'){
translate(xPosition * modelxMax / xMax - modelxMax/2.f,0,0);
}else if(axis == 'y'){
translate(0,0,yPosition * modelyMax / yMax - modelyMax/2.f);
}else if(axis == 'z'){
translate(0,0,yPosition * modelyMax / yMax - modelyMax/2.f);
translate(0,zPosition * modelzMax / zMax,0);
}
}
public void stepMotor(char axis, int steps){
String positive = "0";
if(steps>0){
positive = "1";
}
while(abs(steps)>0){
float degreesToRotate = rotationPerStep*steps;
if(axis == 'x'){// x axis
if(xPosition + degreesToRotate * distancePerDegree>xMax || xPosition + degreesToRotate * distancePerDegree<0)break;
xRotation = xRotation + degreesToRotate;
xPosition = xPosition + degreesToRotate * distancePerDegree;
}else if(axis == 'y'){// y axis
if(yPosition + degreesToRotate * distancePerDegree>yMax || yPosition + degreesToRotate * distancePerDegree<0)break;
yRotation = yRotation + degreesToRotate;
yPosition = yPosition + degreesToRotate * distancePerDegree;
}else if(axis == 'z'){// z axis
if(zPosition + degreesToRotate * distancePerDegree>zMax || zPosition + degreesToRotate * distancePerDegree<0)break;
zRotation = zRotation + degreesToRotate;
zPosition = zPosition + degreesToRotate * distancePerDegree;
//arduino.write("z"+positive);
}else{
println("Unfamiliar axis given!");
}
delay(feedRate);
if(steps>0)steps--;
else steps++;
}
}
public void printPosition(){
println("Position: x="+xPosition+" y="+yPosition+" z="+zPosition);
}
public void sendCommands(ArrayList<Command> comms, Serial arduino){
if(arduino==null)println("serial unavailable!");
for(int i = 0; i<comms.size(); i++){
if(arduinoConnected)comms.get(i).sendCommand(arduino);
comms.get(i).print();
delay(500);
}
}
}