forked from tpi-refurb/SolarTracker2Axes
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SolarTracker2Axes.pde
135 lines (116 loc) · 3.52 KB
/
SolarTracker2Axes.pde
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
import processing.serial.*;
Serial serial;
Robot robot;
RobotAppearance rAppearance;
PFont myFont;
PShape base, shoulder, upArm, loArm, end;
float rotX, rotY;
float posX=1, posY=50, posZ=50;
float alpha, beta, gamma;
float armPos = 0;
float shoulderRotation = 0;
String topLeftSensor="0";
String topRightSensor ="0";
String bottomLeftSensor ="0";
String bottomRightSensor ="0";
String aveTop="0";
String aveDown ="0";
String aveLeft ="0";
String aveRight ="0";
boolean shiftOn = false;
boolean started = false;
void setup() {
size(960, 660, OPENGL);
robot = new Robot();
rAppearance= new RobotAppearance();
myFont = createFont("verdana", 14);
textFont(myFont);
println(Serial.list()); // Use this to print connected serial devices
//serial = new Serial(this, Serial.list()[0], 115200); // Set this to your serial port obtained using the line above
serial = new Serial(this, "COM1", 9600); // Set this to your serial port obtained using the line above
serial.bufferUntil('\n'); // Buffer until line feed
}
void draw() {
writePos();
robot.init(width, height);
//showLabel();
//println(rotX+" "+-rotY);
robot.setBase(rotX, -rotY);
robot.drawBaseFloor(false);
robot.drawAxis();
robot.setShoulder(shoulderRotation);
robot.setArm(armPos);
robot.setEnd(0);
}
void mouseWheel(MouseEvent e) {
float v = e.getCount();
if (e.isShiftDown()) {
shoulderRotation -= (v * 0.01);
} else {
armPos -= (v * 0.01);
}
}
void mouseDragged() {
rotY -= (mouseX - pmouseX) * 0.01;
rotX -= (mouseY - pmouseY) * 0.01;
}
void keyPressed() {
if (key=='r') {
print("RESET");
robot.setBase(0.0, -0.0);
}
if (key=='s') {
started = true;
}
}
void serialEvent (Serial serial) {
try {
//String buffer = serial.readStringUntil('\n'); // read the serial port until a new line
//if (buffer != null) { // if theres data in between the new lines
/*
buffer = trim(buffer); // get rid of any whitespace just in case
println(buffer);
String getX = buffer.substring(1, buffer.indexOf("H")); // get the value of the servo position
String getV = buffer.substring(buffer.indexOf("H")+1, buffer.length()); // get the value of the sensor reading
shoulderRotation = Float.parseFloat(getX)* 0.01; // set the values to variables
armPos = Float.parseFloat(getV)* 0.01;
//shoulderRotation = Float.parseFloat(getX); // set the values to variables
//armPos = Float.parseFloat(getV);
println("BASE: "+ shoulderRotation);
println("VERT: "+ armPos);
*/
if (started) {
String v = serial.readStringUntil('\t');
String h = serial.readStringUntil('\t');
serial.readStringUntil('\t'); // Ignore extra tab
topLeftSensor = serial.readStringUntil('\t');
topRightSensor = serial.readStringUntil('\t');
bottomLeftSensor = serial.readStringUntil('\t');
bottomRightSensor = serial.readStringUntil('\t');
serial.readStringUntil('\t'); // Ignore extra tab
aveTop = serial.readStringUntil('\t');
aveDown = serial.readStringUntil('\t');
aveLeft = serial.readStringUntil('\t');
aveRight = serial.readStringUntil('\t');
shoulderRotation = Float.parseFloat(v)* 0.01; // set the values to variables
armPos = Float.parseFloat(h)* 0.01;
if (armPos >0) {
armPos = armPos* -1;
}
}
//}
}
catch (Exception e) {
//closePort();
}
finally {
serial.clear(); // Clear buffer
}
}
void stop() {
closePort();
}
void closePort() {
serial.stop();
serial = null;
}