Hello,
I'm writing to you on this forum because I'm really not coping with my problem.
I'm trying to make an Arduino Nano 33 BLE work with an MCP2515 CAN module.
I managed to make an Arduino Nano work with this module and the code below.
But with the BLE, the code compiles normally and nothing happens...
Maybe I missed a detail.
I have seen many articles with the107 library but I can't implement it.
As for my wiring, I'm sure I have the same because I plug my Arduino Nano or my Arduino BLE in the same PCB.
I use this module to drive a rotary motor from T-Motor.
Thanks in advance for your help!
Jacqau11
#include <SPI.h>
#include <mcp2515.h> //Bibliothèque qui prend en charge une fréquence d'oscillation du cristal à 8MHZ
// Définition des extremum pour chaque grandeur
#define P_MIN -12.5f
#define P_MAX 12.5f
#define V_MIN -45.0f
#define V_MAX 45.0f
#define KP_MIN 0.0f
#define KP_MAX 500.0f
#define KD_MIN 0.0f
#define KD_MAX 5.0f
#define T_MIN -15.0f
#define T_MAX 15.0f
// Initialisation des variables
float p_in= 0.0f; // Valeur de position envoyée au moteur
float valeur= 0.0f; // Valeur analogique envoyée par le potar
struct can_frame canMsg;
MCP2515 mcp2515(10); // Le pin CS du HW-184 est branché sur le pin 10 de l'Arduino
void setup() {
//while (!Serial);
Serial.begin(115200); // Fréquence du port-série, à faire correspondre dans la console
mcp2515.reset();
mcp2515.setBitrate(CAN_1000KBPS, MCP_8MHZ); // 1000KBPS = Baudrate du moteur, 8MHZ = fréquence d'oscillation du cristal du HW-184
mcp2515.setNormalMode();
delay(5000);
}
void loop() {
float p_step=0.5;
char rc;
rc = Serial.read();
/*Serial.print(valeur);
Serial.print(" | ");
p_in = (valeur*25.0/1023.0)-12.5;
Serial.println(p_in);*/
if(rc=='o')
{
enterMotorMode(); // La LED verte du moteur doit s'allumer
}
if(rc=='f')
{
exitMotorMode(); // La LED verte doit s'éteindre
}
if (rc=='u')
{
p_in=p_in+p_step;
}
if (rc=='d')
{
p_in=p_in-p_step;
}
valeur = analogRead(A0); // on lit les données du pin A0 sur lequel est branché le potar
//Serial.print(valeur);
//Serial.print(" | ");
p_in = (valeur*25.0/1023.0)-12.5; // on convertit la donnée analogique [0; 1023] en une donnée de position [-12.5; 12.5]
//Serial.println(p_in);
sendToMotor(0x1, p_in, 0.0, 5.0, 2.0, 0.0); // 0x1 = 0xn avec n = CAN_ID du moteur (1 par défaut)
}
void enterMotorMode(){
struct can_frame cf;
cf.can_id = 0x1; // ID du moteur
cf.can_dlc = 8; // longueur du paquet
cf.data[0] = 0xFF;
cf.data[1] = 0xFF;
cf.data[2] = 0xFF;
cf.data[3] = 0xFF;
cf.data[4] = 0xFF;
cf.data[5] = 0xFF;
cf.data[6] = 0xFF;
cf.data[7] = 0xFC;
mcp2515.sendMessage(&cf);
}
void exitMotorMode(){
struct can_frame cf;
cf.can_id = 0x1;
cf.can_dlc = 8;
cf.data[0] = 0xFF;
cf.data[1] = 0xFF;
cf.data[2] = 0xFF;
cf.data[3] = 0xFF;
cf.data[4] = 0xFF;
cf.data[5] = 0xFF;
cf.data[6] = 0xFF;
cf.data[7] = 0xFD;
mcp2515.sendMessage(&cf);
}
void sendToMotor(int mot_id, float pos, float vel, float kp, float kd, float torq){
struct can_frame cf;
unsigned int con_pos = float_to_uint(constrain(pos, P_MIN, P_MAX), P_MIN, P_MAX, 16);
unsigned int con_vel = float_to_uint(constrain(vel, V_MIN, V_MAX), V_MIN, V_MAX, 12);
unsigned int con_kp = float_to_uint(constrain(kp, KP_MIN, KP_MAX), KP_MIN, KP_MAX, 12);
unsigned int con_kd = float_to_uint(constrain(kd, KD_MIN, KD_MAX), KD_MIN, KD_MAX, 12);
unsigned int con_torq = float_to_uint(constrain(torq, T_MIN, T_MAX), T_MIN, T_MAX, 12);
cf.can_id = mot_id;
cf.can_dlc = 8;
cf.data[0] = con_pos>>8;
cf.data[1] = con_pos & 0xFF;
cf.data[2] = con_vel >> 4;
cf.data[3] = ((con_vel&0xF)<<4) | (con_kp>>8);
cf.data[4] = con_kp&0xFF;
cf.data[5] = con_kd>>4;
cf.data[6] = ((con_kd&0xF)<<4) | (con_torq>>8);
cf.data[7] = con_torq&0xFF;
mcp2515.sendMessage(&cf);
}
//Function by Ben Katz:
int float_to_uint(float x, float x_min, float x_max, int bits){
// Converts a float to an unsigned int, given range and number of 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);
}else if(bits == 16){
pgg = (unsigned int) ((x-offset)*65535.0/span);
}
return pgg;
}