Alex exoskeleton
ALEX SoftwareDocumentation
Drive.cpp
Go to the documentation of this file.
1 #include "Drive.h"
2 
3 #include "DebugMacro.h"
4 
6  statusWord = 0;
7  error = 0;
8  this->NodeID = -1;
9 }
10 
12  statusWord = 0;
13  error = 0;
14  this->NodeID = NodeID;
15 }
16 
18  return NodeID;
19 }
20 
21 bool Drive::setPos(int position) {
22  DEBUG_OUT("Drive " << this->NodeID << " Writing " << position << " to 0x607A");
23  *(&CO_OD_RAM.targetMotorPositions.motor1 + ((this->NodeID - 1))) = position;
24  return true;
25 }
26 
27 bool Drive::setVel(int velocity) {
28  DEBUG_OUT("Drive " << NodeID << " Writing " << velocity << " to 0x60FF");
29  return true;
30 }
31 
32 bool Drive::setTorque(int torque) {
33  DEBUG_OUT("Drive " << NodeID << " Writing " << torque << " to 0x6071");
34  return true;
35 }
36 
38  int q = *(&CO_OD_RAM.targetMotorPositions.motor1 + ((this->NodeID - 1)));
39  return q;
40 }
41 
43  return 0;
44 }
45 
47  return 0;
48 }
49 
51  *(&CO_OD_RAM.controlWords.motor1 + ((this->NodeID - 1))) = 0x06;
53 }
54 
55 bool Drive::enable() {
56  *(&CO_OD_RAM.controlWords.motor1 + ((this->NodeID - 1))) = 0x0F;
58 }
59 
61  *(&CO_OD_RAM.controlWords.motor1 + ((this->NodeID - 1))) = 0x00;
63 }
64 
66  return driveState;
67 }
68 
70  statusWord = *(&CO_OD_RAM.statusWords.motor1 + ((this->NodeID - 1)));
71  return statusWord;
72 }
73 
75  int controlWord = *(&CO_OD_RAM.controlWords.motor1 + ((this->NodeID - 1)));
76  *(&CO_OD_RAM.controlWords.motor1 + ((this->NodeID - 1))) = controlWord ^ 0x10;
77  if ((controlWord & 0x10) > 0) {
78  return false;
79  } else {
80  return true;
81  }
82 }
83 
85  DEBUG_OUT("Drive::initPDOs")
86  //DEBUG_OUT("Set up STATUS_WORD TPDO")
88 
89  //DEBUG_OUT("Set up ACTUAL_POS and ACTUAL_VEL TPDO")
91 
92  //DEBUG_OUT("Set up ACTUAL_TOR TPDO")
94 
95  //DEBUG_OUT("Set up TARGET_POS RPDO")
97 
98  //DEBUG_OUT("Set up TARGET_VEL RPDO")
100 
101  return true;
102 }
103 
104 std::vector<std::string> Drive::generateTPDOConfigSDO(std::vector<OD_Entry_t> items, int PDO_Num, int SyncRate) {
105  // TODO: Do a check to make sure that the OD_Entry_t items can be transmitted.
106 
107  // Calculate COB_ID. If TPDO:
108  int COB_ID = 0x100 * PDO_Num + 0x80 + NodeID;
109 
110  // Define Vector to be returned as part of this method
111  std::vector<std::string> CANCommands;
112 
113  // Define stringstream for ease of constructing hex strings
114  std::stringstream sstream;
115 
116  // Disable PDO
117  sstream << "[1] " << NodeID << " write 0x" << std::hex << 0x1800 + PDO_Num - 1 << " 1 u32 0x" << std::hex << 0x800000000 + COB_ID;
118  CANCommands.push_back(sstream.str());
119  sstream.str(std::string());
120 
121  // Set so that there no PDO items, enable mapping change
122  sstream << "[1] " << NodeID << " write 0x" << std::hex << 0x1A00 + PDO_Num - 1 << " 0 u8 0";
123  CANCommands.push_back(sstream.str());
124  sstream.str(std::string());
125 
126  // Set the PDO so that it triggers every SYNC Message
127  sstream << "[1] " << NodeID << " write 0x" << std::hex << 0x1800 + PDO_Num - 1 << " 2 u8 0x" << SyncRate;
128  CANCommands.push_back(sstream.str());
129  sstream.str(std::string());
130 
131  for (int i = 1; i <= items.size(); i++) {
132  // Set transmit parameters
133  sstream << "[1] " << NodeID << " write 0x" << std::hex << 0x1A00 + PDO_Num - 1 << " " << i << " u32 0x" << std::hex << OD_Addresses[items[i - 1]] * 0x10000 + OD_Data_Size[items[i - 1]];
134  CANCommands.push_back(sstream.str());
135  sstream.str(std::string());
136  }
137 
138  // Sets Number of PDO items to reenable
139  sstream << "[1] " << NodeID << " write 0x" << std::hex << 0x1A00 + PDO_Num - 1 << " 0 u8 " << items.size();
140  CANCommands.push_back(sstream.str());
141  sstream.str(std::string());
142 
143  // Enable PDO
144  sstream << "[1] " << NodeID << " write 0x" << std::hex << 0x1800 + PDO_Num - 1 << " 1 u32 0x" << std::hex << COB_ID;
145  CANCommands.push_back(sstream.str());
146  sstream.str(std::string());
147 
148  return CANCommands;
149 }
150 
151 std::vector<std::string> Drive::generateRPDOConfigSDO(std::vector<OD_Entry_t> items, int PDO_Num, int UpdateTiming) {
152  // TODO: Do a check to make sure that the OD_Entry_t items can be Received.
153 
154  // Calculate COB_ID. If TPDO:
155  int COB_ID = 0x100 * PDO_Num + NodeID;
156 
157  // Define Vector to be returned as part of this method
158  std::vector<std::string> CANCommands;
159 
160  // Define stringstream for ease of constructing hex strings
161 
162  std::stringstream sstream;
163  // Disable PDO
164  sstream << "[1] " << NodeID << " write 0x" << std::hex << 0x1400 + PDO_Num - 1 << " 1 u32 0x" << std::hex << 0x800000000 + COB_ID;
165  CANCommands.push_back(sstream.str());
166  sstream.str(std::string());
167 
168  // Set so that there no PDO items, enable mapping change
169  sstream << "[1] " << NodeID << " write 0x" << std::hex << 0x1600 + PDO_Num - 1 << " 0 u8 0";
170  CANCommands.push_back(sstream.str());
171  sstream.str(std::string());
172 
173  // Set the PDO so that it triggers every SYNC Message
174  sstream << "[1] " << NodeID << " write 0x" << std::hex << 0x1400 + PDO_Num - 1 << " 2 u8 0x" << UpdateTiming;
175  CANCommands.push_back(sstream.str());
176  sstream.str(std::string());
177 
178  for (int i = 1; i <= items.size(); i++) {
179  // Set transmit parameters
180  sstream << "[1] " << NodeID << " write 0x" << std::hex << 0x1600 + PDO_Num - 1 << " " << i << " u32 0x" << std::hex << OD_Addresses[items[i - 1]] * 0x10000 + OD_Data_Size[items[i - 1]];
181  CANCommands.push_back(sstream.str());
182  sstream.str(std::string());
183  }
184 
185  // Sets Number of PDO items to reenable
186  sstream << "[1] " << NodeID << " write 0x" << std::hex << 0x1600 + PDO_Num - 1 << " 0 u8 " << items.size();
187  CANCommands.push_back(sstream.str());
188  sstream.str(std::string());
189 
190  // Enable PDO
191  sstream << "[1] " << NodeID << " write 0x" << std::hex << 0x1400 + PDO_Num - 1 << " 1 u32 0x" << std::hex << COB_ID;
192  CANCommands.push_back(sstream.str());
193  sstream.str(std::string());
194 
195  return CANCommands;
196 }
197 
198 std::vector<std::string> Drive::generatePosControlConfigSDO(motorProfile positionProfile) {
199  // Define Vector to be returned as part of this method
200  std::vector<std::string> CANCommands;
201  // Define stringstream for ease of constructing hex strings
202  std::stringstream sstream;
203  // start drive
204  sstream << "[1] " << NodeID << " start";
205  CANCommands.push_back(sstream.str());
206  sstream.str(std::string());
207  //enable profile position mode
208  sstream << "[1] " << NodeID << " write 0x" << std::hex << 0x6060 << " 0 i8 1";
209  CANCommands.push_back(sstream.str());
210  sstream.str(std::string());
211 
212  //Set velocity profile
213  sstream << "[1] " << NodeID << " write 0x" << std::hex << 0x6081 << " 0 i32 " << positionProfile.profileVelocity;
214  CANCommands.push_back(sstream.str());
215  sstream.str(std::string());
216 
217  //Set acceleration profile
218  sstream << "[1] " << NodeID << " write 0x" << std::hex << 0x6083 << " 0 i32 " << positionProfile.profileAccelration;
219  CANCommands.push_back(sstream.str());
220  sstream.str(std::string());
221 
222  //Set deceleration profile
223  sstream << "[1] " << NodeID << " write 0x" << std::hex << 0x6084 << " 0 i32 " << positionProfile.profileDeceleration;
224  CANCommands.push_back(sstream.str());
225  sstream.str(std::string());
226 
227  return CANCommands;
228 }
229 
230 int Drive::sendSDOMessages(std::vector<std::string> messages) {
231  char *returnMessage;
232  DEBUG_OUT("sendSDOMessages");
233  // change to = 0 when testing with real network or something which responds. and uncomment
234  // return message check
235  int successfulMessages = 1;
236  for (auto strCommand : messages) {
237  // explicitly cast c++ string to from const char* to char* for use by cancomm function
238  char *SDO_Message = (char *)(strCommand.c_str());
239  // DEBUG_OUT(SDO_Message);
240 
241 #ifndef NOROBOT
242  cancomm_socketFree(SDO_Message, returnMessage);
243 #endif
244 
245  // TODO: in cancomm_socketFree -> return message correctly.
246  // std::string retMsg = returnMessage;
247  // DEBUG_OUT(retMsg);
248  /*if (strcmp(returnMessage, "OK") == 0)
249  {
250  successfulMessages++;
251  }*/
252  }
253  return successfulMessages;
254 }
DriveState driveState
State of the drive.
Definition: Drive.h:164
int profileAccelration
Definition: Drive.h:85
Definition: Drive.h:34
virtual bool setPos(int position)
Definition: Drive.cpp:21
int getNodeID()
Get returns the CanNode ID.
Definition: Drive.cpp:17
virtual DriveState getDriveState()
Get the current state of the drive.
Definition: Drive.cpp:65
Definition: Drive.h:36
virtual bool enable()
Sets the state of the drive to "enabled".
Definition: Drive.cpp:55
virtual int updateDriveStatus()
Definition: Drive.cpp:69
virtual bool disable()
sets the state of the drive to "disabled"
Definition: Drive.cpp:60
int error
Current error state of the drive.
Definition: Drive.h:158
virtual bool setTorque(int torque)
Definition: Drive.cpp:32
virtual bool initPDOs()
Initialises a standard set of PDOs for the use of the drive. These are:
Definition: Drive.cpp:84
virtual int getPos()
Definition: Drive.cpp:37
int profileDeceleration
Definition: Drive.h:86
int NodeID
The CAN Node ID used to address this particular drive on the CAN bus.
Definition: Drive.h:99
int profileVelocity
Definition: Drive.h:84
std::vector< std::string > generateTPDOConfigSDO(std::vector< OD_Entry_t > items, int PDO_Num, int SyncRate)
Generates the list of commands required to configure TPDOs on the drives.
Definition: Drive.cpp:104
virtual bool readyToSwitchOn()
Changes the state of the drive to "ready to switch on".
Definition: Drive.cpp:50
Drive()
Construct a new Drive object.
Definition: Drive.cpp:5
struct to hold desired velocity, acceleration and deceleration values for a drives motor controller p...
Definition: Drive.h:83
virtual bool setVel(int velocity)
Definition: Drive.cpp:27
int sendSDOMessages(std::vector< std::string > messages)
messages Properly formatted SDO Messages
Definition: Drive.cpp:230
virtual int getTorque()
Definition: Drive.cpp:46
#define DEBUG_OUT(x)
Definition: DebugMacro.h:11
virtual int getVel()
Definition: Drive.cpp:42
DriveState
Definition: Drive.h:33
std::vector< std::string > generateRPDOConfigSDO(std::vector< OD_Entry_t > items, int PDO_Num, int UpdateTiming)
Generates the list of commands required to configure RPDOs on the drives.
Definition: Drive.cpp:151
virtual bool posControlConfirmSP()
Flips Bit 4 of Control Word (0x6041) - A new set point is only confirmed if the transition is from 0 ...
Definition: Drive.cpp:74
void cancomm_socketFree(char *command, char *ret)
Definition: CO_command.c:559
int statusWord
Current status word of the drive.
Definition: Drive.h:152
std::vector< std::string > generatePosControlConfigSDO(motorProfile positionProfile)
Generates the list of commands required to configure Position control in CANopen motor drive...
Definition: Drive.cpp:198