Jump to content
niksen

Servo control

Recommended Posts

Hello, my name is Alex and i'm MSP430 beginner.

 

I would like to controll 2 servo from my MSP board. Before i have experience with Arduino, so i'v donwloaded Energia software and was trying to compile Arduino Code to get it work on MSP430 - but still no luck.

 

I saw couple of examples of Servo controll code on this forum but i guess they can not be compiled through Energia. this include - was wondering me.

 

Please advice from where to start to build Servo control from MSP430 board.

 

Here is my code for Arduino project that works well, but not on MSP430 board:

 

 

#include 

Servo servo1; 
Servo servo2;

int minPulse     =  600;  // minimum servo position
int maxPulse     =  2400; // maximum servo position
int turnRate     =  40;  // servo turn rate increment (larger value, faster rate)
int refreshTime  =  20;   // time (ms) between pulses (50Hz)
int ledPin = 13; //laser stick

/** The Arduino will calculate these values for you **/
int centerServo;         // center servo position
int pulseWidthServo1; 
int pulseWidthServo2; // servo pulse width
int moveServo;           // raw user input
long lastPulseServo1   = 0;    // recorded time (ms) of the last pulse
long lastPulseServo2   = 0;

void setup() {
 servo1.attach(2);  
 servo2.attach(7);

 centerServo = maxPulse - ((maxPulse - minPulse)/2);
 pulseWidthServo1 = centerServo;   // Give the servo a starting point (or it floats)
 pulseWidthServo2 = centerServo;

 pinMode(ledPin, OUTPUT);
 digitalWrite(ledPin, HIGH);


 Serial.begin(9600);
 Serial.println("      Ready");
 Serial.println("Press < or > to move, spacebar to center, M, N to move up/down");
 Serial.println();
}

void loop() {
 // wait for serial input
 if (Serial.available() > 0) {
   // read the incoming byte:
   moveServo = Serial.read();

   // ASCII '<' is 44, ASCII '>' is 46 (comma and period, really)
   if (moveServo == 44) { servo1.write(pulseWidthServo1 = pulseWidthServo1 - turnRate); }
   if (moveServo == 46) { servo1.write(pulseWidthServo1 = pulseWidthServo1 + turnRate); }
   if (moveServo == 32) { pulseWidthServo1 = centerServo; }
   if (moveServo == 32) { pulseWidthServo2 = centerServo; }
   if (moveServo == 77) { servo2.write(pulseWidthServo2 = pulseWidthServo2 - turnRate); }
   if (moveServo == 78) { servo2.write(pulseWidthServo2 = pulseWidthServo2 + turnRate); }

   // stop servo 2 pulse at min and max
   if (pulseWidthServo1 > maxPulse) { pulseWidthServo1 = maxPulse; }
   if (pulseWidthServo1 < minPulse) { pulseWidthServo1 = minPulse; }

   // stop servo 9 pulse at min and max
   if (pulseWidthServo2 > maxPulse) { pulseWidthServo2 = maxPulse; }
   if (pulseWidthServo2 < minPulse) { pulseWidthServo2 = minPulse; }

   // print pulseWidth back to the Serial Monitor (uncomment to debug)
    Serial.print("Pulse Width Servo1: ");
    Serial.print(pulseWidthServo1);
    Serial.println("us");   // microseconds

    Serial.print("Pulse Width Servo2: ");
    Serial.print(pulseWidthServo2);
    Serial.println("us");   // microseconds
 }  
}

 

Error i receive is:

 

core.a(TimerSerial.cpp.o): In function `TimerSerial__TxIsr':
/Applications/Energia.app/Contents/Resources/Java/hardware/msp430/cores/msp430/TimerSerial.cpp:202: multiple definition of `__isr_9'
Servo/Servo.cpp.o:/Applications/Energia.app/Contents/Resources/Java/hardware/msp430/libraries/Servo/Servo.cpp:87: first defined here
collect2: ld returned 1 exit status

 

Please advice

Share this post


Link to post
Share on other sites

You probably have a MSP430G2452 in socket on the LaunchPad. This chip does not have a Hardware Serial peripheral and so it uses a software serial instead. The problem is that both Servo and TimerSerial use the Timer resource on the chip and that is what the compiler is complaining about.

 

Easiest way round this is using a MSP430G2553 which has a Hardware Serial. If you do not have one then you can get free samples from TI here: http://www.ti.com/product/msp430g2553. You want the MSP430G2553IN20.

 

If you switch to a G2553 then please read these Wikis for correct J3 jumper settings:

https://github.com/energia/Energia/wiki ... munication

https://github.com/energia/Energia/wiki/Hardware

 

Robert

Share this post


Link to post
Share on other sites

Guys, thanks for answers.

 

Here is my small and simple code for Energia:

 

It should wait for key pressed with ASCII 44 and move servo connected to 9 pin 180 degree. It actually moves Servo, but then Serial Monitor is not responsible - Energia is crashed, no compiling errors.

 

Please help

 

#include 

Servo myservo;  // create servo object to control a servo 
               // a maximum of eight servo objects can be created 

int pos = 0;    // variable to store the servo position 
int moveServo = 0;

void setup() 
{ 
 myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
 Serial.begin(9600);
} 


void loop() 
{ 

 if (Serial.available() > 0) {
   // read the incoming byte:
   moveServo = Serial.read();

   if (moveServo == char(44)) { 
   //myservo.write(50);
   Serial.println("44 is pressed");
   for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
       {                                  // in steps of 1 degree 
              myservo.write(pos);              // tell servo to go to position in variable 'pos' 
              delay(15);                       // waits 15ms for the servo to reach the position 
       }
   }


 }
} 

Share this post


Link to post
Share on other sites

Can you explain what you mean with "Crash"? Does the serial monitor not respond? Can you still type in the serial monitor field next to send? Can you still type in Energia?

 

What operating system are you using? Linux, Max, Windows?

 

The for loop takes 2700 ms and while it is executing that loop the Serial monitor will not be respond during that time.

 

I don't have a Servo motor to test with right now but when I run the Sketch, I can continue to type "," (char 44) without a problem.

 

Robert

Share this post


Link to post
Share on other sites

Hi Alex,

Looking at your last code:

It loops waiting for char(44) or ','

Then when ',' arrives it moves the servo from 0 degrees to 180 degrees in increments of 1 degree

every 15 milliseconds.

After that it continues doing the servo movement jumping from 180 degrees to 0 degrees without delay

and repeating the 0 to 180 movement in steps of 1 degree every 15 milliseconds.

Nothing stops the servo.

 

Will be nice to move back the servo to 0 degrees and asign a diferent value to moveServo.

In this way the servo will be stopped in 0 degrees waiting for the next ',' ...

 

May be it solves the rare behavior.

Hope to be useful. if not: :silent:

Share this post


Link to post
Share on other sites

How about this, then? (I'm new at this, so I'd like to know if I'm doing it proper)

 

 

 

#include <Servo.h>
 
Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created
 
int posm = 0;   // variable to store the servo position
int posz = 0;
int moveServo = 0;
int serswpdeg = 90; // number of degrees to sweep
 
void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  Serial.begin(9600);
}
 
 
void loop()
{
 
  if (Serial.available() > 0) {
    // read the incoming byte:
    moveServo = Serial.read();
    
    if (moveServo == char(44)) {
    //myservo.write(50);
    Serial.println(", is pressed");
    for(posm = 0; posm < serswpdeg; posm += 1)  // goes from 0 degrees to specified sweep degrees
        {                                  // in steps of 1 degree
               myservo.write(posm);              // tell servo to go to position in variable 'posm'
               delay(15);                       // waits 15ms for the servo to reach the position
        }
        
    for (posz = serswpdeg; posz > 0; posz -= 1) // returns servo to 0 degrees
        {
          myservo.write(posz);
          delay(15);
        }
    }
    
    
  }
}

Share this post


Link to post
Share on other sites

how can we convert these Strange Character inputs ? When i Serial.read() and on Serial Monitor i send a number i can not use that number. Do we have to make a conversion or something? ( I have tried Serial.parseInt() too, doesn't work as in Arduino examples :/ )

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×