We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi I have some code to move servos, in my arudino uno in processing. I imported the lp5 library so I have a GUI for the servos. I have sliders for each servo and the data gets transmitted to the arduino pins and out to the servos. I want to know how do I verify IF the data is being transmitted from program to arduino rather than looking at the servos and seeing if they move based on which way I move the sliders?
When I say verify I mean, since the ports are configured and once a connection has been established how do I ensure programmatically that the servos are receiving the data instead of looking at them to see if they move?
here is code:
import processing.serial.*; //imports serail library
import cc.arduino.*; //imports arduino library so I don't have to use arduino IDE
import controlP5.*; //Part of processing library
Arduino arduino;
ControlP5 cp5;
Slider Servo1;
void setup()
{
size(950, 900);
println(Arduino.list()); //sends data to serial port for arduino
arduino = new Arduino(this, Arduino.list()[0], 57600); //sets COMM Port Baud Rate
cp5 = new ControlP5(this); //dynamic variable of control class
arduino.pinMode(3, Arduino.SERVO); //sets this output pin for byte transfer
Servo1 = cp5.addSlider(".").setRange(0,180).setValue(90).setPosition(110,134).setSize(270,25).setNumberOfTickMarks(270).setLabelVisible(true).setColorForeground(color(102,102,102)).setColorBackground(color(255,153,0)).setColorActive(color(102,102,102));
}
void draw()
{
//will fill alter
background(color(51,102,153)); //background of GUI color in RGB format
arduino.servoWrite(3, int(Servo1.getValue())); //get value from slider and write info to the pins
textSize(50);
text("Robot Arm", 300, 75); //Title
textSize(25);
text("Finger", 20, 125); //labels for each servo
textSize(18);
text("[Pin 3]", 25, 150); //Labels for the pins each servo is connected to
}
Answers
I don't have any experience with servos. However, your program is sending the value to your servos constantly since you are calling servoWrite() inside draw: https://processing.org/reference/draw_.html
Only write to the servo after the slide has been released. You will need the mouseReleased() method to accomplish that: https://processing.org/reference/mouseReleased_.html unless than controlP5 has a way to handle those events.
Related to command confirmation... one idea is that for every command you write to your arduino, your arduino will send back the received token. Processing will read this token and you could stored it (or update) a global variable that keeps track of the rotation state of the servo. However this has limitations. Imagine that you send a command and you block the servo from rotating using your finger. Using this idea, processing will update the servo rotation variable but the servo was not able to move during the writing operation.
Kf
So the only way to check if the servos actually get the data besides loooking to see if they move, is I have to intercept the info going back to the processing and store it in a variable OR I could check if the user has released the mouse?
Technically in industrial processes you will have an encoder attached to the shaft of the motor. The encoder is able to tell how many ticks, or degrees the motor has rotated independently of the controller driving the motor. So you could send the command to the controller and another command to read the encoder. That is the only secure way to keep track of motor position.
For servos, I don't know much about them. You can send the command to the arduino, but if the servos are not connected, the arduino will still output the current to drive them with no effect. The arduino will not be able to tell you back if the motor has changed positions (I am talking about simple servos). Unless you have any other idea or setup that allows you to accomplish this.
You have mousePressed, mouseReleased, mouseDragged functions availaable in Processing, if that is of any use to you for this task.
Kf