Hi,
I needed to move servos in Linux and created this very simple starter application. It works well and is built on the standard libraries (iostream, fstream and ctime). It should give you some ideas and can be expanded to use the other protocols. It also has a wait(sec) function, which I have to admit I found somewhere on the internet. (but it’s trick!) It’s a demo program for two servos on the Micro Maestro 6. Note: this is a C++ application, so compile with g++. I have tested it on a couple of desktops running Ubuntu, and it also runs on the Gumstix Overo (ARM9) after cross-compiling. Using the Mini SSC protocol simplifies basic servo movement control.
Hope it helps…
Mark
/*
* micro6.cpp
*
* Created on: Jul 3, 2010
* Author: mwille
*
* Description: Sample program using simple filestream object and
* Mini SSC protocol.
*/
#include <iostream>
#include <fstream>
#include <ctime>
using namespace std;
#define USBDEV "/dev/ttyACM0"
void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
}
int main(void)
{
int i,j;
//small array holds mini ssc command set- (xFF, servo id, servo position)
int SetTarget[3]= {255,0,0}; //(should probably be char array technically).
fstream micro6; //instantiate fstream object
micro6.open(USBDEV,ios::out); //open for output
if(micro6.fail())
{
cout << "Did not open Micro6!" << endl;
return 0;
}else
{
cout << "Opened Micro6 Device!" << endl;
}
// first loop controls servo 0
for(j=0;j<3;j++)
{
SetTarget[1] = 0;
cout << "Sending Position Data to Servo 0" << endl;
switch(j) // change servo 0 position each iteration
{
case 0: SetTarget[2] = 0;
cout << SetTarget[0]<< " " << SetTarget[1] << " " << SetTarget[2] << endl;
break;
case 1: SetTarget[2] = 127;
cout << SetTarget[0]<< " " << SetTarget[1] << " " << SetTarget[2] << endl;
break;
case 2: SetTarget[2] = 254;
cout << SetTarget[0]<< " " << SetTarget[1] << " " << SetTarget[2] << endl;
break;
}
//sending commands - (loop for sending each value)
for(i=0;i<3;i++)
{
micro6.put(SetTarget[i]);
}
micro6.flush(); //**** NOTE: very important to flush(push) command set from buffer.
wait(1);
SetTarget[1] = 1; //change to servo 1
cout << "Sending Position Data to Servo 1" << endl;
switch(j)
{
case 0: SetTarget[2]= 254;
cout << SetTarget[0]<< " " << SetTarget[1] << " " << SetTarget[2] << endl;
break;
case 1: SetTarget[2]= 127;
cout << SetTarget[0]<< " " << SetTarget[1] << " " << SetTarget[2] << endl;
break;
case 2: SetTarget[2]= 0;
cout << SetTarget[0]<< " " << SetTarget[1] << " " << SetTarget[2] << endl;
break;
}
//sending commands
for(i=0;i<3;i++)
{
micro6.put(SetTarget[i]);
}
micro6.flush();
wait(1);
}
}
cout << "Closing the Micro6..." << endl;
//closing micro6
micro6.close(); //**** NOTE: must close the filestream when done!
cout << "End of program..." << endl;
return 0;
}