Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using Microsoft.Kinect;
- using dynamixel_sdk;
- using System.Runtime.InteropServices;
- using System.Threading;
- namespace KinectTest4
- {
- public partial class Form1 : Form
- {
- KinectSensor kinectSensor = null;
- BodyFrameReader bodyFrameReader = null;
- Body[] bodies = null;
- public static float Wy;
- public static float Hy;
- public static float Hx;
- public static float Hz;
- public static float Ey;
- public static float Ex;
- public static float Sx;
- public static float Sy;
- public static double Q2;
- public static double angleSE;
- public static double angleEH;
- public static double q1;
- public static double q2;
- public static double anglePitch;
- public static double openclose;
- public static int angle1;
- public static int angle23;
- public static int angle45;
- public static int d1;
- public static int d23;
- public static int d45;
- public static int d7;
- // Control table address
- public const int ADDR_MX_TORQUE_ENABLE = 24; // Control table address is different in Dynamixel model
- public const int ADDR_MX_GOAL_POSITION = 30;
- public const int ADDR_MX_PRESENT_POSITION = 36;
- // Data Byte Length
- public const int LEN_MX_GOAL_POSITION = 2;
- public const int LEN_MX_PRESENT_POSITION = 2;
- // Protocol version
- public const int PROTOCOL_VERSION = 1; // See which protocol version is used in the Dynamixel
- // Default setting
- public const int DXL4_ID = 4; // Dynamixel ID: 4
- public const int DXL5_ID = 5; // Dynamixel ID: 5
- public const int DXL2_ID = 2; // Dynamixel ID: 2
- public const int DXL3_ID = 3; // Dynamixel ID: 3
- public const int DXL1_ID = 1; // Dynamixel ID: 1
- public const int DXL7_ID = 7; // Dynamixel ID: 7
- public const int BAUDRATE = 57142;
- public const string DEVICENAME = "COM4"; // Check which port is being used on your controller
- // ex) Windows: "COM1" Linux: "/dev/ttyUSB0" Mac: "/dev/tty.usbserial-*"
- public const int TORQUE_ENABLE = 1; // Value for enabling the torque
- public const int TORQUE_DISABLE = 0; // Value for disabling the torque
- public const int DXL_MINIMUM_POSITION_VALUE = 128; // Dynamixel will rotate between this value
- public const int DXL_MAXIMUM_POSITION_VALUE = 700; // and this value (note that the Dynamixel would not move when the position value is out of movable range. Check e-manual about the range of the Dynamixel you use.)
- public const int DXL_MOVING_STATUS_THRESHOLD = 5; // Dynamixel moving status threshold
- public const byte ESC_ASCII_VALUE = 0x1b;
- public const int COMM_SUCCESS = 0; // Communication Success result value
- public const int COMM_TX_FAIL = -1001; // Communication Tx Failed
- public static int POS45 = 128;
- public static int POS23 = 138;
- public static int POS1 = 410;
- public static int POS7 = 700;
- public Form1()
- {
- InitializeComponent();
- initializeKinect();
- }
- public void initializeKinect()
- {
- kinectSensor = KinectSensor.GetDefault();
- if(kinectSensor != null)
- {
- kinectSensor.Open();
- }
- bodyFrameReader = kinectSensor.BodyFrameSource.OpenReader();
- if(bodyFrameReader != null)
- {
- bodyFrameReader.FrameArrived += Reader_FrameArrived;
- }
- }
- private void Reader_FrameArrived(object sender, BodyFrameArrivedEventArgs e)
- {
- bool dataReceived = false;
- using (BodyFrame bodyFrame = e.FrameReference.AcquireFrame())
- {
- if (bodyFrame != null)
- {
- if(bodies == null)
- {
- bodies = new Body[bodyFrame.BodyCount];
- }
- bodyFrame.GetAndRefreshBodyData(bodies);
- dataReceived = true;
- }
- if (dataReceived)
- {
- foreach(Body body in bodies)
- {
- if(body.IsTracked)
- {
- IReadOnlyDictionary<JointType, Joint> joints = body.Joints;
- Dictionary<JointType, Point> jointPoints = new Dictionary<JointType, Point>();
- Joint Rightwrist = joints[JointType.WristRight];
- Joint Rightthumb = joints[JointType.ThumbRight];
- Joint LeftHand = joints[JointType.WristLeft];
- Joint LeftElbow = joints[JointType.ElbowLeft];
- Joint LeftShoulder = joints[JointType.ShoulderLeft];
- Wy = Rightwrist.Position.Y;
- Hy = LeftHand.Position.Y;
- Hx = LeftHand.Position.X;
- Hz = LeftHand.Position.Z;
- Ex = LeftElbow.Position.X;
- Ey = LeftElbow.Position.Y;
- Sx = LeftShoulder.Position.X;
- Sx = LeftShoulder.Position.X;
- if (Hy >= Ey && Hx >= Ex)
- {
- Q2 = Math.Atan((Hy - Ey) / (Hx - Ex));
- }
- else if (Hy >= Ey && Hx < Ex)
- {
- Q2 = Math.PI + Math.Atan((Hy - Ey) / (Hx - Ex));
- }
- else if (Hy < Ey && Hx < Ex)
- {
- Q2 = Math.PI + Math.Atan((Hy - Ey) / (Hx - Ex));
- }
- else if (Hy < Ey && Hx >= Ex)
- {
- Q2 = 2 * Math.PI + Math.Atan((Hy - Ey) / (Hx - Ex));
- }
- //angle SE and actual EH
- if (Ey >= Sy && Ex >= Sx)
- {
- angleSE = Math.Atan((Ey - Sy) / (Ex - Sx));
- if (Q2 <= Math.PI)
- {
- angleEH = Math.PI - angleSE + Q2;
- }
- else if (Q2 > Math.PI && Q2 <= 3 * Math.PI / 2)
- {
- angleEH = Q2 - angleSE;
- }
- else if (Q2 > 3 * Math.PI / 2 && Q2 <= 2 * Math.PI)
- {
- angleEH = Q2 - angleSE - Math.PI;
- }
- }
- else if (Ey >= Sy && Ex < Sx)
- {
- angleSE = Math.PI + Math.Atan((Ey - Sy) / (Ex - Sx));
- if (Q2 <= 3 * Math.PI / 2)
- {
- angleEH = Math.PI - angleSE + Q2;
- }
- else if (Q2 > 2 * Math.PI && ((Ey - Hy) / (Hx - Ex)) < ((Ey - Sy) - (Sx - Ex)))
- {
- angleEH = Q2 - angleSE - Math.PI;
- }
- else if (Q2 > 2 * Math.PI && ((Ey - Hy) / (Hx - Ex)) >= ((Ey - Sy) - (Sx - Ex)))
- {
- angleEH = Math.PI - angleSE + Q2;
- }
- }
- else if (Ey < Sy && Ex < Sx)
- {
- angleSE = Math.PI + Math.Atan((Ey - Sy) / (Ex - Sx));
- if (Q2 <= Math.PI / 2 && ((Sy - Ey) / (Sx - Ex) >= (Hy - Ey) / (Hx - Ex)))
- {
- angleEH = 3 * Math.PI - angleSE + Q2;
- }
- else if (Q2 <= Math.PI / 2 && ((Sy - Ey) / (Sx - Ex) < (Hy - Ey) / (Hx - Ex)))
- {
- angleEH = Q2 - angleSE + Math.PI;
- }
- else if (Q2 > Math.PI / 2 && Q2 <= 2 * Math.PI)
- {
- angleEH = Q2 - angleSE + Math.PI;
- }
- }
- else if (Ey < Sy && Ex >= Sx)
- {
- angleSE = 2 * Math.PI + Math.Atan((Ey - Sy) / (Ex - Sx));
- if (Q2 <= Math.PI / 2)
- {
- angleEH = 3 * Math.PI - angleSE + Q2;
- }
- else if (Q2 > Math.PI / 2 && Q2 <= Math.PI && ((Hy - Ey) / (Ex - Hx) >= (Sy - Ey) / (Ex - Sx)))
- {
- angleEH = 3 * Math.PI - angleSE + Q2;
- }
- else if (Q2 > Math.PI / 2 && Q2 <= Math.PI && ((Hy - Ey) / (Ex - Hx) < (Sy - Ey) / (Ex - Sx)))
- {
- angleEH = Math.PI - angleSE + Q2;
- }
- else if (Q2 > Math.PI && Q2 <= 3 * Math.PI / 2)
- {
- angleEH = Q2 - angleSE + Math.PI;
- }
- else if (Q2 > 3 * Math.PI / 2 && Q2 <= 2 * Math.PI && ((Ey - Hy) / (Hx - Ex) < (Sy - Ey) / (Ex - Sx)))
- {
- angleEH = angleSE + Math.PI - Q2;
- }
- else if (Q2 > 3 * Math.PI / 2 && Q2 <= 2 * Math.PI && ((Ey - Hy) / (Hx - Ex) >= (Sy - Ey) / (Ex - Sx)))
- {
- angleEH = Math.PI - angleSE + Q2;
- }
- }
- angle23 = (int)((180 * angleSE / Math.PI +45-30) * 1023 / 300); ///-45
- angle45 = (int)((180 * angleEH / Math.PI -30) * 1023 / 300);
- angle1 = (int)((((Hz * 100 - 90) * 3 / 4) + 120) * 1023 / 300);
- if (Wy>= 0)
- {
- d7 = 536;
- }
- else
- {
- d7 = 705;
- }
- if (angle23 <= 775 && angle23 >= 128)
- {
- d23 = angle23;
- }
- else if (angle23 > 775)
- {
- d23 = 775;
- }
- else if (angle23 < 128)
- {
- d23 = 128;
- }
- if (angle45 <= 885 && angle45 >= 128)
- {
- d45 = angle45;
- }
- else if (angle45 > 885)
- {
- d23 = 775;
- }
- else if (angle45 < 128)
- {
- d23 = 128;
- }
- if (angle1 <= 612 && angle1 >= 410)
- {
- d1 = angle1;
- }
- else if (angle1 > 612)
- {
- d1 = 612;
- }
- else if (angle1 < 410)
- {
- d1 = 410;
- }
- q1 = angleSE * 180 / Math.PI;
- q2 = angleEH * 180 / Math.PI;
- textBox1.Text = d1.ToString("");
- //textBox2.Text = q1.ToString("");
- //textBox3.Text = q2.ToString("");
- textBox2.Text = d23.ToString("");
- textBox3.Text = d45.ToString("");
- textBox4.Text = d7.ToString("");
- }
- }
- }
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- POS1 = (ushort)d1;
- POS23 = (ushort)d23;
- POS45 = (ushort)d45;
- POS7 = (ushort)d7;
- getPos();
- //getPos();
- //getPos();
- }
- private void getPos()
- {
- Console.WriteLine("");
- Console.WriteLine(d1 + " " + d23 + " " + d45 + " " + d7);
- Console.ReadLine();
- int port_num = dynamixel.portHandler(DEVICENAME);
- // Initialize PacketHandler Structs
- dynamixel.packetHandler();
- // Initialize Groupsyncwrite instance
- int group_num = dynamixel.groupSyncWrite(port_num, PROTOCOL_VERSION, ADDR_MX_GOAL_POSITION, LEN_MX_GOAL_POSITION);
- int index = 0;
- int dxl_comm_result = COMM_TX_FAIL; // Communication result
- bool dxl_addparam_result = false; // AddParam result
- UInt16[] dxl_goal_position45 = new UInt16[2] { (ushort)POS45, (ushort)POS45 };// { DXL_MINIMUM_POSITION_VALUE, DXL_MAXIMUM_POSITION_VALUE }; // Goal position
- UInt16[] dxl_goal_position23 = new UInt16[2] { (ushort)POS23, (ushort)POS23 };
- UInt16[] dxl_goal_position1 = new UInt16[2] { (ushort)POS1, (ushort)POS1 };
- UInt16[] dxl_goal_position7 = new UInt16[2] { (ushort)POS7, (ushort)POS7 };
- byte dxl_error = 0; // Dynamixel error
- UInt16 dxl1_present_position = 0, dxl2_present_position = 0, dxl3_present_position = 0, dxl4_present_position = 0, dxl5_present_position = 0, dxl7_present_position = 0; // Present position
- // Open port
- if (dynamixel.openPort(port_num))
- {
- //Console.WriteLine("Succeeded to open the port!");
- }
- else
- {
- //Console.WriteLine("Failed to open the port!");
- //Console.WriteLine("Press any key to terminate...");
- // Console.ReadKey();
- return;
- }
- // Set port baudrate
- if (dynamixel.setBaudRate(port_num, BAUDRATE))
- {
- // Console.WriteLine("Succeeded to change the baudrate!");
- }
- else
- {
- // Console.WriteLine("Failed to change the baudrate!");
- // Console.WriteLine("Press any key to terminate...");
- // Console.ReadKey();
- return;
- }
- // Enable Dynamixel#1 Torque
- dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL1_ID, ADDR_MX_TORQUE_ENABLE, TORQUE_ENABLE);
- if ((dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)) != COMM_SUCCESS)
- {
- //Console.WriteLine(Marshal.PtrToStringAnsi(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result)));
- }
- else if ((dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)) != 0)
- {
- //Console.WriteLine(Marshal.PtrToStringAnsi(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error)));
- }
- else
- {
- Console.WriteLine("Dynamixel{0} has been successfully connected ", DXL1_ID);
- }
- // Enable Dynamixel#2 Torque
- dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL2_ID, ADDR_MX_TORQUE_ENABLE, TORQUE_ENABLE);
- if ((dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)) != COMM_SUCCESS)
- {
- //Console.WriteLine(Marshal.PtrToStringAnsi(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result)));
- }
- else if ((dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)) != 0)
- {
- //Console.WriteLine(Marshal.PtrToStringAnsi(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error)));
- }
- else
- {
- Console.WriteLine("Dynamixel{0} has been successfully connected ", DXL2_ID);
- }
- // Enable Dynamixel#3 Torque
- dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL3_ID, ADDR_MX_TORQUE_ENABLE, TORQUE_ENABLE);
- if ((dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)) != COMM_SUCCESS)
- {
- //Console.WriteLine(Marshal.PtrToStringAnsi(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result)));
- }
- else if ((dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)) != 0)
- {
- //Console.WriteLine(Marshal.PtrToStringAnsi(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error)));
- }
- else
- {
- Console.WriteLine("Dynamixel{0} has been successfully connected ", DXL3_ID);
- }
- // Enable Dynamixel#4 Torque
- dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL4_ID, ADDR_MX_TORQUE_ENABLE, TORQUE_ENABLE);
- if ((dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)) != COMM_SUCCESS)
- {
- //Console.WriteLine(Marshal.PtrToStringAnsi(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result)));
- }
- else if ((dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)) != 0)
- {
- //Console.WriteLine(Marshal.PtrToStringAnsi(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error)));
- }
- else
- {
- Console.WriteLine("Dynamixel{0} has been successfully connected ", DXL4_ID);
- }
- // Enable Dynamixel#5 Torque
- dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL5_ID, ADDR_MX_TORQUE_ENABLE, TORQUE_ENABLE);
- if ((dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)) != COMM_SUCCESS)
- {
- //Console.WriteLine(Marshal.PtrToStringAnsi(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result)));
- }
- else if ((dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)) != 0)
- {
- //Console.WriteLine(Marshal.PtrToStringAnsi(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error)));
- }
- else
- {
- Console.WriteLine("Dynamixel{0} has been successfully connected ", DXL5_ID);
- }
- // Enable Dynamixel#7 Torque
- dynamixel.write1ByteTxRx(port_num, PROTOCOL_VERSION, DXL7_ID, ADDR_MX_TORQUE_ENABLE, TORQUE_ENABLE);
- if ((dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)) != COMM_SUCCESS)
- {
- //Console.WriteLine(Marshal.PtrToStringAnsi(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result)));
- }
- else if ((dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)) != 0)
- {
- //Console.WriteLine(Marshal.PtrToStringAnsi(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error)));
- }
- else
- {
- Console.WriteLine("Dynamixel{0} has been successfully connected ", DXL7_ID);
- }
- while (true)
- {
- //Console.WriteLine("Press any key to continue! (or press ESC to quit!)");
- //if (Console.ReadKey().KeyChar == ESC_ASCII_VALUE)
- //break;
- // Add Dynamixel#1 goal position value to the Syncwrite parameter storage
- dxl_addparam_result = dynamixel.groupSyncWriteAddParam(group_num, DXL1_ID, dxl_goal_position1[index], LEN_MX_GOAL_POSITION);
- if (dxl_addparam_result != true)
- {
- Console.WriteLine("[ID: {0}] groupSyncWrite addparam failed", DXL1_ID);
- return;
- }
- // Add Dynamixel#2 goal position value to the Syncwrite parameter storage
- dxl_addparam_result = dynamixel.groupSyncWriteAddParam(group_num, DXL2_ID, dxl_goal_position23[index], LEN_MX_GOAL_POSITION);
- if (dxl_addparam_result != true)
- {
- Console.WriteLine("[ID: {0}] groupSyncWrite addparam failed", DXL2_ID);
- return;
- }
- // Add Dynamixel#3 goal position value to the Syncwrite parameter storage
- dxl_addparam_result = dynamixel.groupSyncWriteAddParam(group_num, DXL3_ID, dxl_goal_position23[index], LEN_MX_GOAL_POSITION);
- if (dxl_addparam_result != true)
- {
- Console.WriteLine("[ID: {0}] groupSyncWrite addparam failed", DXL3_ID);
- return;
- }
- // Add Dynamixel#4 goal position value to the Syncwrite storage
- dxl_addparam_result = dynamixel.groupSyncWriteAddParam(group_num, DXL4_ID, dxl_goal_position45[index], LEN_MX_GOAL_POSITION);
- if (dxl_addparam_result != true)
- {
- Console.WriteLine("[ID: {0}] groupSyncWrite addparam failed", DXL4_ID);
- return;
- }
- // Add Dynamixel#5 goal position value to the Syncwrite parameter storage
- dxl_addparam_result = dynamixel.groupSyncWriteAddParam(group_num, DXL5_ID, dxl_goal_position45[index], LEN_MX_GOAL_POSITION);
- if (dxl_addparam_result != true)
- {
- Console.WriteLine("[ID: {0}] groupSyncWrite addparam failed", DXL5_ID);
- return;
- }
- // Add Dynamixel#7 goal position value to the Syncwrite parameter storage
- dxl_addparam_result = dynamixel.groupSyncWriteAddParam(group_num, DXL7_ID, dxl_goal_position7[index], LEN_MX_GOAL_POSITION);
- if (dxl_addparam_result != true)
- {
- Console.WriteLine("[ID: {0}] groupSyncWrite addparam failed", DXL7_ID);
- return;
- }
- // Syncwrite goal position
- dynamixel.groupSyncWriteTxPacket(group_num);
- if ((dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)) != COMM_SUCCESS)
- //Console.WriteLine(Marshal.PtrToStringAnsi(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result)));
- // Clear syncwrite parameter storage
- dynamixel.groupSyncWriteClearParam(group_num);
- do
- {
- // Read Dynamixel#1 present position
- dxl1_present_position = dynamixel.read2ByteTxRx(port_num, PROTOCOL_VERSION, DXL1_ID, ADDR_MX_PRESENT_POSITION);
- if ((dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)) != COMM_SUCCESS)
- {
- //Console.WriteLine(Marshal.PtrToStringAnsi(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result)));
- }
- else if ((dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)) != 0)
- {
- //Console.WriteLine(Marshal.PtrToStringAnsi(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error)));
- }
- // Read Dynamixel#2 present position
- dxl2_present_position = dynamixel.read2ByteTxRx(port_num, PROTOCOL_VERSION, DXL2_ID, ADDR_MX_PRESENT_POSITION);
- if ((dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)) != COMM_SUCCESS)
- {
- //Console.WriteLine(Marshal.PtrToStringAnsi(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result)));
- }
- else if ((dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)) != 0)
- {
- //Console.WriteLine(Marshal.PtrToStringAnsi(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error)));
- }
- // Read Dynamixel#3 present position
- dxl3_present_position = dynamixel.read2ByteTxRx(port_num, PROTOCOL_VERSION, DXL3_ID, ADDR_MX_PRESENT_POSITION);
- if ((dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)) != COMM_SUCCESS)
- {
- //Console.WriteLine(Marshal.PtrToStringAnsi(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result)));
- }
- else if ((dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)) != 0)
- {
- //Console.WriteLine(Marshal.PtrToStringAnsi(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error)));
- }
- // Read Dynamixel#4 present position
- dxl4_present_position = dynamixel.read2ByteTxRx(port_num, PROTOCOL_VERSION, DXL4_ID, ADDR_MX_PRESENT_POSITION);
- if ((dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)) != COMM_SUCCESS)
- {
- //Console.WriteLine(Marshal.PtrToStringAnsi(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result)));
- }
- else if ((dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)) != 0)
- {
- //Console.WriteLine(Marshal.PtrToStringAnsi(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error)));
- }
- // Read Dynamixel#5 present position
- dxl5_present_position = dynamixel.read2ByteTxRx(port_num, PROTOCOL_VERSION, DXL5_ID, ADDR_MX_PRESENT_POSITION);
- if ((dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)) != COMM_SUCCESS)
- {
- //Console.WriteLine(Marshal.PtrToStringAnsi(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result)));
- }
- else if ((dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)) != 0)
- {
- //Console.WriteLine(Marshal.PtrToStringAnsi(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error)));
- }
- // Read Dynamixel#7 present position
- dxl7_present_position = dynamixel.read2ByteTxRx(port_num, PROTOCOL_VERSION, DXL7_ID, ADDR_MX_PRESENT_POSITION);
- if ((dxl_comm_result = dynamixel.getLastTxRxResult(port_num, PROTOCOL_VERSION)) != COMM_SUCCESS)
- {
- // Console.WriteLine(Marshal.PtrToStringAnsi(dynamixel.getTxRxResult(PROTOCOL_VERSION, dxl_comm_result)));
- }
- else if ((dxl_error = dynamixel.getLastRxPacketError(port_num, PROTOCOL_VERSION)) != 0)
- {
- //Console.WriteLine(Marshal.PtrToStringAnsi(dynamixel.getRxPacketError(PROTOCOL_VERSION, dxl_error)));
- }
- //Console.WriteLine("[ID: {0}] [ID: {1}] GoalPos: {2} PresPos: {3} PresPos: {4} [ID: {5}] [ID: {6}] GoalPos: {7} PresPos: {8} PresPos: {9}",
- // DXL2_ID, DXL3_ID, dxl_goal_position23[index], dxl2_present_position, dxl3_present_position, DXL4_ID, DXL5_ID, dxl_goal_position45[index], dxl4_present_position, dxl5_present_position);
- // Console.WriteLine("[ID: {0}] GoalPos: {1} PresPos: {2} [ID: {3}] GoalPos: {4} PresPos: {5}",
- // DXL1_ID, dxl_goal_position1[index], dxl1_present_position, DXL7_ID, dxl_goal_position7[index], dxl7_present_position);
- Thread.Sleep(1000);
- Console.WriteLine(dxl1_present_position + " " + dxl2_present_position + " " + dxl3_present_position + " " + dxl4_present_position + " " + dxl5_present_position + " " + dxl7_present_position);
- }
- while ((Math.Abs(dxl_goal_position45[index] - dxl4_present_position) > DXL_MOVING_STATUS_THRESHOLD)
- && (Math.Abs(dxl_goal_position45[index] - dxl5_present_position) > DXL_MOVING_STATUS_THRESHOLD)
- && (Math.Abs(dxl_goal_position23[index] - dxl2_present_position) > DXL_MOVING_STATUS_THRESHOLD)
- && (Math.Abs(dxl_goal_position23[index] - dxl3_present_position) > DXL_MOVING_STATUS_THRESHOLD)
- && (Math.Abs(dxl_goal_position1[index] - dxl1_present_position) > DXL_MOVING_STATUS_THRESHOLD)
- && (Math.Abs(dxl_goal_position7[index] - dxl7_present_position) > DXL_MOVING_STATUS_THRESHOLD));
- // Change goal position
- if (index == 0)
- {
- index = 1;
- }
- else
- {
- index = 0;
- }
- dynamixel.closePort(port_num);
- return;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement