-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathold_app.js
190 lines (162 loc) · 4.82 KB
/
old_app.js
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
const Gamecontroller = require('gamecontroller');
const ctrl = new Gamecontroller('jesstech'); // Model of the controller (from ./node_modules/gamecontroller/vendors.js)
const SerialPort = require('serialport')
const Readline = require('@serialport/parser-readline')
const COM_PORT = "COM8";
const BAUD_RATE = 115200;
const STEPS_DISTANCE = 1; // Distance of each steps (in mm)
const STEPS_SPEED = 30; // Speed of the stepper moves (in mm/s)
const MOVE_FREQ = 50; // Frequency (in ms) at which the commands are sent to the robot
// Store Joystick coordinates
// Initialized to 128 => center position
var xyAxis = {
x:128, y:128
};
var zAxis = {
x:128, y:128
};
// Setup COM port
const port = new SerialPort(COM_PORT, { baudRate: BAUD_RATE });
const parser = port.pipe(new Readline({ delimiter: '\n' }));
// The open event is always emitted
port.on('open', function () {
// open logic
console.log("Com Port was opened...")
});
// Display incomming serial port data
port.on('data', function (data) {
console.log('Data:', data.toString());
});
// Open errors will be emitted as an error event
port.on('error', function (err) {
console.log('Error: ', err.message)
});
/******************************************
* Process Game controller events
* Buttons first
*/
ctrl.connect(function () {
console.log('Game On!');
});
// Button 1 is mapped to HOME
ctrl.on('1:release', function () {
console.log('1 was released moving home..');
moveHome();
});
// Button 3 is mapped to INFO
ctrl.on('3:release', function () {
console.log('3 was released');
port.write('M114\r')
});
// Start Button is mapped to Energize the steppers
ctrl.on('Start:release', function () {
console.log('Start was released');
port.write('M17\r');
console.log('.................')
});
// Select Button is mapped to power-off the steppers
ctrl.on('Select:release', function () {
console.log('Select was released');
port.write('M18\r')
});
// Left trigger (L1) Button is mapped to openGripper
ctrl.on('L1:press', function (o) {
console.log('button:L1');
openGripper();
});
// Right trigger (R1) Button is mapped to closeGripper
ctrl.on('R1:press', function (o) {
console.log('button:R1');
closeGripper();
});
/******************************************
* Process Game controller events (JoySticks)
**/
// Left stick (x/y axis)
ctrl.on('JOYL:move', function(o) {
xyAxis.x = o.x;
xyAxis.y = o.y;
});
// Right stick (z axis)
ctrl.on('JOYR:move', function (o) {
zAxis.x = o.x;
zAxis.y = o.y;
});
// Handle error/disconnection events from the game controller
ctrl.on('error', function () {
console.log('Controller could not be found');
});
ctrl.on('close', function () {
console.log('Closing the controller, nothing else to do.');
});
/**
* Main loop to process continuous events while the sticks are moved from rest positions.
* It will call the move methods at a rapid interval
*/
setInterval(function() {
// If no axis are touched exit. No time to waste.
if(xyAxis.x == 128 && xyAxis.y == 128 && zAxis.y == 128) {
return;
}
// Diplay stick position (debug)
if(xyAxis.x != 128) {
console.log('X: ', xyAxis.x);
}
if(xyAxis.y != 128) {
console.log('Y: ', xyAxis.y);
}
if(zAxis.y != 128) {
console.log('Y: ', zAxis.y);
}
// Call move methods for each axis
moveX(xyAxis);
moveY(xyAxis);
moveZ(zAxis);
}, MOVE_FREQ);
/**********************************************
* Move function bellow
* G-code
*/
const DIST_AND_SPEED = STEPS_DISTANCE + " F" + STEPS_SPEED +"\r";
function moveHome() {
port.write('G90\r') // Set to ABSOLUTE coordinate mode
port.write('G28\r'); // Go to home position (end-stop)
port.write('G1 X0 Y120 Z100 F30\r'); // Move to a good "Ready" position
port.write('G91\r') // Set to RELATIVE coordinate mode
}
function moveX(o) {
// console.log(o);
if(o.x > 128) {
port.write("G1 X" + DIST_AND_SPEED);
//port.write("G1 X1 F30\r")
}
if(o.x < 128) {
port.write("G1 X-" + DIST_AND_SPEED);
//port.write("G1 X-1 F30\r")
}
}
function moveY(o) {
// console.log(o);
if(o.y > 128) {
port.write("G1 Y" + DIST_AND_SPEED);
// port.write("G1 Y1 F30\r")
}
if(o.y < 128) {
port.write("G1 Y-" + DIST_AND_SPEED);
// port.write("G1 Y-1 F30\r")
}
}
function moveZ(o) {
// console.log('Z:', o.y);
if(o.y < 128) {
port.write("G1 Z" + DIST_AND_SPEED);
// port.write("G1 Z1 F30\r")
}
if(o.y > 128) {
port.write("G1 Z-" + DIST_AND_SPEED);
// port.write("G1 Z-1 F30\r")
}
}
// Gripper functions
function openGripper() { port.write("M3\r") }
function closeGripper() { port.write("M5\r") }