Hello everyone,
My problem : my system works erratically when I don't activate Serial.begin. I can't understand why.
I'm building the control system of an electric airship.
Here is my system : I have 1 "Master" Arduino Mega 2560 giving orders to 2 "Slaves" Arduino Nano on a can bus. First the master sends instructions to a slave, the slave answers the order with its measures. Then the master talks to another node.
Inventory :
-Mega Pro CH340G /Atmega2560
x2-Mini Nano V3.0 ATmega328P
x3-CAN bus module MCP2515
I'm powering my system with a TENMA Digital-Control DC Power supply 30V 5A set to 5V, powering the 2 nanos, 2 CAN modules, + 4 servomotors. The master part is powered with USB to a laptop.
x4 servos + 1 brushless motor + 2 DS18b20 + 1 joystick + potentiometers + switches
I'm using the CAN_BUS_SHIELD library from Seeed-Studio.
Here is the code of the Mega, made with FSM :
(pardon my french but I won't rewrite every com)
#include <SPI.h>
#include <Wire.h>
#include <mcp_can.h>
#include <Arduino.h>
#include <U8x8lib.h>
//Définition des états de la FSM
#define STATE_INIT 1
#define STATE_ENVOI_MESSAGE 2
#define STATE_ATTEND_REP 3
#define STATE_GET_INFOS 4
#define STATE_DISPLAY 5
//Définition des noeuds attachés au système
#define EMPENNAGE 1
#define MOT_LAT_DROIT 2
#define MOT_LAT_GAUCHE 3
#define UNDEFINED 4
//Mappage des pins
//joystick
const int pinSW = 14; // digital pin connected to SW D2 (clic joystick)
const int pinX = 56; // analog pin connected to VRx A2
const int pinY = 57; // analog pin connected to VRy A3
//module CAN
const int pinSpiCS_CAN = 15;
//potentiomètre
const int pinPot1 = 54; //A0
//switch
const int pinSwitch = 23;
//objet CAN
MCP_CAN CAN(pinSpiCS_CAN);
U8X8_SSD1306_128X32_UNIVISION_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE); // Adafruit ESP8266/32u4/ARM Boards + FeatherWing OLED
void setup()
{
u8x8.begin();
Serial.begin(115200);
pinMode(pinSW, INPUT); //clic joystick
digitalWrite(pinSW, HIGH);
pinMode(pinSwitch, INPUT_PULLUP);
//initialisation bus CAN
while (CAN_OK != CAN.begin(CAN_1000KBPS, MCP_16MHz))
{
Serial.println("CAN BUS Init Failed");
delay(100);
}
Serial.println("CAN BUS Init OK!");
cli();//stop interrupts
TCCR2A = 0; //default
TCCR2B = 0b00000110; // clk/256 est incrémenté toutes les 16uS
TIMSK2 = 0b00000001; // TOIE2
sei();//allow interrupts
}
//définition/initialisation des variables
byte valPot1 = 0;
byte len = 0;
byte Compteur = 0; //Compteur pour interruption
byte trameReception[4];
byte trameConsigne[4];
byte infos[4][4];
volatile bool flag1s = 0;
static byte state = STATE_ENVOI_MESSAGE;
static byte currentNode = 0;
unsigned long currentMillis = 0;
byte compt = 0;
ISR (TIMER2_OVF_vect) //interruption pour le timer2
{
// 256-6 --> 250X16uS = 4mS
// Recharge le timer pour que la prochaine interruption se déclenche dans 4mS
TCNT2 = 6;
if (Compteur++ == 250) {
Compteur = 0;
flag1s = 1;
}
}
void loop()
{
switch (state) {
case STATE_ENVOI_MESSAGE:
{
//delay(2); //?? n'améliore pas les choses
if (flag1s == 1)
{
state = STATE_DISPLAY;
flag1s = 0;
break;
}
increaseNode();
Serial.println("");
Serial.println("*******************************");
Serial.print(millis());
Serial.print("Communication avec le noeud ");
Serial.println(currentNode);
Serial.println("STATE_ENVOI_MESSAGE");
if (currentNode == EMPENNAGE)
{
trameConsigne[1] = byte(analogRead(pinX) / 4);
trameConsigne[2] = byte(analogRead(pinY) / 4);
trameConsigne[3] = byte(digitalRead(pinSW));
}
if (currentNode == MOT_LAT_DROIT)
{
trameConsigne[1] = byte(analogRead(pinPot1) / 4);
trameConsigne[2] = 0;
trameConsigne[3] = byte(digitalRead(pinSwitch));
}
if (currentNode == MOT_LAT_GAUCHE)
{
}
if (currentNode == UNDEFINED)
{
}
trameConsigne[0] = compt;
compt++;
byte adresseCAN = ((1 << 4) | currentNode );
CAN.sendMsgBuf(adresseCAN, 0, 4, trameConsigne);
state = STATE_ATTEND_REP;
break;
}
case STATE_ATTEND_REP:
Serial.println("STATE_ATTEND_REP");
static bool pasDeMessageDuBonNoeud;
pasDeMessageDuBonNoeud = true;
currentMillis = millis();
while ( ((unsigned long)(millis() - currentMillis) < 6) && pasDeMessageDuBonNoeud )
{
if (CAN_MSGAVAIL == CAN.checkReceive())
{
//Serial.println("Message recu");
CAN.readMsgBuf(&len, trameReception);
if (CAN.getCanId() == ((2 << 4) | currentNode ))
{
afficherMessageSerial();
Serial.println("Message du bon noeud");
pasDeMessageDuBonNoeud = false;
rstErr(); //reset du compteur d'erreur propre au noeud
}
}
}
if (pasDeMessageDuBonNoeud == true)
{
addErr(); //augmente le compteur d'erreur propre au noeud
Serial.print("Pas de rep du noeud ");
Serial.println(currentNode);
state = STATE_ENVOI_MESSAGE;
}
else
{
state = STATE_GET_INFOS;
}
break;
case STATE_GET_INFOS:
Serial.println("STATE_GET_INFOS");
infos[currentNode - 1][1] = trameReception[1];
infos[currentNode - 1][2] = trameReception[2];
infos[currentNode - 1][3] = trameReception[3];
state = STATE_ENVOI_MESSAGE;
break;
case STATE_DISPLAY:
Serial.println("");
Serial.println("-------------------------------");
Serial.println("STATE_DISPLAY");
Serial.println("-------------------------------");
// u8x8.setFont(u8x8_font_px437wyse700b_2x2_r);
u8x8.setFont(u8x8_font_chroma48medium8_r);
u8x8.setCursor(0, 0);
u8x8.print("Emp.:");
u8x8.print(infos[0][0]); //erreur Emp
u8x8.print(" ");
u8x8.setCursor(0, 1);
u8x8.print("MLD.:");
u8x8.print(infos[1][0]); //erreur MLD
u8x8.print(" ");
u8x8.setCursor(8, 1);
if ((infos[1][1] >> 7) == 0) //détermine si c'est une valeur positive ou négative
{
u8x8.print(infos[1][1]); //Temp
}
else
{
u8x8.print("-"); //Temp
u8x8.print(infos[1][1] - 128); //Temp
}
u8x8.setCursor(11, 1);
if ((infos[1][2] >> 7) == 0) //détermine si c'est une valeur positive ou négative
{
u8x8.print(infos[1][2]); //Temp
}
else
{
u8x8.print("-"); //Temp
u8x8.print(infos[1][2] - 128); //Temp
}
u8x8.setCursor(0, 2);
u8x8.print("MLG.:");
u8x8.print(infos[2][0]); //erreur MLG
u8x8.print(" ");
u8x8.setCursor(0, 3);
u8x8.print("Und.:");
u8x8.print(infos[3][0]); //erreur Und
u8x8.print(" ");
state = STATE_ENVOI_MESSAGE;
break;
default:
Serial.println("STATE_DEFAULT");
state = STATE_ENVOI_MESSAGE;
currentNode = 0;
break;
}
//************************* Fonctions *************************
void increaseNode(void) {
if (currentNode == UNDEFINED) {
currentNode = EMPENNAGE;
}
else {
currentNode++;
}
}
void rstErr(void) {
infos[currentNode - 1][0] = 0;
}
void addErr(void) {
if (infos[currentNode - 1][0] < 255)
{
infos[currentNode - 1][0]++;
}
}
void afficherMessageSerial(void) {
unsigned long canId = CAN.getCanId();
Serial.println("-----------------------------");
Serial.print("Data from ID: 0x");
Serial.println(canId, HEX);
for (int i = 0; i < len; i++)
{
Serial.print(trameReception[i]);
Serial.print("\t");
}
Serial.println();
}