If you trying using this code it will do nothing. That is there is nothing in the loop function. However, this code contains the sub-functions needed to communicate with the 24-port servo controller that Pololu sells. This only exploits movement. If you look into different command numbers you can also control the speed of the servos and access other features available to the pololu protocol.

#include <SoftwareSerial.h>

SoftwareSerial Servos(10, 11); // RX is digital pin 10 (connect to TX of other device) * TX is digital pin 11 (connect to RX of other device)--we only need to connect pin 11
  
void talk(int command, int servo, int data1, int data2);
void rotate(int servo,int angle);
void initialize(void);//sets initial servo positons

void setup(){
  initialize();
  
  Serial.begin(9600);//set up serial communication to monitor
  Servos.begin(9600);
}
void loop(){

}
//////////////////////
////////////////////
//Servo Controller
void talk(int command, int servo, int data1, int data2){  
/*
This uses the pololu protocol for controlling the servos--the shorting block must be removed for 
these commands to work--I say this twice, because it is important.
The data 1 is lsb and data 2 is msb, it ignores the first bit, and the units in 1/4 micro seconds.
For example, the code uses 0x70, 0x2E->in binary 00111000, 00101110 to indicate 6000 1/4 micro seconds, which is  lsb:  0111000, msb:  
0101110, but together in proper oder reads as 0x1770.  
This is further 1500 microseconds which is the 90 degree position--The conversion from degrees to microseconds is covered below.
*/
  Servos.write(0xAA);  //start byte
  Servos.write(0x0C);  //device id which is 12 in decimal for the 24 serial servo controller
  Servos.write(command); //command number
  Servos.write((byte)servo);  //servo number (0-23)
  Servos.write((byte)data1);  //data 1--lsb--lowest 7 bits
  Servos.write((byte)data2);  //data 2--msb--highest 7 bits
  delay(75);
}

/*  I made this cheat sheet of postions and inputs until a proper conversion was found,
it discusses how to convert from degrees to microseconds and then to proper bits need to be sent:

Microseconds are found by multiplying by 11 and then adding 500 to degrees--remember that the controller
takes ins quarter of a microsecond (four times this number) though and discrads the 
first bit of data of each data byte (i.e. send the lowest 7 bits with a leading 0 and then
the highest 7 bits with a leading 0)

The afore said covnversion is found by noting that the signals to the servo controller vary
from 500 (0) to 2500 (180).  Thus to get zero subtract 500.  To get everything else divide 2000 by 180.
The result is not exactly 11, but the error is very slight.

(microseconds--least significant bit, most significant bit)
500--0X50,0X0F->about 0 degrees
700--0X0F,0X15->18
750--0X38,0X17->22
800--0X00,0X19->27
1000--0X20,0X1F->45
1200--0X40,0X25->63
1500--0X70,0X2E->about 90 degrees
1800--0X20,0X38->118
2500--0X10,0X4E->about 180 degrees
*/

/////////////////////////////////////////////////////

void rotate(int servo,int angle){
  //Uses talk() to send servo to some angle
  int converted=(angle*11+500)*4;//in quarter microseconds
  int data2=converted>>7;//most significant bit
  int data1=converted&0x7F;//least significant bit
  
  talk(0x04, servo, data1, data2);//sends data
}

void initialize(void){
  //set all servos to an inital 90 degrees.  Allows for rotation in either direction of 90 degrees
  for(int i=0; i<targets; i++){
    rotate(i,90);
  }
  delay(250);
}
 
classes/designandproto/me155a_groups/servo_controller.txt · Last modified: 2013/03/08 20:24 by nmitchell
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki