RoboAide
Project to improve a DIY robotic arm used for mobility assistance
main.ino
Go to the documentation of this file.
1 #include <Arduino.h>
2 #include "dynamixel.h"
3 #include "axialMotor.h"
4 // We used those links to modify encoder.cpp
5 //https://github.com/ROBOTIS-GIT/OpenCM9.04/pull/30/files
6 //http://emanual.robotis.com/docs/en/parts/controller/opencr10/#layoutpin-map
7 #include "encoder/Encoder.cpp"
8 
9 // Declare global variables
10 const int MESSAGE_SIZE = 19;
11 char endOfMessageChar = '\0';
12 const int id3 = 221;
13 const int id1 = 222;
14 const int id2 = 223;
15 Dynamixel mot1(id1, 0.879); //28
16 Dynamixel mot2(id2, 0.879*2); //40
17 Dynamixel mot3(id3, 1); //20
18 
19 
24 struct dataPack {
26  char mode;
28  uint16_t p1;
30  uint16_t p2;
32  uint16_t p3;
34  uint16_t p4;
36  uint16_t p5;
38  uint16_t p6;
40  bool shouldStop;
42  bool drawer1;
44  bool drawer2;
46  bool drawer3;
48  char last;
49 };
50 
51 // Function prototypes
52 bool readDataToStruct(dataPack *data);
53 void readMessage(char *message);
54 void sendMessage(dataPack message);
55 void moveAbsolute(uint16_t p1, uint16_t p2, uint16_t p3, uint16_t p4, uint16_t p5, uint16_t p6);
56 void moveIncremental(uint16_t p1, uint16_t p2, uint16_t p3, uint16_t p4, uint16_t p5, uint16_t p6);
57 void setDrawerGoalState(bool drawer1, bool drawer2, bool drawer3);
58 void stopMotors();
59 void startMotors();
60 
61 // Arduino functions
62 void setup() {
63  Serial.begin(9600); // set the baud rate, must be the same for both machines
64  //while (!Serial);
65  mot1.init();
66  mot2.init();
67  mot3.init();
68  //dataPack outgoingMessage{(byte)'s',(int32_t)(mot1.getPosition()), (int32_t)(mot2.getPosition()), (int32_t)(mot3.getPosition()), 0, 0, 0, (byte)'\0'};
69  //sendMessage(outgoingMessage);
70  //pinMode(test.getProximitySensorPin(1), INPUT_PULLUP); //Set input as a pull-up for proximity sensor
71  //pinMode(test.getProximitySensorPin(2), INPUT_PULLUP); //Set input as a pull-up for proximity sensor
72  //pinMode(test.getMotorPin(1),OUTPUT);
73  //pinMode(test.getMotorPin(2),OUTPUT);
74  //pinMode(test.getDrivePin(),OUTPUT);
75 }
76 long oldPosition = -999; //variable de départ pour l'encodeur
77 void loop() {
78  /* long newPosition = myEnc.read();
79  if (newPosition != oldPosition) {
80  oldPosition = newPosition;
81  Serial.println(newPosition);
82  }
83  test.setEnableDrive(true);
84  test.setMotorState(-1);
85  test.runAxialCalibration();
86  Serial.println("done");
87  while (1)*/
88 
89  if (Serial.available() >= MESSAGE_SIZE) // Only parse message when the full message has been received.
90  {
91  // Read data
92  dataPack data;
93  if (readDataToStruct(&data))
94  {
95  // Debug
96  //Serial.println(data.p1);
97  //Serial.println(data.p2);
98  //Serial.println(data.p3);
99  //Serial.println(data.p4);
100  //Serial.println(data.p5);
101  //Serial.println(data.p6);
102  //Serial.println(data.end);
103  //byte* serializedMessage = (byte*)&data, sizeof(data);
104  //Serial.println(serializedMessage);
105  if(data.shouldStop == false)
106  {
107  if(data.mode == 'a')
108  {
109  moveAbsolute(data.p1, data.p2, data.p3, data.p4, data.p5, data.p6);
110  setDrawerGoalState(data.drawer1, data.drawer2, data.drawer3);
111  }
112  else if(data.mode == 'i')
113  {
114  moveIncremental(data.p1, data.p2, data.p3, data.p4, data.p5, data.p6);
115  setDrawerGoalState(data.drawer1, data.drawer2, data.drawer3);
116  }
117  else if(data.mode == 's')
118  {
119  // Do nothing apart from sending message
120  }
121  else if(data.mode == 'c')
122  {
123  //TODO: connect to calibration function
124  }
125  }
126  else
127  {
128  stopMotors();
129  startMotors();
130  }
131 
132 
133 
134  dataPack outgoingMessage{(byte)'a',
135  (int16_t)(mot1.getPosition()),
136  (int16_t)(mot2.getPosition()),
137  (int16_t)(mot3.getPosition()),
138  0,
139  0,
140  0,
141  (bool)data.shouldStop,
142  (bool)data.drawer1,
143  (bool)data.drawer2,
144  (bool)data.drawer3,
145  (byte)'\0'};
146  sendMessage(outgoingMessage);
147  }
148  else
149  {
150  //Serial.println("Failed to parse message");
151  }
152  }
153 }
154 
155 // Functions
156 
161 void readMessage(char *message)
162 {
163  int count = 0;
164  char c;
165 
166  do
167  {
168  if (Serial.available())
169  {
170  c = Serial.read();
171  message[count] = c;
172  count++;
173  }
174  } while (c != '\0');
175 }
176 
177 
183 {
184  int i = 0;
185  byte buf[MESSAGE_SIZE];
186  while (Serial.available() && i < MESSAGE_SIZE)
187  {
188  buf[i] = Serial.read();
189  i++;
190  }
191  memcpy(data, buf, sizeof(*data));
192  if (data->last != endOfMessageChar) // if the last character is not the end-of-message character, message is corrupted
193  return false;
194 
195  return true;
196 }
197 
198 
202 void sendMessage(dataPack message)
203 {
204  Serial.write((byte*)&message, sizeof(message));
205 }
206 
207 
211 void moveAbsolute(uint16_t p1, uint16_t p2, uint16_t p3, uint16_t p4, uint16_t p5, uint16_t p6)
212 {
213  mot1.moveMotor(p1);
214  mot2.moveMotor(p2);
215  mot3.moveMotor(p3);
216 }
217 
221 void moveIncremental(uint16_t p1, uint16_t p2, uint16_t p3, uint16_t p4, uint16_t p5, uint16_t p6)
222 {
223  //TODO
224 }
225 
227 {
228  //TODO
229 }
230 
232 {
233  mot1.torque(false);
234  mot2.torque(false);
235  mot3.torque(false);
236 }
237 
239 {
240  mot1.torque(true);
241  mot2.torque(true);
242  mot3.torque(true);
243 }
uint16_t p6
Motor 6 position.
Definition: main.ino:38
Dynamixel mot1(id1, 0.879)
bool drawer3
Drawer 3 state.
Definition: main.ino:46
void setDrawerGoalState(bool drawer1, bool drawer2, bool drawer3)
Definition: main.ino:226
uint16_t p5
Motor 5 position.
Definition: main.ino:36
const int id1
Definition: main.ino:13
char endOfMessageChar
Definition: main.ino:11
uint16_t p4
Motor 4 position.
Definition: main.ino:34
char last
End of message character.
Definition: main.ino:48
long oldPosition
Definition: main.ino:76
void stopMotors()
Definition: main.ino:231
char mode
Operation mode.
Definition: main.ino:26
uint16_t p2
Motor 2 position.
Definition: main.ino:30
bool init()
Initialize Dynamixel servo motor and verify that it is reachable.
Definition: dynamixel.h:33
void sendMessage(dataPack message)
Sends an encoded message over serial.
Definition: main.ino:202
bool drawer2
Drawer 2 state.
Definition: main.ino:44
void moveAbsolute(uint16_t p1, uint16_t p2, uint16_t p3, uint16_t p4, uint16_t p5, uint16_t p6)
move motors to an absolute position
Definition: main.ino:211
void moveIncremental(uint16_t p1, uint16_t p2, uint16_t p3, uint16_t p4, uint16_t p5, uint16_t p6)
move motors to an incremental position
Definition: main.ino:221
Dynamixel mot3(id3, 1)
const int MESSAGE_SIZE
Definition: main.ino:10
void torque(bool onoff)
Definition: dynamixel.h:134
void setup()
Definition: main.ino:62
Dynamixel mot2(id2, 0.879 *2)
void startMotors()
Definition: main.ino:238
const int id2
Definition: main.ino:14
uint16_t p1
Motor 1 position.
Definition: main.ino:28
void loop()
Definition: main.ino:77
void moveMotor(int32_t pos)
Set the goal position of the dynamixel.
Definition: dynamixel.h:116
const int id3
Definition: main.ino:12
bool readDataToStruct(dataPack *data)
Iterates through message one byte at a time casts it to a dataPack struct.
Definition: main.ino:182
bool shouldStop
Stop indicator.
Definition: main.ino:40
uint16_t p3
Motor 3 position.
Definition: main.ino:32
void readMessage(char *message)
Iterates through message one character at a time until the end character is found and returns a char ...
Definition: main.ino:161
bool drawer1
Drawer 1 state.
Definition: main.ino:42
A structure containing message data.
Definition: main.ino:24
int32_t getPosition()
Get the current position of the Dynamixel.
Definition: dynamixel.h:126