india
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« on: July 05, 2012, 06:38:39 am » |
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' } } }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 226
Posts: 14101
Lua rocks!
|
 |
« Reply #1 on: July 05, 2012, 06:47:28 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 334
Posts: 36457
Seattle, WA USA
|
 |
« Reply #2 on: July 05, 2012, 07:03:23 am » |
Serial.begin(9600); We're out of the stone age. Get the speed up. 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. 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.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Tesla Member
Karma: 76
Posts: 6849
Arduino rocks
|
 |
« Reply #3 on: July 05, 2012, 08:58:58 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
South Texas
Offline
God Member
Karma: 8
Posts: 978
|
 |
« Reply #4 on: July 05, 2012, 12:57:00 pm » |
Serial speed - maybe he has other hardware that needs this, and besides, it really is not the issue, just your personal preference.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 2
Posts: 212
|
 |
« Reply #5 on: July 05, 2012, 04:02:41 pm » |
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
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Tesla Member
Karma: 76
Posts: 6849
Arduino rocks
|
 |
« Reply #6 on: July 05, 2012, 09:35:32 pm » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 2
Posts: 212
|
 |
« Reply #7 on: July 06, 2012, 06:34:45 am » |
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)
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 143
Posts: 19368
I don't think you connected the grounds, Dave.
|
 |
« Reply #8 on: July 06, 2012, 06:38:31 am » |
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.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 334
Posts: 36457
Seattle, WA USA
|
 |
« Reply #9 on: July 06, 2012, 09:27:52 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
india
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #10 on: July 06, 2012, 05:39:24 pm » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
india
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #11 on: July 06, 2012, 05:41:42 pm » |
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' } } }
|
|
|
|
|
Logged
|
|
|
|
|
india
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #12 on: July 06, 2012, 06:28:19 pm » |
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 is it possible from ms visual cpp that im using?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 226
Posts: 14101
Lua rocks!
|
 |
« Reply #13 on: July 06, 2012, 07:15:11 pm » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 226
Posts: 14101
Lua rocks!
|
 |
« Reply #14 on: July 06, 2012, 07:16:41 pm » |
|
|
|
|
|
Logged
|
|
|
|
|
|