Go Down

Topic: servo moving very slow,only works for delay of 1500 or more (Read 2664 times) previous topic - next topic

anuran

im trying to rotate the servo motor by using this code,but this seems to be very slow how can i fast this thing up,so as to imitate mouse motion in real time

following code for vc++
#include "stdafx.h"
#include "conio.h"
#include "iostream"
#include "windows.h"
#include "WinDef.h"
#include "WinUser.h"
#include "windowsx.h"
#using <mscorlib.dll>
#pragma comment(lib, "gdi32.lib")
#pragma comment(lib, "User32.lib")

using namespace System;
using namespace System::IO::Ports;

int main(array<system::string ^=""> ^args)
{
String^ answer; 
String^ answernew; 
POINT coord;   
answernew="hello";   
// arduino settings

SerialPort^ arduino;   
arduino = gcnew SerialPort("COM5", 9600);   
// open port   
try{   
arduino->Open();   
while(1)
{      
GetCursorPos(&coord);   
answer=Convert::ToString(int(coord.x/7.80))->PadLeft(3,'0');
//7.80 is scaleing factor of screenwidth by max angle of servo motor

//if mouse moves to new location   
if(String::Compare(answer,answernew))   
{
answernew=answer;   
Sleep(1500);   //servo moving only after this delay or more
//1 is motor no im trying to control    
arduino->Write(String::Concat("1",answernew));
    }
}   
// close port to arduino   
arduino->Close();
}   
catch (IO::IOException^ e  )   {
Console::WriteLine(e->GetType()->Name+": Port is not ready");
}   
catch (ArgumentException^ e)   {   
Console::WriteLine(e->GetType()->Name+": incorrect port name syntax, must start with COM/com");   
}   
Sleep(500);   
return 0;
}

code for arduino
#include <servo.h>
Servo myservo1;  // create servo object to control a servo
Servo myservo2;
Servo myservo3;// a maximum of eight servo objects can be created 

void setup()

myservo1.attach(7 );
myservo2.attach(8 );
myservo3.attach(9 );
// attaches the servo on pin 9 to the servo object 

Serial.begin(9600);


void loop()
{
if(Serial.available()>0)   
{
int pos=Serial.parseInt();     
int servo=pos/1000;
pos=pos%1000;
Serial.print(pos);
      if(pos>0&&pos<176)     
switch(servo)     
{     
case 1:myservo1.write(pos);break;
      case 2:myservo2.write(pos);break;
      case 3:myservo3.write(pos);break;                  // tell servo to go to position in variable 'pos'     
}   
}                         
}

Nick Gammon

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

PaulS

Code: [Select]
Serial.begin(9600);
We're out of the stone age. Get the speed up.

Code: [Select]
int servo=pos/1000;
pos=pos%1000;
Serial.print(pos);

printing data takes time. If you need speed, quit doing things that slow you down.

Code: [Select]
int pos=Serial.parseInt();     
Sending two bytes - the servo # and the servo position - would be orders of magnitude faster then sending a string that needs to be converted back to an int.

MarkT

Definitely use Serial.begin(115200) - 12 times faster!

You might do better to encode the position into a single byte, then send this single byte to change the servo - rather than an ascii representation.
[ I won't respond to messages, use the forum please ]

kf2qd

Serial speed - maybe he has other hardware that needs this, and besides, it really is not the issue, just your personal preference.

PGT

not sure if this is the case, but if you use mosfets to control your motor, there needs to be a resistor between the pinout and the mosfet control.
If you give a mosfet full power its inner fieldeffect doesnt do a good job of discharging resulting in less fast switching off turned on mosfets
resulting in previous cycle of magnets not powering off fast enough, and slow motor performance.
Like a transistor a mosfet only requires little power to drive their output

it seams to be an error that happens to a lot of people

MarkT


not sure if this is the case, but if you use mosfets to control your motor, there needs to be a resistor between the pinout and the mosfet control.
If you give a mosfet full power its inner fieldeffect doesnt do a good job of discharging resulting in less fast switching off turned on mosfets
resulting in previous cycle of magnets not powering off fast enough, and slow motor performance.
Like a transistor a mosfet only requires little power to drive their output

it seams to be an error that happens to a lot of people



I am having a lot of trouble understanding any of this posting - can you rephrase?
[ I won't respond to messages, use the forum please ]

PGT

ehm you do use Mosfets ?

well, a mosfet has 3 pins, lets suppose a pin is connected to 12V (V in) and another pin to your arduino to control the mosfet (gate), and another (V out)to your motors magnet from where it goes to ground.

The principle of the Mosfet is that a charge in the gate creates a tunnel inside the Mosfet so the magnet inside your motor gets power.

Mosfet however require little charge to control that tunnel, this gate charge is tiny compared to the their (V out) power. If you overwhelm it with a lot of charge, then it takes more time before the charge is gone (and you might even destroy the Mosfet if you give it to much on the gate).

If it takes more time to discharge then it means it doesnt switch off as fast as you like.

So that would result that inside a motor more magnets might still be turned on.

Resulting in weak motor performance, for both speed and torsion


To resolve use resistors on the gate (its also protective for the Mosfet)





AWOL

Quote
ehm you do use Mosfets ?

The OP uses the Servo library, so I think it is safe to assume that MOSFETs are not driven directly.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

Quote
Serial speed - maybe he has other hardware that needs this, and besides, it really is not the issue, just your personal preference.

The Serial speed IS related to the issue. It takes time to send serial data. The amount of time depends on the baud rate. After the data is received, it needs to be parsed. The parseInt function is waiting a while to see if there will be more data. If not, it eventually ends, and the servo can be moved.

Meanwhile, the PC application is having to wait for the serial data to be received, the parseInt function to determine that there will be no more data, and for the servo to move. That is why the long sleep time is required.

Sending something from the PC that lets Serial.parseInt() know that the end of the int has arrived, such as a space, would speed up parseInt. Setting a higher speed would decrease the time that the serial data took to arrive, too.

Nothing will change how long the servo takes to move, but I got the impression that the speed of the servo wasn't the issue. It was how long it took before the PC could send a new position that was the issue.

anuran

sorry for the missing code snipplet.im using basic circuit diagram as .
i tried removing print statements,but still the same,does increasing baud rate has any harmful effect on arduino uno chip im using?

anuran


im trying to rotate the servo motor by using this code,but this seems to be very slow how can i fast this thing up,so as to imitate mouse motion in real time
Code: [Select]

following code for vc++
#include "stdafx.h"
#include "conio.h"
#include "iostream"
#include "windows.h"
#include "WinDef.h"
#include "WinUser.h"
#include "windowsx.h"
#using <mscorlib.dll>
#pragma comment(lib, "gdi32.lib")
#pragma comment(lib, "User32.lib")

using namespace System;
using namespace System::IO::Ports;

int main(array<system::string ^=""> ^args)
{
String^ answer; 
String^ answernew; 
POINT coord;   
answernew="hello";
// arduino settings

SerialPort^ arduino;
arduino = gcnew SerialPort("COM5", 9600);
// open port
try{
arduino->Open();
while(1)
{   
GetCursorPos(&coord);
answer=Convert::ToString(int(coord.x/7.80))->PadLeft(3,'0');
//7.80 is scaleing factor of screenwidth by max angle of servo motor

//if mouse moves to new location
if(String::Compare(answer,answernew))
{
answernew=answer;
Sleep(1500); //servo moving only after this delay or more
//1 is motor no im trying to control
arduino->Write(String::Concat("1",answernew));
}
}
// close port to arduino
arduino->Close();
}
catch (IO::IOException^ e  ) {
Console::WriteLine(e->GetType()->Name+": Port is not ready");
}
catch (ArgumentException^ e) {
Console::WriteLine(e->GetType()->Name+": incorrect port name syntax, must start with COM/com");
}
Sleep(500);   
return 0;
}

code for arduino
Code: [Select]

#include <servo.h>
Servo myservo1;  // create servo object to control a servo
Servo myservo2;
Servo myservo3;// a maximum of eight servo objects can be created 

void setup()

myservo1.attach(7 );
myservo2.attach(8 );
myservo3.attach(9 );
// attaches the servo on pin 9 to the servo object 

Serial.begin(9600);


void loop()
{
if(Serial.available()>0)   
{
int pos=Serial.parseInt();     
int servo=pos/1000;
pos=pos%1000;
Serial.print(pos);
      if(pos>0&&pos<176)     
switch(servo)     
{     
case 1:myservo1.write(pos);break;
      case 2:myservo2.write(pos);break;
      case 3:myservo3.write(pos);break;                  // tell servo to go to position in variable 'pos'     
}   
}                         
}


anuran

i tried using increasing the baud rate,deleted print line,and using two Serial.parseInt(),but still there has to be a delay of 1500 or more otherwise,its not working after first angle.i want to do something like this http://www.youtube.com/watch?v=lFvQRWXX24U is it possible from ms visual cpp that im using?

Nick Gammon


i tried removing print statements,but still the same,does increasing baud rate has any harmful effect on arduino uno chip im using?


No, why would it?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up