Skip to content

Commit 01d4703

Browse files
create two different classes for the comm protocol: CAN (CANCommClass) and RSx (RS485CommClass)
1 parent cee0a68 commit 01d4703

File tree

12 files changed

+376
-278
lines changed

12 files changed

+376
-278
lines changed

examples/CAN/ReadCan/ReadCan.ino

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
1111
*/
1212
#include <Arduino_MachineControl.h>
13-
#include <CAN.h>
1413

1514
#define DATARATE_2MB 2000000
1615
#define DATARATE_1_5MB 1500000
@@ -24,16 +23,14 @@ void setup() {
2423
}
2524

2625
Serial.println("Start CAN initialization");
27-
MachineControl_CommProtocols.begin();
28-
MachineControl_CommProtocols.CANEnable();
26+
MachineControl_CANComm.begin(DATARATE_800KB);
2927

30-
MachineControl_CommProtocols.CAN.frequency(DATARATE_800KB);
3128
Serial.println("Initialization done");
3229
}
3330

3431
void loop() {
3532
mbed::CANMessage msg;
36-
if (MachineControl_CommProtocols.CAN.read(msg)) {
33+
if (MachineControl_CANComm.read(msg)) {
3734

3835
// Print the sender ID
3936
Serial.print("ID: ");
@@ -42,7 +39,6 @@ void loop() {
4239
// Print the first Payload Byte
4340
Serial.print("Message received:");
4441
Serial.println(msg.data[0], DEC);
45-
4642
}
4743

4844
delay(100);

examples/CAN/WriteCan/WriteCan.ino

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
1111
*/
1212
#include <Arduino_MachineControl.h>
13-
#include <CAN.h>
1413

1514
#define DATARATE_2MB 2000000
1615
#define DATARATE_1_5MB 1500000
@@ -25,9 +24,7 @@ void setup() {
2524

2625
Serial.println("Start CAN initialization");
2726

28-
MachineControl_CommProtocols.begin();
29-
MachineControl_CommProtocols.CANEnable();
30-
MachineControl_CommProtocols.CAN.frequency(DATARATE_800KB);
27+
MachineControl_CANComm.begin(DATARATE_800KB);
3128
Serial.println("Initialization done");
3229
}
3330

@@ -38,12 +35,12 @@ int payload_size = 1;
3835
void loop() {
3936

4037
mbed::CANMessage msg = mbed::CANMessage(13ul, &payload, payload_size);
41-
if (MachineControl_CommProtocols.CAN.write(msg)) {
38+
if (MachineControl_CANComm.write(msg)) {
4239
Serial.println("Message sent");
4340
} else {
4441
Serial.println("Transmission Error: ");
45-
Serial.println(MachineControl_CommProtocols.CAN.tderror());
46-
MachineControl_CommProtocols.CAN.reset();
42+
Serial.println(MachineControl_CANComm.tderror());
43+
MachineControl_CANComm.reset();
4744
}
4845

4946
delay(100);

examples/RS232/RS232.ino

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,28 @@ void setup()
3333
delay(2500);
3434
Serial.println("Start RS232 initialization");
3535

36-
// Set the PMC Communication Protocols to default config
37-
MachineControl_CommProtocols.begin();
38-
36+
// Set the PMC Communication Protocols to default config and enable the RS485/RS232 system
3937
// RS485/RS232 default config is:
4038
// - RS485/RS232 system disabled
4139
// - RS485 mode
4240
// - Half Duplex
4341
// - No A/B and Y/Z 120 Ohm termination enabled
42+
// Specify baudrate for the communication
43+
MachineControl_RS485Comm.begin(115200);
4444

45-
// Enable the RS485/RS232 system
46-
MachineControl_CommProtocols.RS485Enable();
4745
// Enable the RS232 mode
48-
MachineControl_CommProtocols.RS485SetModeRS232(true);
46+
MachineControl_RS485Comm.setModeRS232(true);
4947

50-
// Specify baudrate for RS232 communication
51-
MachineControl_CommProtocols.RS485.begin(115200);
5248
// Start in receive mode
53-
MachineControl_CommProtocols.RS485.receive();
49+
MachineControl_RS485Comm.receive();
5450

5551
Serial.println("Initialization done!");
5652
}
5753

5854
void loop()
5955
{
60-
if (MachineControl_CommProtocols.RS485.available())
61-
Serial.write(MachineControl_CommProtocols.RS485.read());
56+
if (MachineControl_RS485Comm.available())
57+
Serial.write(MachineControl_RS485Comm.read());
6258

6359
if (millis() > sendNow) {
6460
String log = "[";
@@ -72,14 +68,14 @@ void loop()
7268
Serial.println(log);
7369

7470
// Disable receive mode before transmission
75-
MachineControl_CommProtocols.RS485.noReceive();
71+
MachineControl_RS485Comm.noReceive();
7672

77-
MachineControl_CommProtocols.RS485.beginTransmission();
78-
MachineControl_CommProtocols.RS485.println(msg);
79-
MachineControl_CommProtocols.RS485.endTransmission();
73+
MachineControl_RS485Comm.beginTransmission();
74+
MachineControl_RS485Comm.println(msg);
75+
MachineControl_RS485Comm.endTransmission();
8076

8177
// Re-enable receive mode after transmission
82-
MachineControl_CommProtocols.RS485.receive();
78+
MachineControl_RS485Comm.receive();
8379

8480
sendNow = millis() + sendInterval;
8581
}

examples/RS485_fullduplex/RS485_fullduplex.ino

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,47 +32,43 @@ void setup()
3232
Serial.println("Start RS485 initialization");
3333

3434
// Set the PMC Communication Protocols to default config
35-
MachineControl_CommProtocols.begin();
3635
// RS485/RS232 default config is:
3736
// - RS485 mode
3837
// - Half Duplex
3938
// - No A/B and Y/Z 120 Ohm termination enabled
40-
4139
// Enable the RS485/RS232 system
42-
MachineControl_CommProtocols.RS485Enable();
40+
// Specify baudrate, and preamble and postamble times for RS485 communication
41+
MachineControl_RS485Comm.begin(115200, 0, 500);
4342

4443
// Enable Full Duplex mode
4544
// This will also enable A/B and Y/Z 120 Ohm termination resistors
46-
MachineControl_CommProtocols.RS485SetFullDuplex(true);
47-
48-
// Specify baudrate, and preamble and postamble times for RS485 communication
49-
MachineControl_CommProtocols.RS485.begin(115200, 0, 500);
45+
MachineControl_RS485Comm.setFullDuplex(true);
5046

5147
// Start in receive mode
52-
MachineControl_CommProtocols.RS485.receive();
48+
MachineControl_RS485Comm.receive();
5349

5450

5551
Serial.println("Initialization done!");
5652
}
5753

5854
void loop()
5955
{
60-
if (MachineControl_CommProtocols.RS485.available())
61-
Serial.write(MachineControl_CommProtocols.RS485.read());
56+
if (MachineControl_RS485Comm.available())
57+
Serial.write(MachineControl_RS485Comm.read());
6258

6359
if (millis() > sendNow) {
6460
// Disable receive mode before transmission
65-
MachineControl_CommProtocols.RS485.noReceive();
61+
MachineControl_RS485Comm.noReceive();
6662

67-
MachineControl_CommProtocols.RS485.beginTransmission();
63+
MachineControl_RS485Comm.beginTransmission();
6864

69-
MachineControl_CommProtocols.RS485.print("hello ");
70-
MachineControl_CommProtocols.RS485.println(counter++);
65+
MachineControl_RS485Comm.print("hello ");
66+
MachineControl_RS485Comm.println(counter++);
7167

72-
MachineControl_CommProtocols.RS485.endTransmission();
68+
MachineControl_RS485Comm.endTransmission();
7369

7470
// Re-enable receive mode after transmission
75-
MachineControl_CommProtocols.RS485.receive();
71+
MachineControl_RS485Comm.receive();
7672

7773
sendNow = millis() + sendInterval;
7874
}

examples/RS485_halfduplex/RS485_halfduplex.ino

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ unsigned long counter { 0 };
2323

2424
void setup()
2525
{
26-
2726
Serial.begin(115200);
2827
// Wait for Serial or start after 2.5s
2928
for (auto const timeout = millis() + 2500; !Serial && timeout < millis(); delay(500))
@@ -33,42 +32,38 @@ void setup()
3332
Serial.println("Start RS485 initialization");
3433

3534
// Set the PMC Communication Protocols to default config
36-
MachineControl_CommProtocols.begin();
37-
3835
// RS485/RS232 default config is:
3936
// - RS485 mode
4037
// - Half Duplex
4138
// - No A/B and Y/Z 120 Ohm termination enabled
42-
4339
// Enable the RS485/RS232 system
44-
MachineControl_CommProtocols.RS485Enable();
45-
4640
// Specify baudrate, and preamble and postamble times for RS485 communication
47-
MachineControl_CommProtocols.RS485.begin(115200, 0, 500);
41+
MachineControl_RS485Comm.begin(115200, 0, 500);
42+
4843
// Start in receive mode
49-
MachineControl_CommProtocols.RS485.receive();
44+
MachineControl_RS485Comm.receive();
5045

5146
Serial.println("Initialization done!");
5247
}
5348

5449
void loop()
5550
{
56-
if (MachineControl_CommProtocols.RS485.available())
57-
Serial.write(MachineControl_CommProtocols.RS485.read());
51+
if (MachineControl_RS485Comm.available())
52+
Serial.write(MachineControl_RS485Comm.read());
5853

5954
if (millis() > sendNow) {
6055
// Disable receive mode before transmission
61-
MachineControl_CommProtocols.RS485.noReceive();
56+
MachineControl_RS485Comm.noReceive();
6257

63-
MachineControl_CommProtocols.RS485.beginTransmission();
58+
MachineControl_RS485Comm.beginTransmission();
6459

65-
MachineControl_CommProtocols.RS485.print("hello ");
66-
MachineControl_CommProtocols.RS485.println(counter++);
60+
MachineControl_RS485Comm.print("hello ");
61+
MachineControl_RS485Comm.println(counter++);
6762

68-
MachineControl_CommProtocols.RS485.endTransmission();
63+
MachineControl_RS485Comm.endTransmission();
6964

7065
// Re-enable receive mode after transmission
71-
MachineControl_CommProtocols.RS485.receive();
66+
MachineControl_RS485Comm.receive();
7267

7368
sendNow = millis() + sendInterval;
7469
}

src/Arduino_MachineControl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "RtcControllerClass.h"
1111
#include "USBClass.h"
1212
#include "EncoderClass.h"
13-
#include "COMMClass.h"
13+
#include "CANCommClass.h"
14+
#include "RS485CommClass.h"
1415

1516
#endif /* __ARDUINO_MACHINE_CONTROL_H */

src/CANCommClass.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @file CANCommClass.cpp
3+
* @author Leonardo Cavagnis
4+
* @brief Source file for the CANCommClass used to initialize and interact with the CAN Bus communication protocol on the Portenta Machine Control board.
5+
*/
6+
7+
/* Includes -----------------------------------------------------------------*/
8+
#include "CANCommClass.h"
9+
10+
/* Functions -----------------------------------------------------------------*/
11+
CANCommClass::CANCommClass(PinName can_tx_pin, PinName can_rx_pin) :
12+
mbed::CAN(can_tx_pin, can_rx_pin)
13+
{ }
14+
15+
CANCommClass::~CANCommClass()
16+
{ }
17+
18+
bool CANCommClass::begin(int can_bitrate) {
19+
pinMode(PinNameToIndex(PA_13), OUTPUT); //Disable CAN pin
20+
pinMode(PinNameToIndex(PB_8), OUTPUT);
21+
pinMode(PinNameToIndex(PH_13), OUTPUT);
22+
23+
_enable();
24+
25+
return mbed::CAN::frequency(can_bitrate);
26+
}
27+
28+
void CANCommClass::end() {
29+
_disable();
30+
}
31+
32+
void CANCommClass::_enable() {
33+
digitalWrite(PinNameToIndex(PA_13), LOW);
34+
}
35+
36+
void CANCommClass::_disable() {
37+
digitalWrite(PinNameToIndex(PA_13), HIGH);
38+
}
39+
40+
CANCommClass MachineControl_CANComm;
41+
/**** END OF FILE ****/

src/CANCommClass.h

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* @file CANCommClass.h
3+
* @author Leonardo Cavagnis
4+
* @brief Header file for the CANCommClass used to initialize and interact with the CAN Bus communication protocol on the Portenta Machine Control board.
5+
*
6+
* This library provides a class to manage the CAN Bus communication protocol of the Portenta Machine Control board.
7+
* It allows initializing and interacting with the CAN Bus protocol. The library also initializes the corresponding LED for CAN.
8+
*/
9+
10+
#ifndef __CAN_COMM_CLASS_H
11+
#define __CAN_COMM_CLASS_H
12+
13+
/* Includes -------------------------------------------------------------------*/
14+
#include <Arduino.h>
15+
#include <pinDefinitions.h>
16+
#include <mbed.h>
17+
18+
/* Class ----------------------------------------------------------------------*/
19+
20+
/**
21+
* @class CANCommClass
22+
* @brief Class for managing the CAN Bus communication protocol of the Portenta Machine Control.
23+
*
24+
* The `CANCommClass` is a subclass of `mbed::CAN` and provides methods to work with the CAN Bus communication protocol on the Portenta Machine Control board.
25+
* It includes initialization of the corresponding LED for CAN.
26+
*/
27+
class CANCommClass: public mbed::CAN {
28+
public:
29+
/**
30+
* @brief Construct a CANCommClass object.
31+
*
32+
* This constructor initializes a CANCommClass object with the specified CAN_TX and CAN_RX pins.
33+
*
34+
* @param can_tx_pin The pin for transmitting data on the CAN Bus.
35+
* @param can_rx_pin The pin for receiving data on the CAN Bus.
36+
*/
37+
CANCommClass(PinName can_tx_pin = PB_8, PinName can_rx_pin = PH_13);
38+
39+
/**
40+
* @brief Destruct the CANCommClass object.
41+
*
42+
* This destructor releases any resources used by the CANCommClass object.
43+
*/
44+
~CANCommClass();
45+
46+
/**
47+
* @brief Begin the CAN communication protocol.
48+
*
49+
* This method initializes the CAN communication protocol, including the corresponding LED for CAN.
50+
*
51+
* @param can_bitrate The desired bitrate for the CAN communication protocol.
52+
* @return true If the initialization is successful, false otherwise.
53+
*/
54+
bool begin(int can_bitrate);
55+
56+
/**
57+
* @brief Close the CAN communication protocol.
58+
*
59+
* This method de-initializes the CAN communication protocol, stopping communication on the CAN Bus.
60+
*/
61+
void end();
62+
63+
private:
64+
/**
65+
* @brief Set the CAN transceiver in Normal mode.
66+
*
67+
* In this mode, the CAN transceiver can transmit and receive data via the bus lines CANH and CANL.
68+
*/
69+
void _enable();
70+
71+
/**
72+
* @brief Set the CAN transceiver in Standby (low power) mode.
73+
*
74+
* In this mode, the CAN transceiver will not be able to transmit or correctly receive data via the bus lines.
75+
* The wake-up filter on the output of the low-power receiver does not latch bus dominant states,
76+
* but ensures that only bus dominant and bus recessive states that persist longer than tfltr(wake)
77+
* bus are reflected on pin RXD.
78+
*/
79+
void _disable();
80+
};
81+
82+
extern CANCommClass MachineControl_CANComm;
83+
84+
#endif /* __CAN_COMM_CLASS_H */

0 commit comments

Comments
 (0)