RoboAide
Project to improve a DIY robotic arm used for mobility assistance
dynamixel.h
Go to the documentation of this file.
1 #include <DynamixelWorkbench.h>
2 
3 // OpenCR device definition
4 #if defined(__OPENCM904__)
5  #define DEVICE_NAME "3" //Dynamixel on Serial3(USART3) <-OpenCM 485EXP
6 #elif defined(__OPENCR__)
7  #define DEVICE_NAME ""
8 #endif
9 
10 class Dynamixel {
11  private:
12  DynamixelWorkbench dxl_wb;
13  const int BAUDRATE = 57600;
15  int id;
17  float gearRatio;
18 
19  public:
20  Dynamixel(int idNumber, float ratio=1)
21  {
22  id = idNumber;
23  gearRatio = ratio;
24  }
25 
27  {
28  }
29 
33  bool init()
34  {
35  const char *log;
36  bool result = false;
37 
38  uint16_t model_number = 0;
39 
40  bool initSuccess = false;
41  bool pingSuccess = false;
42 
43  result = dxl_wb.init(DEVICE_NAME, BAUDRATE, &log);
44  if (result == false)
45  {
46  //Serial.println(log);
47  //Serial.println("Failed to init");
48  }
49  else
50  {
51  //Serial.print("Succeeded to init : ");
52  //Serial.println(BAUDRATE);
53  initSuccess = true;
54  }
55 
56  result = dxl_wb.ping(id, &model_number, &log);
57  if (result == false)
58  {
59  //Serial.println(log);
60  //Serial.println("Failed to ping");
61  }
62  else
63  {
64  //Serial.println("Succeeded to ping");
65  //Serial.print("id : ");
66  //Serial.print(id);
67  //Serial.print(" model_number : ");
68  //Serial.println(model_number);
69  pingSuccess = true;
70  }
71  dxl_wb.setNormalDirection(id);
73  if(initSuccess && pingSuccess)
74  {
75  //Serial.print("Motor id: ");
76  //Serial.print(id);
77  //Serial.println(" has been initialized successfully") ;
78 
79  return true;
80  }
81  return false;
82  }
83 
84 
89  {
90  const char *log;
91  bool result = false;
92  //result = dxl_wb.jointMode(id, 150, 10, &log);
93  result = dxl_wb.setExtendedPositionControlMode(id ,&log);
94  //Serial.println(log);
95  dxl_wb.writeRegister(id,"Profile_Acceleration",10,&log);
96  //Serial.println(log);
97  dxl_wb.writeRegister(id,"Profile_Velocity",150,&log);
98  dxl_wb.torqueOn(id);
99  if (result == false)
100  {
101  //Serial.println(log);
102  //Serial.println("Failed to change joint mode");
103  return false;
104  }
105  else
106  {
107  //Serial.println("Succeed to change joint mode");
108  return true;
109  }
110  }
111 
112 
116  void moveMotor(int32_t pos)
117  {
118  int32_t goalPos = pos * gearRatio;
119  dxl_wb.goalPosition(id, (int32_t)goalPos);
120  //Serial.println("Dynamixel is moving...");
121  }
122 
126  int32_t getPosition()
127  {
128  int32_t data;
129  dxl_wb.getPresentPositionData(id, &data);
130  uint16_t pos = data/gearRatio;
131  return pos;
132  }
133 
134  void torque(bool onoff)
135  {
136 
137  dxl_wb.torque(id, onoff);
138  }
139 };
int id
Dynamixel id.
Definition: dynamixel.h:15
DynamixelWorkbench dxl_wb
Definition: dynamixel.h:12
~Dynamixel()
Definition: dynamixel.h:26
const int BAUDRATE
Definition: dynamixel.h:13
bool init()
Initialize Dynamixel servo motor and verify that it is reachable.
Definition: dynamixel.h:33
Dynamixel(int idNumber, float ratio=1)
Definition: dynamixel.h:20
void torque(bool onoff)
Definition: dynamixel.h:134
void moveMotor(int32_t pos)
Set the goal position of the dynamixel.
Definition: dynamixel.h:116
bool setOperatingMode()
Set the dynamixel in joint mode.
Definition: dynamixel.h:88
float gearRatio
Gear ratio for the motor&#39;s joint.
Definition: dynamixel.h:17
int32_t getPosition()
Get the current position of the Dynamixel.
Definition: dynamixel.h:126