I am having a problem trying to control an Arduino servomotor via TD. When sending a signal (integer) via serial with a range of 0 to 180 (degrees), the servo recognises it as speed rather than degrees (it does not stop when it reaches the indicated degrees, the motor continues to turn).
Could anyone give me some clues as to how to solve the problem?
I don’t have that specific servo here so I can’t test for you, but perhaps this is because you are sending a new rotation value every frame from TD, instead of only when your slider changes value.
As a quick test you can try this example which uses a CHOP Execute DAT to only send a message to Arduino when you move the slider.
The method you suggest doesn’t work for me either; the motor keeps spinning out of control. It seems that now i’m controlling the speed instead the rotation…
I’m no expert, but after looking through some documentation, I think the problem lies in the motor pulse… is it possible? Using the Arduino examples, when I set the delay to 0.2 (the measurement in ms of the motor pulse), it seems that the rotation is in line with the degrees. The problem is that I don’t know how to transfer this from TouchDesigner (nor via the serial DAT, nor via firmata)… Looking at related tutorials, there are none that mention the motor pulse…
Could you tell me what type of servomotors you normally use in your TouchDesigner projects?
is the Pulse Width Modulation which is used by Arduino and the servo.h driver to send the correct pulse width to correspond to the desired shaft angle. This is fully handled by servo.h and therefor not something you have to worry about in your arduino code or TD
is the time it takes the servo to reach the new position. In most examples there’s like 15ms delay added after myservo.write(newposition) command - which is not strictly necessary but will make it more smooth.
The best place for this delay is in the arduino code. It is also already in the second code example you attached TD-control-servo.ino
Thanks to the link you provided, I finally found the root of the problem. The problem lies in the type of servo… Mine rotates in a range of 360 degrees, and these models move with speed values, not position values. If I want to work with angles (according to the link you provided), I have to work with motors that rotate in a range of 180 degrees.
If you know of any servos that rotate 360 degrees and move using position values (degrees), I would appreciate it if you could let me know the model.
360 or continuous servos can only be controlled by speed&direction. But if you have a version with built-in position sensor (often Hall effect sensors) then that servo also has an additional feedback wire, which can be used by your Arduino to read the current servo position.
There are many, but for instance this low budget one:
So then the code in your arduino becomes something like this:
Send a speed command via Servo.h.
Then start a loop in which you:
1)​Read the feedback wire using analogRead(). Then map that 0-1023 value from analogRead to 0-360 degrees, which outputs your current angle
2)​Compare that current angle to your target angle.
3)​Stop the motor once the feedback wire shows you’ve reached the target.
A nice exercise for your arduino skills - and you can probably find more extensive examples online.
But if you want direct position control, another (probably more accurate) option is to buy a steppermotor & driver. A stepper will rotate a specific amount of (fractional) degrees on a single pulse.
To get rid of servo problems (coarse steps, half circle rotation etc.), I changed for Dynamixel motors. 4096 steps, infinite rotations, speed and acceleration settings, easy command by Python.
here is a cheap one (in euros)
Downside is price, but not so much for simple ones and connection. I made a Python script running outside TD, sending orders (position, speed and acc) and receiving infos (actual position) by OSC.