I am working on a project where i need the output shaft position of the motor to be fed in my logic, but when i am trying to read the motor position, sing various commands, like 9C, 92, 94, it gives me maybe inside encoder position as my readings fluctuate 9 times between 0 and the highest value, and it is written in the manual which i am referring to that this gives output shaft angle, and i have seen the same command used in Skyentific youtube videos ( Great and Affordable Robotic Actuator (MyActuator RMD X8 and RMD X8 Pro) (youtube.com)), i am sharing my code as well down here, can anyone help me in figuring out what's the error?
I have tried dividing, multiplying the values but it doesnt work because i get values in 0-max_value cycle so any arithmetic operation will not work on the read values.
If needed i can give much elaborate detail and explanation of my understanding, please let me know,
#include "mcp_can.h"
#include <SPI.h>
/*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB
#else
#define SERIAL Serial
#endif
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 9;
const int CAN_INT_PIN = 2;
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup() {
// put your setup code here, to run once:
SERIAL_PORT_MONITOR.begin(115200);
delay(1000);
while (CAN_OK != CAN.begin(CAN_1000KBPS)) // init can bus : baudrate = 500k
{
SERIAL_PORT_MONITOR.println("CAN init failed, retry...");
delay(1000);
}
SERIAL_PORT_MONITOR.println("CAN init ok");
}
void loop() {
FreeRotate(); // i.e. with the desired torque at zero, it should be easy to spin the motor
PosReadSingleCircle();
// PosReadMultiCircle();
}
void FreeRotate(){
int torque_current = 0;
unsigned char buf[8];
unsigned char len = 0;
buf[0] = 0xA1;
buf[1] = 0x00;
buf[2] = 0x00;
buf[3] = 0x00;
buf[4] = torque_current;
buf[5] = torque_current >> 8;
buf[6] = 0x00;
buf[7] = 0x00;
CAN.sendMsgBuf(0x141, 0, 8, buf);
delay(1); // send data per 100ms
// SERIAL_PORT_MONITOR.println("CAN BUS sendMsgBuf ok!");
}
void PosReadMultiCircle(){
unsigned char buf[8];
long bufPos;
double CurPos = 0;
unsigned char len = 0;
buf[0] = 0x92;
buf[1] = 0x00;
buf[2] = 0x00;
buf[3] = 0x00;
buf[4] = 0x00;
buf[5] = 0x00;
buf[6] = 0x00;
buf[7] = 0x00;
CAN.sendMsgBuf(0x141, 0, 8, buf);
delay(1);
if(CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming
{
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
unsigned long canId = CAN.getCanId();
bufPos = (buf[7] << 48 ) | (buf[6] << 40 ) | (buf[5] << 32 ) | (buf[4] << 24) | (buf[3] << 16 ) | (buf[2] << 8) | buf[1];
CurPos = (double) (bufPos % 35999)/100/9; // 0.01degrees per LSB and the gear ratio of 9:1
SERIAL.print("MultReadCurBufPos : ");
SERIAL.print(CurPos, DEC);
SERIAL.print(" -- ");
for (int i = 1; i<7; i++){
SERIAL.print(buf[i],HEX);
SERIAL.print(",");
}
SERIAL.println(buf[7]);
}
}
void PosReadSingleCircle()
{
unsigned char buf[8];
unsigned int bufPos;
double CurPos = 0;
unsigned char len = 0;
buf[0] = 0x94;
buf[1] = 0x00;
buf[2] = 0x00;
buf[3] = 0x00;
buf[4] = 0x00;
buf[5] = 0x00;
buf[6] = 0x00;
buf[7] = 0x00;
CAN.sendMsgBuf(0x141, 0, 8, buf);
delay(1);
if(CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming
{
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
unsigned long canId = CAN.getCanId();
bufPos = ((buf[7] << 8) | buf[6]);
CurPos = (double)(bufPos % 35999)/100/9;
SERIAL.print(" SingReadCurBufPos : ");
SERIAL.print(CurPos, DEC);
SERIAL.print(" -- ");
SERIAL.print(buf[7],HEX);
SERIAL.print(",");
SERIAL.println(buf[6],HEX);
}
}
2 posts - 1 participant