Thank you all. I found out the problem was in the way i created the Rotation matrix from the unit vectors of the target frames.
By constructing it like this i managed to get the Euler angles like i said:
Rmat =
Xx | Xy | Xz |
Yx | Yy | Yz |
Zx | Zy | Zz |
The poses simulated from the script:
The results obtained with robodk (inputting the angles manually obtained from the script):
Checking you screen shot shows that target 2 is actually the flange coordinate system with an angle b of 135 deg (rough guess)
angle b must be in the range of +/90 deg. Doing frame2mat and afterwards an mat2frame will correct this (angle a and angle will change their values and angle b will be in the allowed range.
could this be the problem?
Yes it can be, i am aware of that but ill just make a specific condition for that to give the same orientation or ill implement the code that micahstuh posted from kuka.
I feel like i made the problem a bit confusing but this was just so i can get the orientation of a pose given the vector from the pose coordinates to the target point in space, and the XZ plane of the orientation. The whole idea is that I define a number of poses, the given angle from the base and the distance to the target. This is mainly to use a camera that has manual focus so it needs to face an object with the same orientation in all poses, but i can also use the same concept for other projects like welding,non planar 3d printing, etc..