Hello,
Can anyone help me with these errors? Im doing my first can bus based project, which is activating and working cube mars/t-motor ak-60 motor actuators. Ive used this code from a youtube video from skyentific, a great video for these. But i cant get it to work, there's many errors, fixed some of them, of whcih using a different can library (seeed-studio) helped. but ive still alot that remain. not sure if some are still library issues like the 'mcp_can' does not name a type. My apologies if i havent posted this correctly i have tried to follow the formatting instructions but im not sure if its correct still.
thanks
ERRORS first code below
Arduino: 1.8.19 (Windows Store 1.8.57.0) (Windows 10), Board: "Arduino Nano, ATmega328P"
C:\Users\user\Documents\Arduino\AK60_JOYSTICK\AK60_JOYSTICK.ino:13:0: warning: "SERIAL" redefined
#define SERIAL Serial
In file included from sketch\AK60_JOYSTICK.ino.cpp:1:0:
C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.57.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\cores\arduino/Arduino.h:54:0: note: this is the location of the previous definition
#define SERIAL 0x0
AK60_JOYSTICK:49:1: error: 'mcp_can' does not name a type
mcp_can CAN (SPI_CS_PIN);
^~~~~~~
C:\Users\user\Documents\Arduino\AK60_JOYSTICK\AK60_JOYSTICK.ino: In function 'void setup()':
AK60_JOYSTICK:57:17: error: 'CAN' was not declared in this scope
while (CAN_OK!= CAN.begin(CAN_1000KBPS))
^~~
C:\Users\user\Documents\Arduino\AK60_JOYSTICK\AK60_JOYSTICK.ino:57:17: note: suggested alternative: 'NAN'
while (CAN_OK!= CAN.begin(CAN_1000KBPS))
^~~
NAN
C:\Users\user\Documents\Arduino\AK60_JOYSTICK\AK60_JOYSTICK.ino: In function 'void loop()':
AK60_JOYSTICK:104:3: error: 'EnterMotorMode' was not declared in this scope
EnterMotorMode();
^~~~~~~~~~~~~~
C:\Users\user\Documents\Arduino\AK60_JOYSTICK\AK60_JOYSTICK.ino:104:3: note: suggested alternative: 'ExitMotorMode'
EnterMotorMode();
^~~~~~~~~~~~~~
ExitMotorMode
AK60_JOYSTICK:114:21: error: 'CAN' was not declared in this scope
if (CAN_MSGAVAIL == CAN.checkReceive())
^~~
C:\Users\user\Documents\Arduino\AK60_JOYSTICK\AK60_JOYSTICK.ino:114:21: note: suggested alternative: 'NAN'
if (CAN_MSGAVAIL == CAN.checkReceive())
^~~
NAN
AK60_JOYSTICK:116:3: error: 'unpack_reply' was not declared in this scope
unpack_reply():
^~~~~~~~~~~~
AK60_JOYSTICK:133:24: error: a function-definition is not allowed here before '{' token
void EnterMotorMode (){
^
C:\Users\user\Documents\Arduino\AK60_JOYSTICK\AK60_JOYSTICK.ino: In function 'void ExitMotorMode()':
AK60_JOYSTICK:157:3: error: 'CAN' was not declared in this scope
CAN.sendMsgBuf(0x01, 0, 8, buf);
^~~
C:\Users\user\Documents\Arduino\AK60_JOYSTICK\AK60_JOYSTICK.ino:157:3: note: suggested alternative: 'NAN'
CAN.sendMsgBuf(0x01, 0, 8, buf);
^~~
NAN
C:\Users\user\Documents\Arduino\AK60_JOYSTICK\AK60_JOYSTICK.ino: In function 'void Zero()':
AK60_JOYSTICK:170:3: error: 'CAN' was not declared in this scope
CAN.sendMsgBuf(0x01, 0, 8, buf);
^~~
C:\Users\user\Documents\Arduino\AK60_JOYSTICK\AK60_JOYSTICK.ino:170:3: note: suggested alternative: 'NAN'
CAN.sendMsgBuf(0x01, 0, 8, buf);
^~~
NAN
C:\Users\user\Documents\Arduino\AK60_JOYSTICK\AK60_JOYSTICK.ino: In function 'void pack_cmd()':
AK60_JOYSTICK:182:24: error: 'float_to_uint' was not declared in this scope
unsigned int p_int = float_to_uint (p_des, P_MIN, P_MAX, 16);
^~~~~~~~~~~~~
AK60_JOYSTICK:184:44: error: 'kP_MIN' was not declared in this scope
unsigned int kp_int = float_to_uint (kp, kP_MIN, P_MAX, 12);
^~~~~~
C:\Users\user\Documents\Arduino\AK60_JOYSTICK\AK60_JOYSTICK.ino:184:44: note: suggested alternative: 'KP_MIN'
unsigned int kp_int = float_to_uint (kp, kP_MIN, P_MAX, 12);
^~~~~~
KP_MIN
AK60_JOYSTICK:196:3: error: 'CAN' was not declared in this scope
CAN.sendMsgBuf(0x01, 0, 8, buf);
^~~
C:\Users\user\Documents\Arduino\AK60_JOYSTICK\AK60_JOYSTICK.ino:196:3: note: suggested alternative: 'NAN'
CAN.sendMsgBuf(0x01, 0, 8, buf);
^~~
NAN
AK60_JOYSTICK:199:20: error: a function-definition is not allowed here before '{' token
void unpack_reply(){
^
AK60_JOYSTICK:217:72: error: a function-definition is not allowed here before '{' token
unsigned int float_to_uint(float x, float x_min, float x_max, int bits){
^
AK60_JOYSTICK:230:77: error: a function-definition is not allowed here before '{' token
float uint_to_float(unsigned int x_int, float x_min, float x_max, int bites){
^
C:\Users\user\Documents\Arduino\AK60_JOYSTICK\AK60_JOYSTICK.ino: At global scope:
AK60_JOYSTICK:248:1: error: expected declaration before '}' token
}
^
exit status 1
'mcp_can' does not name a type
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
}#include <can-serial.h>
#include <mcp2515_can.h>
#include <mcp2515_can_dfs.h>
#include <mcp2518fd_can.h>
#include <mcp2518fd_can_dfs.h>
#include <mcp_can.h>
#include <SPI.h>
#ifdef ARDUINO_SAMD_COMPLIANCE
#define SERIAL SerialUSB
#else
#define SERIAL Serial
#endif
#define UP A1
#define DOWN A2
#define LEFT A3
#define RIGHT A4
#define CLICK A5
#define LED2 8
#define LED3 7
#define P_MIN -12.5f
#define P_MAX 12.5f
#define V_MIN -65.0f
#define V_MAX 65.0f
#define KP_MIN 0.0f
#define KP_MAX 500.0f
#define KD_MIN 0.0f
#define KD_MAX 5.0f
#define T_MIN -18.0f
#define T_MAX 18.0f
float p_in = 0.0f;
float v_in = 0.0f;
float kp_in = 20.0f;
float kd_in = 1.0f;
float t_in = 0.0f;
float p_out = 0.0f;
float v_out = 0.0f;
float t_out = 0.0f;
const int SPI_CS_PIN = 9;
mcp_can CAN (SPI_CS_PIN);
void setup() {
SERIAL.begin (115200);
delay (1000);
while (CAN_OK != CAN.begin(CAN_1000KBPS))
{
SERIAL.println("CAN BUS shield init fail");
SERIAL.println("INIT CAN BUS SHIELD AGAIN");
delay (100);
}
SERIAL.println ("CAN BUS SHIELD INIT OK");
pinMode (UP, INPUT);
pinMode (DOWN, INPUT);
pinMode (LEFT, INPUT);
pinMode (RIGHT, INPUT);
pinMode (CLICK, INPUT);
pinMode (LED2, OUTPUT);
pinMode (LED3, OUTPUT);
digitalWrite (UP, HIGH);
digitalWrite (DOWN, HIGH);
digitalWrite (LEFT, HIGH);
digitalWrite (RIGHT, HIGH);
digitalWrite (CLICK, HIGH);
digitalWrite (LED2, LOW);
digitalWrite (LED3, LOW);
}
long previousMillis = 0;
void loop() {
{
float p_step = 0.001;
delay (200);
if (digitalRead(UP) == LOW)
{
p_in = p_in + p_step;
}
if (digitalRead(DOWN) == LOW)
{
p_in = p_in - p_step;
}
p_in = constrain(p_in, P_MIN, P_MAX);
if (digitalRead(RIGHT) == LOW)
{
EnterMotorMode();
digitalWrite (LED2, HIGH);
}
if (digitalRead (LEFT) == LOW)
{
ExitMotorMode();
digitalWrite (LED2, LOW);
}
pack_cmd();
if (CAN_MSGAVAIL == CAN.checkReceive())
{
unpack_reply():
}
SERIAL.print(millis() - previousMillis);
previousMillis = millis();
SERIAL.print (" ");
SERIAL.print (p_in);
SERIAL.print (" ");
SERIAL.print (p_out);
SERIAL.print (" ");
SERIAL.print (v_out);
SERIAL.print (" ");
SERIAL.print (t_out);
}
void EnterMotorMode () {
byte buf[8];
buf[0] = 0xFF;
buf[1] = 0xFF;
buf[2] = 0xFF;
buf[3] = 0xFF;
buf[4] = 0xFF;
buf[5] = 0xFF;
buf[6] = 0xFF;
buf[7] = 0xFC;
CAN.sendMsgBuf(0x01, 0, 8, buf);
}
}
void ExitMotorMode () {
byte buf[8];
buf[0] = 0xFF;
buf[1] = 0xFF;
buf[2] = 0xFF;
buf[3] = 0xFF;
buf[4] = 0xFF;
buf[5] = 0xFF;
buf[6] = 0xFF;
buf[7] = 0xFD;
CAN.sendMsgBuf(0x01, 0, 8, buf);
}
void Zero () {
byte buf[8];
buf[0] = 0xFF;
buf[1] = 0xFF;
buf[2] = 0xFF;
buf[3] = 0xFF;
buf[4] = 0xFF;
buf[5] = 0xFF;
buf[6] = 0xFF;
buf[7] = 0xFE;
CAN.sendMsgBuf(0x01, 0, 8, buf);
}
void pack_cmd() {
byte buf[8];
float p_des = constrain (p_in, P_MIN, P_MAX);
float v_des = constrain (v_in, V_MIN, V_MAX);
float kp = constrain (kp_in, KP_MIN, KP_MAX);
float kd = constrain (kd_in, KD_MIN, KD_MAX);
float t_ff = constrain (t_in, T_MIN, T_MAX);
unsigned int p_int = float_to_uint (p_des, P_MIN, P_MAX, 16);
unsigned int v_int = float_to_uint (v_des, V_MIN, V_MAX, 12);
unsigned int kp_int = float_to_uint (kp, kP_MIN, P_MAX, 12);
unsigned int kd_int = float_to_uint (kd, KD_MIN, KD_MAX, 12);
unsigned int t_int = float_to_uint (t_ff, T_MIN, T_MAX, 12);
buf[0] = p_int >> 8;
buf[1] = p_int & 0xFF;
buf[2] = v_int >> 4;
buf[3] = ((v_int & 0xF) << 4) | (kp_int >> 8);
buf[4] = kp_int & 0xFF;
buf[5] = kd_int >> 4;
buf[6] = ((kd_int & 0xF) << 4) | (t_int >> 8);
buf[7] = t_int & 0xFF;
CAN.sendMsgBuf(0x01, 0, 8, buf);
void unpack_reply() {
byte len = 0;
byte buf [8];
CAN.readMsgBuf (&len, buf);
unsigned long canId = CAN.getCanId();
unsigned int id = buf[0];
unsigned int p_int = (buf[1] << 8) | buf[2];
unsigned int v_int = (buf[3] << 4) | (buf[4] >> 4);
unsigned int i_int = ((buf[4] & 0xF) << 8) | buf[5];
p_out = uint_to_float(p_int, P_MIN, P_MAX, 16);
v_out = uint_to_float(v_int, V_MIN, V_MAX, 12);
p_out = uint_to_float(t_int, T_MIN, T_MAX, 12);
}
unsigned int float_to_uint(float x, float x_min, float x_max, int bits) {
float span = x_max - x_min;
float offset = x_min;
unsigned int pgg = 0;
if (bits == 12) {
pgg = (unsigned int) ((x - offset) * 4095.0 / span);
}
if (bits == 16) {
pgg = (unsigned int)((x - offset) * 65535.0 / span);
}
return pgg;
}
float uint_to_float(unsigned int x_int, float x_min, float x_max, int bites) {
float span = x_max - x_min;
float offset = x_min;
float pgg = 0;
if (bits == 12) {
pgg = ((float) x_int) * span / 4065.0 + offset;
}
if (bits == 16) {
pgg = ((float)x_int) * span / 65535.0 + offset;
}
return pgg;
}
}