Ok so I finally got them to communicate and decompose the string correctly! Instead of using the string class, I just made a “char” array of whatever I wanted, and utilized what jafal sent to me (its in another tab). I am now running into the problem of the motors not running now, but that could be a power issue at this point. Heres what I got.
Arduino:
#include <Wire.h>
#include <Servo.h>
#include <Adafruit_MotorShield.h>
//set up motorhsield
Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x60);
//assign motors
Adafruit_DCMotor *RailMotor = AFMS.getMotor(1);
Adafruit_DCMotor *Swivel = AFMS.getMotor(2);
Adafruit_DCMotor *Elbow = AFMS.getMotor(3);
Adafruit_DCMotor *Screw = AFMS.getMotor(4);
//call the servo library
Servo DrillArm;
Servo ClawArm;
Servo Claw;
const byte number_of_chars = 32;
char receivedChars[number_of_chars];
boolean new_data = false;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
// create with the default frequency 1.6KHz
AFMS.begin();
// Attach a servo to pin #10 and pin #9
DrillArm.attach(11);
ClawArm.attach(10);
Claw.attach(9);
// turn on motor M1
RailMotor->setSpeed(300);
RailMotor->run(FORWARD);
RailMotor->run(BACKWARD);
RailMotor->run(RELEASE);
// turn on M2
Swivel->setSpeed(200);
Swivel->run(FORWARD);
Swivel->run(BACKWARD);
Swivel->run(RELEASE);
// turn on motor M3
Elbow->setSpeed(200);
Elbow->run(FORWARD);
Elbow->run(BACKWARD);
Elbow->run(RELEASE);
// turn on M4
Screw->setSpeed(200);
Screw->run(FORWARD);
Screw->run(BACKWARD);
Screw->run(RELEASE);
}
//setting variables
void loop() {
// put your main code here, to run repeatedly:
receivePacket();
if (new_data == true) {
parseData();
executeParsedData();
new_data = false;
Serial.println(receivedChars);
char buf[4] = {receivedChars[1], receivedChars[2], receivedChars[3], receivedChars[4]};
int mapp = atoi(buf);
Serial.println(mapp);
if (receivedChars[0] == 'A'){ //claw arm control
Serial.println(receivedChars);
ClawArm.write(map(mapp, 0, 180, 0, 180));
}
if (receivedChars[0] == 'B'){ //elbow control
Serial.println(receivedChars);
Elbow->setSpeed(mapp);
Elbow->run(FORWARD);
delay(2000);
}
if (receivedChars[0] == 'C'){ //claw open/close
Serial.println(receivedChars);
Claw.write(map(mapp, 0, 180, 0, 180));
}
if (receivedChars[0] == 'D'){ //drill arm control
Serial.println(receivedChars);
DrillArm.write(map(mapp, 0, 180, 0, 180));
}
if (receivedChars[0] == 'E'){ //rail control
Serial.println(receivedChars);
RailMotor->setSpeed(mapp);
RailMotor->run(FORWARD);
delay(2000);
}
if (receivedChars[0] == 'F'){ //swivel control
Serial.println(receivedChars);
Swivel->setSpeed(mapp);
Swivel->run(FORWARD);
delay(2000);
}
if (receivedChars[0] == 'G'){ //screw control
Serial.println(receivedChars);
Screw->setSpeed(mapp);
Screw->run(FORWARD);
delay(2000);
}
}
}
Processing:
import processing.serial.*;
import net.java.games.input.*;
import org.gamecontrolplus.*;
import org.gamecontrolplus.gui.*;
Serial port;
ControlDevice cont;
ControlIO control;
Arduino arduino;
float swivel, screw, rail, selectcontrol;
boolean shoulderselect, elbowselect, clawarmselect, clawgrab, clawrelease;
String portmessage;
PImage splash;
void setup(){
size(700, 400);
control = ControlIO.getInstance(this);
cont = control.getMatchedDevice("STAR_2021_Artificial_Astronauts_controls");
if (cont == null) {
println("No suitable device configured, exiting program");
System.exit(-1);
}
splash = loadImage("Artificial Astronaut Control Scheme");
println((Object[])Serial.list());
port = new Serial(this, "COM8", 9600);
}
public void getUserInput() {
screw = map(cont.getSlider("Screw").getValue(), -1, 1, 0, 180);
swivel = map(cont.getSlider("SwivelJoint").getValue(), -1, 1, 0, 180);
rail = map(cont.getSlider("Rail").getValue(), -1, 1, 0, 180);
selectcontrol = map(cont.getSlider("SelectControl").getValue(), -1, 1, 0, 360);
shoulderselect = cont.getButton("ShoulderSelect").pressed();
elbowselect = cont.getButton("ElbowSelect").pressed();
clawarmselect = cont.getButton("ClawArmSelect").pressed();
clawgrab = cont.getButton("ClawGrab").pressed();
clawrelease = cont.getButton("ClawRelease").pressed();
}
void draw() {
getUserInput();
background(selectcontrol,400,400);
println("You are now in Control >:)");
//send a list that consists of the joint's name and, position(and direction) to the serial for the arduino
if(clawarmselect){ //control claw arm with right stick
println("Selecting the claw arm to control, use the right stick to control");
portmessage = "<A"+str((int)selectcontrol)+">";
port.write(portmessage);
}
if(elbowselect){ //control elbow with button and control right stick
println("Selecting the elbow to control, use the right stick to control");
portmessage = "<B"+str((int)selectcontrol)+">";
port.write(portmessage);
}
if(clawgrab){ //close claw
println("Claw Closing");
port.write("<"+"C90"+">");
}
if(clawrelease){ //open claw\
println("Claw Opening");
port.write("<"+"C180"+">");
}
if(shoulderselect){ //control elbow with button and control right stick
println("Selecting the shoulder to control, use the right stick to control");
portmessage = "<D"+str((int)selectcontrol)+">";
port.write(portmessage);
}
if(rail > 0){ //control elbow with button and control right stick
println("Selecting the rail to control, use the left stick to control");
portmessage = "<E"+str((int)selectcontrol)+">";
port.write(portmessage);
}
if(swivel > 0){ //control swivel with button and control right stick
println("Selecting the swivel to control, use the left stick to control");
portmessage = "<F"+str((int)selectcontrol)+">";
port.write(portmessage);
}
if(screw > 0){ //control screw with trigger buttons
println("Selecting the elbow to control, use the right stick to control");
portmessage = "<G"+str((int)selectcontrol)+">";
port.write(portmessage);
}
}