Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 2:12431dd6475f, committed 2019-06-15
- Comitter:
- DunkelAmber
- Date:
- Sat Jun 15 09:31:22 2019 +0000
- Parent:
- 1:9ecda2b969c2
- Commit message:
- feat: itocan supports template
Changed in this revision
| canid.hpp | Show annotated file Show diff for this revision Revisions of this file |
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/canid.hpp Sat Jun 15 09:31:22 2019 +0000
@@ -0,0 +1,37 @@
+#ifndef __CANID__
+#define __CANID__
+
+enum can_commands {
+ /* VESCs */
+ BLDC_SET_DUTY = 0, // Set the duty cycle of the motor
+ BLDC_SET_CURRENT, // Set the motor current
+ BLDC_SET_CURRENT_BRAKE, // Set the braking current
+ BLDC_SET_RPM, // Set the motor ERPM
+ BLDC_SET_POS, // Set the motor position
+ BLDC_FILL_RX_BUFFER, // Copy data to the VESC recieve buffer
+ BLDC_FILL_RX_BUFFER_LONG, // Copy data to the VESC recieve buffer
+ BLDC_PROCESS_RX_BUFFER, // Process a command with data from the recieve buffer
+ BLDC_PROCESS_SHORT_BUFFER, // Process a command with data from the recieve buffer
+ BLDC_STATUS, // The motor status (1 of 3)
+ BLDC_STATUS2, // The motor status (2 of 3)
+ BLDC_STATUS3, // The motor status (3 of 3)
+
+ /* Mobility Controller */
+ MOBILITY_GO = 15, // Set the motor speed
+
+ /* BMS - Battery Management System */
+
+ /* Arm Controller */
+
+ /* Arm Joint Controllers */
+
+ /* Scientific Module */
+ SCIENTIFIC_BOX_COMMANDS = 201,
+ SCIENTIFIC_BOX_DATA,
+ SCIENTIFIC_ENVIRONMENT_DATA
+
+ /* Turret Pan-Tilt Controller */
+
+ };
+
+#endif // __CANID__
--- a/main.cpp Fri Jun 14 15:08:55 2019 +0000
+++ b/main.cpp Sat Jun 15 09:31:22 2019 +0000
@@ -1,4 +1,7 @@
- #include "mbed.h" //5.4.7 (144)
+#include "mbed.h" //5.4.7 (144)
+#include "canid.hpp"
+
+#define BIG_ENDIAN
const PinName can1rxPins[] = {PA_11};
const PinName can1txPins[] = {PA_12};
@@ -9,7 +12,7 @@
//CAN1
//0,0=OK 0,1=OK 0,2=OK 1,0=OK 1,1=OK 1,2=OK 2,0=OK 2,1=OK 2,2=OK
-//CAN2
+//CAN"
//0,0=RX_OK 0,1=OK 1,0=RX_OK 1,1=OK
CAN can1(can1rxPins[0], can1txPins[0]);
@@ -24,23 +27,62 @@
CANMessage messageIn;
CANMessage messageOut;
+void setID (CANMessage& message, uint32_t device_id, uint32_t content_id) {
+ uint32_t id = device_id;
+ id |= (uint32_t)content_id << 8;
+ id |= 0x80000000; // Set EFF(Extended Frame Format) flag true
+
+ message.id = id;
+
+}
+
uint64_t cantoi(CANMessage message) {
- int data=0;
+ uint64_t ret=0;
+
+ #ifdef BIG_ENDIAN
for(int i=message.len-1, k=0; i>=0; i--, k++) {
- data += message.data[i] << (8*k);
+ ret += message.data[i] << (8*k);
}
- return data;
+ #else //LITTLE ENDIAN
+ for(int i=0, k=0; i>=message.len-1; i++, k++) {
+ ret += message.data[i] << (8*k);
+ }
+ #endif
+
+
+ return ret;
//pose = message.data[0] + (message.data[1] << 8) + (message.data[2] << 16) + (message.data[3] << 24);
}
-void itocan(uint64_t data, CANMessage& message) {
+template <class data_t> void xtocan(data_t _data, unsigned int n_bytes, CANMessage& message) {
+ data_t data = 0;
+ message.len = n_bytes;
+ int mask = 0xff; // last 8 bits
+
+ #ifdef BIG_ENDIAN
+ for(int i=message.len-1; i>=0; i--) {
+ message.data[i] = data & mask;
+ data = data>>8;
+ }
+ #else //LITTLE ENDIAN
+ for(int i=0; i<message.len; i++) {
+ message.data[i] = data & maks;
+ data = data>>8;
+ }
+ #endif
+
+ return;
+}
+
+/* DEPRECATED
+void ui64tocan(uint64_t data, CANMessage& message) {
for(int i=message.len-1; i>=0; i--) {
//printf("dataconv:\t% 12llu\n\r", data);
message.data[i] = data & 0x00000000000000ff;
data = data>>8;
}
-}
+}*/
void canRxIsr()
{
@@ -65,12 +107,14 @@
while (true) {
counter += 1;
- messageOut.id = 1000;
- messageOut.format = CANStandard;
+ messageOut.id = 1000; // DELETEME
+ setID(messageOut, 1, BLDC_SET_RPM);
+ messageOut.format = CANExtended;
messageOut.data[0]=25;
messageOut.len = 8;
- itocan(counter, messageOut);
+ xtocan<uint64_t>(counter, 8, messageOut);
+ //ui64tocan(counter, messageOut);
printf("sending couter: %llu\t| CAN data: %llu\n\r", counter, cantoi(messageOut));
sendMessage();
osDelay(1000);
@@ -84,7 +128,7 @@
can.frequency(500000);
messageOut.id = 1000;
- messageOut.format = CANStandard;
+ messageOut.format = CANExtended;
messageOut.data[0]=24;
messageOut.len = 8;