Hello friends,
I am trying to control servo motor( has 180 degree motion) with Matlab and then simulink using atmega 32 on arduino UNO with serial communication, like a hardware in loop experiment.
I want to achieve maximum resolution as possible. If i control it from matlab - the max i can get is 180 /255 = 0.71 degrees , so according to my understanding, i can only move in multiples of 0.7 degrees ?? But When i enter any character from keyboard, it takes the ASCII value of it and instead of 0.7 it moves to around 1.4 degrees(255/180). Then Is it possible to achieve 1 degree?
Another thing normally if try to control it with arduino , then i can achieve better resolution then 0.7 deg according to calculations. am i correct?
I am really confused now.
Please correct me if i am doing wrong.Any comments will be greatly appreciated.
I'd doubt most hobby servos are really capable of that sort of repeatability - don't forget they're intended for human-in-the-loop applications.
However, you can get finer control if you use the writeMicroseconds method, which is automatically called from the usual Servo "write" method if you specify an "angle" greater than about 540.
Hello Awol,
Thanks for your reply.
But my problem is just to control the servo from matlab (hardware in loop) . I have a program flashed on arduino to convert the Ascii char input from the keyboard to be input as angle( in degrees). But with matlab, the input given to the servo is not using the arduino code( i am guessing).I have attached the code. Also if i want to give input like 34.6(float) , how to do it?
I'm sorry, but I'm maybe misunderstanding something basic here.
If your Arduino application does
int a = 600;
myServo.write (a);
, then the servo will receive a 600 microsecond PWM pulse in a 20ms frame time.
All you have to do is arrange for your matlab code to translate your 0-180 degree range into a roughly 540 to 2000 microsecond pulse length, and send that. You only need to send an integer, because that is all the write method accepts.
Back to the documentation for you. The Serial.available() method returns the number of bytes available to be read. It can NEVER return a negative value.
double inChar = Serial.read() ;
While you are looking at the documentation, pay some attention to what the various functions return. read() does NOT return a double.
Putting each { on it's own line, and using Tools + Auto format to clean up the random indenting, would make your code much easier to read.
The commented out code clearly has nothing to do with your problem. Don't post it. Figuring out what is relevant with all that stuff commented out is more difficult than it needs to be.
You have plenty of serial output that you didn't bother sharing. That wasn't very nice of you.
Paul - I am sorry for indentation as i was just started writing the code.
I wanted to know how to input angles like 130.5 or 30.9 because they are double. How to write and read double angles in arduino ? if serail library does not support it?
I want to use floating point values because my project requires accuracy, even though the servo can give accuracy upto 0.5 degrees but the command cannot be sended via arduino?
I wanted to know how to input angles like 130.5 or 30.9 because they are double. How to write and read double angles in arduino ? if serail library does not support it?
The serial port is used to send/receive bytes. Characters just happen to be byte sized. So, if the value you are sending is 130.5, the values that the serial port will see are '1', '3', '0', '.', and '5'. It is up to you to read all the characters that make up a packet, store them somewhere, and then convert that stored data to a numeric value.