Shop OBEX P1 Docs P2 Docs Learn Events
C++ interface for Parallax Servo Controller USB to Computer — Parallax Forums

C++ interface for Parallax Servo Controller USB to Computer

fastcarfastcar Posts: 2
edited 2010-08-24 16:52 in Robotics
Hi all,
I am a rather novice user of servo controller and would ask some noob question.
I have a Parallax Servo Controller USB and need to write a C++ code to control few servos to make my robot move.
I found a software (PSCI) on parallax website in which mouse is used to control the servos by using sliders on GUI.
Is there a library or a piece of code written in CPP/C which I can use to programatically control the servos on various channels to accomplish my task.
I tried my best to look for one for last 2 hours but couldn't find :confused: .
I would highly appreciate any help in this regard.
-Atul

Comments

  • FranklinFranklin Posts: 4,747
    edited 2010-08-22 19:50
    You won't find much c code on the Parallax site but since the controller uses serial commands sent over the usb port you can write code to do that in almost any language that can talk to a serial port.
  • Mike GMike G Posts: 2,702
    edited 2010-08-23 21:46
    I attached Windows C++ console app that sends the VER command to the the PSC. That should give you a good starting point. Make sure that you change the COMx string in the file to the COM port you have connected to the PSC.

    This forum would not accept a cpp file extension so I changed it to txt.
  • fastcarfastcar Posts: 2
    edited 2010-08-24 16:52
    Thank you everyone for the replies. I found out that there is lot of code out there for serial port communication using boost asio. Advantage is platform independence. Based on many sources and parallax microcontroller manual, I wrote a small piece of code that talks with the microcontroller and posting it here, so that it helps people in future.
    #include <iostream>
    #include <boost/asio.hpp>
    
    using namespace std;
    using namespace boost;
    
    int main(int argc, char* argv[])
    {
        try 
    	{
       		boost::asio::io_service io;
    		boost::asio::serial_port serial(io,"COM6");// set your own com port, linux people can use /dev/ttyS1 or something like that
    		serial.set_option(boost::asio::serial_port_base::baud_rate(2400));//I found parallax controller works at 2400 baud rate
    		
    		std::string comd = "!SC";//preamble
    		char c = 5;//set channel
    		char r = 0;//set ramp value		
    		char l = 250;//lower		
    		char h = 0;//higher
    		char ret = '\x0D';//end of line character
    
    //prepare the command as below
    		comd += c;
    		comd += r;
    		comd += l;
    		comd += h;
    		comd += ret; 
    
    //run the commandboost::asio::write(serial,boost::asio::buffer(comd.c_str(),comd.size()));	
    //sleep for command to execute
    		Sleep(10);
    //read the data from controller and print 
    		vector<uint8_t> buf(8);
    		try
    		{ 
    			size_t len = boost::asio::read(serial, boost::asio::buffer(buf));		  
    			cout<<"Data returned by servo:"<<endl;
    			cout<<"Channel="<<(int)buf.at(3)<<"\n";
    			cout<<"Ramp="<<(int)buf.at(4)<<"\n";
    			cout<<"L="<<(int)buf.at(5)<<"\n";
    			cout<<"H="<<(int)buf.at(6)<<"\n";			
    		}
    		catch (boost::system::system_error &err)
    		{
    			cout<<"Error in reading data from the servo:"<<err.what()<<"\n";
    		}
    //close the connection
    		serial.close();
    
        } 
    	catch(boost::system::system_error& e)
        {
            cout<<"Error: "<<e.what()<<endl;
            return 1;
        }
    }
    
Sign In or Register to comment.