Last active
October 11, 2022 14:38
-
-
Save Neumi/3dca7ba9af8d06fb30662689ef906e1b to your computer and use it in GitHub Desktop.
Ethersweep robot keyboard control Processing Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// more info: github.com/neumi/ethersweep | |
import hypermedia.net.*; | |
UDP udp; | |
String ipLeft = "192.168.0.102"; | |
String ipRight = "192.168.0.101"; | |
String ipGripper = "192.168.0.100"; | |
int port = 8888; | |
JSONObject jsonLeft; | |
JSONObject jsonRight; | |
JSONObject jsonGripper; | |
int speed = 100; | |
int steps = 1600; | |
int hold = 0; | |
int ramp = 50; | |
int stepmode = 8; | |
boolean gripperOpen = true; | |
void setup() { | |
size(600, 600); | |
udp = new UDP( this, 8888 ); | |
} | |
void draw() { | |
background(20); | |
steps = mouseX * 10; | |
speed = mouseY + 50; | |
text( "Steps: " + str(steps), mouseX +10, mouseY); | |
text( "Speed: " + str(speed) + " µs", mouseX +10, mouseY+20); | |
} | |
void keyPressed() { | |
background(200, 50, 50); | |
switch(key) { | |
case 'w': | |
println("forward"); | |
driveForward(); | |
break; | |
case 's': | |
println("backward"); | |
driveBack(); | |
break; | |
case 'd': | |
driveRight(); | |
break; | |
case 'a': | |
driveLeft(); | |
break; | |
case 'x': | |
toggleGripper(); | |
break; | |
} | |
} | |
void driveForward() { | |
jsonLeft = new JSONObject(); | |
jsonLeft.setInt("drivemode", 2); | |
jsonLeft.setInt("speed", speed); | |
jsonLeft.setInt("slope", ramp); | |
jsonLeft.setInt("steps", steps); | |
jsonLeft.setInt("stepmode", stepmode); | |
jsonLeft.setInt("direction", 0); | |
jsonLeft.setInt("hold", hold); | |
jsonRight = new JSONObject(); | |
jsonRight.setInt("drivemode", 2); | |
jsonRight.setInt("speed", speed); | |
jsonRight.setInt("slope", ramp); | |
jsonRight.setInt("steps", steps); | |
jsonRight.setInt("stepmode", stepmode); | |
jsonRight.setInt("direction", 1); | |
jsonRight.setInt("hold", hold); | |
udp.send( jsonLeft.toString(), ipLeft, port ); | |
udp.send( jsonRight.toString(), ipRight, port ); | |
} | |
void driveLeft() { | |
jsonLeft = new JSONObject(); | |
jsonLeft.setInt("drivemode", 2); | |
jsonLeft.setInt("speed", speed); | |
jsonLeft.setInt("slope", ramp); | |
jsonLeft.setInt("steps", steps); | |
jsonLeft.setInt("stepmode", stepmode); | |
jsonLeft.setInt("direction", 0); | |
jsonLeft.setInt("hold", hold); | |
jsonRight = new JSONObject(); | |
jsonRight.setInt("drivemode", 2); | |
jsonRight.setInt("speed", speed); | |
jsonRight.setInt("slope", ramp); | |
jsonRight.setInt("steps", steps); | |
jsonRight.setInt("stepmode", stepmode); | |
jsonRight.setInt("direction", 0); | |
jsonRight.setInt("hold", hold); | |
udp.send( jsonLeft.toString(), ipLeft, port ); | |
udp.send( jsonRight.toString(), ipRight, port ); | |
} | |
void driveRight() { | |
jsonLeft = new JSONObject(); | |
jsonLeft.setInt("drivemode", 2); | |
jsonLeft.setInt("speed", speed); | |
jsonLeft.setInt("slope", ramp); | |
jsonLeft.setInt("steps", steps); | |
jsonLeft.setInt("stepmode", stepmode); | |
jsonLeft.setInt("direction", 1); | |
jsonLeft.setInt("hold", hold); | |
jsonRight = new JSONObject(); | |
jsonRight.setInt("drivemode", 2); | |
jsonRight.setInt("speed", speed); | |
jsonRight.setInt("slope", ramp); | |
jsonRight.setInt("steps", steps); | |
jsonRight.setInt("stepmode", stepmode); | |
jsonRight.setInt("direction", 1); | |
jsonRight.setInt("hold", hold); | |
udp.send( jsonLeft.toString(), ipLeft, port ); | |
udp.send( jsonRight.toString(), ipRight, port ); | |
} | |
void driveBack() { | |
jsonLeft = new JSONObject(); | |
jsonLeft.setInt("drivemode", 2); | |
jsonLeft.setInt("speed", speed); | |
jsonLeft.setInt("slope", ramp); | |
jsonLeft.setInt("steps", steps); | |
jsonLeft.setInt("stepmode", stepmode); | |
jsonLeft.setInt("direction", 1); | |
jsonLeft.setInt("hold", hold); | |
jsonRight = new JSONObject(); | |
jsonRight.setInt("drivemode", 2); | |
jsonRight.setInt("speed", speed); | |
jsonRight.setInt("slope", ramp); | |
jsonRight.setInt("steps", steps); | |
jsonRight.setInt("stepmode", stepmode); | |
jsonRight.setInt("direction", 0); | |
jsonRight.setInt("hold", hold); | |
udp.send( jsonLeft.toString(), ipLeft, port ); | |
udp.send( jsonRight.toString(), ipRight, port ); | |
} | |
void toggleGripper() { | |
if (gripperOpen) { | |
jsonGripper = new JSONObject(); | |
jsonGripper.setInt("drivemode", 0); | |
jsonGripper.setInt("speed", 500); | |
jsonGripper.setInt("slope", 100); | |
jsonGripper.setInt("steps", 600); | |
jsonGripper.setInt("stepmode", 16); | |
jsonGripper.setInt("direction", 1); | |
jsonGripper.setInt("hold", 1); | |
gripperOpen = false; | |
} else { | |
jsonGripper = new JSONObject(); | |
jsonGripper.setInt("drivemode", 0); | |
jsonGripper.setInt("speed", 500); | |
jsonGripper.setInt("slope", 100); | |
jsonGripper.setInt("steps", 600); | |
jsonGripper.setInt("stepmode", 16); | |
jsonGripper.setInt("direction", 0); | |
jsonGripper.setInt("hold", 1); | |
gripperOpen = true; | |
} | |
udp.send( jsonGripper.toString(), ipGripper, port); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment