# Help with Code Conversion

Hello, I need to implement inverse kinematics in a windows program I am writing, using an AL5D arm. Unfortunately, I am not an expert programmer, and DEFINITELY not an expert mathematician, so I cannot write it myself and must use existing code. I managed to find some arduino code for controlling the arm. However, I will not be using the Arduino in my project, and will instead be interfacing directly with the SSC-32U servo controlled. The controlled is controlled by ascii commands transmitted over a serial connection. The commands are found HERE.
If I knew C++, I would not have this problem (arduino code is compiled in C++, i believe), but I dont. I know some arduino programming, but not how to write a real windows application in C++. Therefore I need your help converting this code into Visual Basic.NET.

``````int Arm(float x, float y, float z, int g, float wa, int wr) //Here's all the Inverse Kinematics to control the arm
{
float M = sqrt((y*y)+(x*x));
if(M <= 0)
return 1;
float A1 = atan(y/x);
if(x <= 0)
return 1;
float A2 = acos((A*A-B*B+M*M)/((A*2)*M));
float Elbow = acos((A*A+B*B-M*M)/((A*2)*B));
float Shoulder = A1 + A2;
Elbow = Elbow * rtod;
Shoulder = Shoulder * rtod;
if((int)Elbow <= 0 || (int)Shoulder <= 0)
return 1;
float Wris = abs(wa - Elbow - Shoulder) - 90;
#ifdef DIGITAL_RANGE
Elb.writeMicroseconds(map(180 - Elbow, 0, 180, 900, 2100  ));
Shldr.writeMicroseconds(map(Shoulder, 0, 180, 900, 2100));
#else
Elb.write(180 - Elbow);
Shldr.write(Shoulder);
#endif
Wrist.write(180 - Wris);
Base.write(z);
WristR.write(wr);
#ifndef FSRG
Gripper.write(g);
#endif
Y = tmpy;
X = tmpx;
Z = tmpz;
WA = tmpwa;
#ifndef FSRG
G = tmpg;
#endif
WR = tmpwr;
return 0;
}
``````
1. How do I implement the statements that begin with # in VB?
2. How would I implement the commands that move the arm in ASCII? (syntax is detailed in the link)
3. What other adjustments do I have to make?
1. Arduinos are not programmed in VB. Why would you think we would know anything about it ?
2. No idea

This program is written in arduino. I'm hoping someone comes along who knows both VB and arduino.

1. How do I implement the statements that begin with # in VB?

You don't. A #define statement is a preprocessor directive. VB doesn't have a preprocessor or a compiler.

C++ preprocessing statements are used to define elements to the compiler. These symbols can be defined as constants that allow conditional compilation of parts of the code. In your example it seems that there are different options for how the device might work.

You have 2 options in dealing with these. Firstly decide if you need the options and only use the relevant code (that is, you are acting like the preprocessor and picking which path the code will take).

The second option is to define global constants in the code that allow a standard VB if statement to select the path. So #ifdef or #if translates to if.

You can get the definition of the preprocessing directives by googling "C++ preprocessing directives" so that you can understand what they do.

You lost me at "visual basic". That's an awful language.

In any event, do you have the serial communication working?
IMO, that's the first thing you need to get sorted. By the time you've got that working, you'll probably have learned enough about VB (my condolences) to convert that function.

The key thing to know about preprocessor directives is that #define/etc are evaluated at compile time; you can either use constants and implement them as if/then in the VB, or you could remove them if the conditional is always going to go one way or another.