Go Down

Topic: pd (puredata) to the arduino (Read 741 times) previous topic - next topic

armadillo

hi all, I will like to control a servo motor connected in my arduino board via a pd patch using [comport] or any other object that could be good for the case.

since the examples i found are for sending data to pd from sensors connected to the arduino and what i need is precisely working in the other way round (pd -> arduino) i decided to open a thread hoping someone could share the matching basic examples from a pd patch and its corresponding arduino patch receiving the control data.

i hope to manage to do it soon, if so i will put them both here below, cheers! /a  
                                                                                       |
                                                                                       |                                                                                      
                                                                                       V

armadillo

not much to tell but some juicy threads on the pd-list so far:

http://elists.resynthesize.com/pd-list/2006/05/1602280/

i.e.

do you happen to have an example on how to send data to the arduino from pd??


armadillo

here is a .pde program that i have tried with one servo, it did work in a very unprecise way since the servo moved almost randomly..but at least moved when i scrolled data from within pd puredata.

could you test it and make it better?

here it is:

/*
  Two-servo control from an analog input

  Moves two servos based on the value of one analog sensor,
  in  opposite directions.

  The minimum (minPulse) and maxiumum (maxPuluse) values
  will be different depending on your specific servo motor.
  Ideally, it should be between 1 and 2 milliseconds, but in practice,
  0.5 - 2.5 milliseconds works well for me.
  Try different values to see what numbers are best for you.

  by Tom Igoe
  Created 28 Jan. 2006
  Repurposed for Dual Servos by Carlyn Maw
  24 Mar. 2006
  Modified for serial control by Martin PEach 20060603
  Send data via serial port at 19200 baud with angle as 0-15 in low nibble and
servo number in high nibble.
  This does 2 servos but could be expanded to do up to 16.

*/

int servoPinL = 2; // Control pin for servo motor - L
int servoPinR = 3; // Control pin for servo motor - R
int pulseL = 0; // Amount to pulse the servoL
int pulseR = 0; // Amount to pulse the servoR
int delayValueMax = 20000; // the 2 millisecond maxiumum pulse range for the
servo
int delayValue = 20000; // the actual value the code should wait, determined
later
int analogValue = 0; // the value returned from the analog sensor
int analogPin = 0; // the analog pin that the sensor's on
char c, servoNumber, servoValue;

void setup(void)
{
   pinMode(servoPinL, OUTPUT); // Set servo pins as an output pins
   pinMode(servoPinR, OUTPUT);
   pulseL = 1000; // Set the motor position values to the minimum
   pulseR = 1000;
   beginSerial(19200);
}

void loop(void)
{
   while (serialAvailable != 0)
   {
     c = serialRead();
     servoNumber = (c & 0xF0) >> 4; // servo ID is in high nibble
     servoValue = (c & 0x0F); // servo angle is in low nibble
     if (servoNumber == 0) pulseL = (servoValue*62) + 1000; // make a pulse
1000-2000 us
     else if (servoNumber == 1) pulseR = (servoValue*62) + 1000; // make a
pulse 1000-2000 us
   }

   digitalWrite(servoPinL, HIGH); // Turn the L motor on
   delayMicroseconds(pulseL); // Length of the pulse sets the motor position
   digitalWrite(servoPinL, LOW); // Turn the L motor off

   digitalWrite(servoPinR, HIGH); // Turn the R motor on
   delayMicroseconds(pulseR); // Length of the pulse sets the motor position
   digitalWrite(servoPinR, LOW); // Turn the R motor off

   delayValue = delayValueMax - pulseL - pulseR; // determine how much time
you
have before the 20 ms is over...
   delayMicroseconds(delayValue); // 20 millisecond delay is needed between
pulses to keep the servos in sync
}




a simple pd patch will be this one:

#N canvas 0 22 466 316 10;
#X floatatom 179 166 5 0 0 0 - - -;
#X obj 159 239 print;
#X obj 160 216 spigot;
#X msg 179 189 1;
#X msg 209 188 0;
#X obj 181 85 key;
#X floatatom 25 52 3 0 0 0 - - -;
#X obj 25 26 keyname;
#X symbolatom 68 52 10 0 0 0 - - -;
#X obj 25 77 bng 15 50 10 0 empty empty empty 0 -6 0 8 -262144 -1 -1
;
#X floatatom 180 110 5 0 0 0 - - -;
#X obj 179 139 comport 3 19200;
#X obj 275 110 float;
#X obj 275 87 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1
-1;
#X floatatom 289 60 5 0 0 0 - - -;
#X msg 341 34 1;
#X msg 342 53 2;
#X msg 342 72 3;
#X msg 342 92 4;
#X msg 342 111 5;
#X msg 342 130 6;
#X msg 343 149 7;
#X msg 343 168 8;
#X msg 343 188 9;
#X msg 343 207 11;
#X msg 342 226 12;
#X msg 343 245 13;
#X msg 343 265 14;
#X msg 343 284 15;
#X connect 2 0 1 0;
#X connect 3 0 2 1;
#X connect 4 0 2 1;
#X connect 5 0 10 0;
#X connect 6 0 9 0;
#X connect 6 0 11 0;
#X connect 7 0 6 0;
#X connect 7 1 8 0;
#X connect 10 0 11 0;
#X connect 11 0 3 0;
#X connect 11 0 0 0;
#X connect 12 0 11 0;
#X connect 13 0 12 0;
#X connect 14 0 12 1;
#X connect 14 0 13 0;
#X connect 15 0 12 1;
#X connect 16 0 12 1;
#X connect 17 0 12 1;
#X connect 18 0 12 1;
#X connect 19 0 12 1;
#X connect 20 0 12 1;
#X connect 21 0 12 1;
#X connect 22 0 12 1;
#X connect 23 0 12 1;
#X connect 24 0 12 1;
#X connect 25 0 12 1;
#X connect 26 0 12 1;
#X connect 27 0 12 1;
#X connect 28 0 12 1;


dont forget that this is still in very early stages, please if you have done this step already (controlling servos from within pd) please share your discoveries.

tchuss!
/a

Go Up