Hi everyone i just made an account in this forum...please excuse me if my English is kinda bad or i don't follow forum rules...
I am trying to control a robotic arm via kinect and arduino duemilanove using serial port of arduino....so far i have succeed to control one servomotor of arm but when i try to control two servomotor's i cant control it at all and i believe it is because i don't fully understand how serial port works...for the programing of kinect i use Microsoft 2010 C# express and for the arduino i use the environment that this site provide us the code of arduino is the following...
P.S. the project is that through kinect that seeing my movement of my body to transfer it to robotic arm through arduino serial port
#include <math.h>
#include <Servo.h>
int i=0;
int j=0;
int powc=1;
byte RIGHT[20];
byte LEFT[20];
double angleR=0;
double angleL=0;
int aposR=0;
int aposL=0;
int mpoR;
int mpoL;
Servo a;
Servo b;
void setup()
{
// Turn the Serial Protocol ON
Serial.begin(9600);
a.attach(9);
b.attach(8);
}
void loop() {
/* check if data has been sent from the computer: */
while (Serial.available() > 0) {
RIGHT[i]=Serial.read();
if(RIGHT[i]=='a')
{
aposR=i;
for(int c=0; c<aposR; c++ )
{
angleR=angleR+(RIGHT[aposR-c-1] - '0') * powc;
powc=powc*10;
}
powc=1;
}
LEFT[j]=Serial.read();
if(LEFT[j]=='b')
{
aposL=j;
for (int t=0; t<aposL;t++)
{
angleL=angleL+(LEFT[aposL-t-1]- '0')*powc;
powc=powc*10;
}
}
powc=1;
if(RIGHT[i]=='m')
{
mpoR=i;
int motor=RIGHT[mpoR-1] -'0';
if (motor==1)
{a.write(angleR);}
Serial.print(angleR);
Serial.print(" degrees on motor ");
Serial.print(motor);
Serial.print(" ");
Serial.println();
i=0;
RIGHT[i]='/0';
angleR=0;
}
if(LEFT[j]=='m')
{
mpoL=j;
int motor=LEFT[mpoL-1] -'0';
if (motor==2)
{b.write(angleL);}
Serial.print(angleL);
Serial.print(" degrees on motor ");
Serial.print(motor);
Serial.print(" ");
Serial.println();
j=0;
LEFT[j]='/0';
angleL=0;
}
i++;
j++;
}
}
And the part of the code in C# that i am sending in the serial port of arduino is this
int right= GetJointAngle(skeleton.Joints[JointType.ShoulderRight], skeleton.Joints[JointType.ElbowRight], skeleton.Joints[JointType.WristRight]);
PixelDepth.Text = string.Format("Angle Right hand {0}",right);
canvasSkeleton.Children.Add(CreateFigure(skeleton, userBrush, joints));
string a = right.ToString();
port.Write(a);
port.WriteLine("a1m");
this is when i move my right arm
[/b]
So far is good but when i try add code so i can move at the same time my right and my left hand to control 2 servomotor each the arm is doing random things....any help why is this happening ??
The code for the movement of my left hand is the following
int left = GetAngle(skeleton.Joints[JointType.ShoulderLeft], skeleton.Joints[JointType.ElbowLeft], skeleton.Joints[JointType.WristLeft]);
PixelDepth.Text = string.Format("Angle Left hand {0}", left);
canvasSkeleton.Children.Add(CreateFigure(skeleton, userBrush, joints));
string b = left.ToString();
port.Write(b);
port.WriteLine("b2m");
so far i have succeed to control one servomotor of arm but when i try to control two servomotor's i cant control it at all and i believe it is because i don't fully understand how serial port works.
What are you using for servo power? Most multi servo issues arise from an inadequate power supply for the servos. Trying to power servos from the arduino generally doesn't work.
When there is serial data to read, read one character, and store it in RIGHT. Then, read the next character, even if there isn't one, and store it in LEFT. Does that really make sense?
PaulS:
When there is serial data to read, read one character, and store it in RIGHT. Then, read the next character, even if there isn't one, and store it in LEFT. Does that really make sense?
So you are saying that this line "LEFT[j]=Serial.read();" is completely useless ?
So you are saying that this line "LEFT[j]=Serial.read();" is completely useless ?
No. But, you need to KNOW whether you are reading the RIGHT value or the LEFT value.
If you want to send two values for RIGHT and LEFT, such as 123 and 45, I seriously doubt that you send data like "14253". So, why are you reading values as though they were interspersed?
I will try it as you said...Also can you tell me how can i see what serial port receives so i would be able to figure out the way that it receives data. I cant see by the serial monitor because when i run the code the serial port is in use and for that does not let me see what data is receiving
anyone knows how can I see what serial port of arduino is receiving ? I can't use serial monitor because port is already in use when i run the code so is there another way to see what is receiving ?
dimis90:
anyone knows how can I see what serial port of arduino is receiving ? I can't use serial monitor because port is already in use when i run the code so is there another way to see what is receiving ?
The application that is sending data to the Arduino needs to have a way of reading data that the Arduino sends. If it does, you are out of luck.
You could, of course, use the Serial Monitor application to send data to the Arduino in the format that you think that application is sending the data. Make the Arduino deal with the known format. If the Arduino then deals with the unknown format data in the same way, then you were correct in guessing what the application is sending. If not, you weren't. So, you make another guess.
dimis90:
anyone knows how can I see what serial port of arduino is receiving ? I can't use serial monitor because port is already in use when i run the code so is there another way to see what is receiving ?
The application that is sending data to the Arduino needs to have a way of reading data that the Arduino sends. If it does, you are out of luck.
You could, of course, use the Serial Monitor application to send data to the Arduino in the format that you think that application is sending the data. Make the Arduino deal with the known format. If the Arduino then deals with the unknown format data in the same way, then you were correct in guessing what the application is sending. If not, you weren't. So, you make another guess.
Does the application that is sending data to the Arduino have the capability to receive data from the Arduino and display it to you?
The application is only sending not receiving. Arduino is only receiving and do some calculations all i want is to see what arduino is receiving but i can't do it through serial monitor because the port is in use when i am running the code from c# so i am asking you is there another way to see how can i see what arduino is receiving?
There is serial port monitoring software available that will run on the pc and capture the transactions occurring on the serial port. In the past i used a program called portmon to monitor serial communications.