|
| 1 | +--- |
| 2 | +title: 'Arduino Portenta Machine Control Library Guide' |
| 3 | +difficulty: beginner |
| 4 | +compatible-products: [portenta-machine-control] |
| 5 | +description: 'Learn about the new Arduino Portenta Machine Control library, which enables efficient management of the features of the Arduino Portenta Machine Control.' |
| 6 | +tags: |
| 7 | + - Library |
| 8 | + - Portenta Machine Control |
| 9 | +author: 'José Bagur' |
| 10 | +hardware: |
| 11 | + - hardware/05.pro-solutions/solutions-and-kits/portenta-machine-control |
| 12 | +software: |
| 13 | + - ide-v1 |
| 14 | + - ide-v2 |
| 15 | + - arduino-cli |
| 16 | + - web-editor |
| 17 | +--- |
| 18 | + |
| 19 | +## Overview |
| 20 | + |
| 21 | +The Arduino Portenta Machine Control is a versatile industrial control unit for driving machinery. It offers soft-PLC control, diverse input/output options, and flexible network connectivity. The `Arduino_PortentaMachineControl` library enables efficient management of the software and hardware features of the Portenta Machine Control. This library is an upgraded and improved version designed to replace the deprecated [Arduino_MachineControl](https://github.com/arduino-libraries/Arduino_MachineControl) library. The tutorial aims to highlight the recent changes in the library, focusing on its user-friendly approach and adherence to the Arduino ecosystem rules, conventions, and style. |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +## Goals |
| 26 | + |
| 27 | +- Introduce the `Arduino_PortentaMachineControl` library’s latest release key features and capabilities. |
| 28 | +- Show the main differences between the `Arduino_PortentaMachineControl` library and the deprecated `Arduino_MachineControl`. |
| 29 | +- Learn how to port a program using the old library to the new one. |
| 30 | + |
| 31 | +## Hardware and Software Requirements |
| 32 | + |
| 33 | +### Hardware Requirements |
| 34 | + |
| 35 | +- [Portenta Machine Control](https://store.arduino.cc/products/arduino-portenta-machine-control) (x1) |
| 36 | +- [USB-A to Micro-USB cable](https://store.arduino.cc/products/usb-2-0-cable-type-a-micro) (x1) |
| 37 | +- +24 VDC power supply (x1) |
| 38 | + |
| 39 | +***The minimum recommended output current rating for the Portenta Machine Control +24 VDC power supply is 0.5 A*** |
| 40 | + |
| 41 | +### Software Requirements |
| 42 | + |
| 43 | +- [Arduino IDE 2.0+](https://www.arduino.cc/en/software) or [Arduino Web Editor](https://create.arduino.cc/editor) |
| 44 | +- [Arduino_PortentaMachineControl library](https://github.com/arduino-libraries/Arduino_PortentaMachineControl) |
| 45 | + |
| 46 | +## The Arduino Portenta Machine Control Library |
| 47 | + |
| 48 | +The `Arduino_PortentaMachineControl` is a C++ library designed to manage the software and hardware functionalities of the Portenta Machine Control board efficiently. It provides extensive support for inputs such as digital, analog, and encoder ports while offering outputs including digital and analog ports. This library also manages communication through interfaces like CAN bus, RS-232, RS-422, and RS-485, allowing connectivity via Ethernet, Wi-Fi®, Bluetooth® Low Energy and USB. |
| 49 | + |
| 50 | +The library empowers users to easily initialize, control, and access the diverse functionalities of the Portenta Machine Control, enhancing its capability and adaptability for industrial applications. |
| 51 | + |
| 52 | +### Library Features |
| 53 | + |
| 54 | +The `Arduino_PortentaMachineControl` library offers a wide range of functionalities organized for ease of reference: |
| 55 | + |
| 56 | +**Input signals** |
| 57 | + |
| 58 | +- Eight digital inputs at +24 VDC |
| 59 | +- Two channels for encoder readings |
| 60 | +- Three analog inputs for PT100, J, and K temperature probes |
| 61 | +- Three analog inputs for 4-20 mA, 0-10V, and NTC signals |
| 62 | + |
| 63 | +**Output signals** |
| 64 | + |
| 65 | +- Eight digital output terminals at +24 VDC |
| 66 | +- Four analog output terminals, ranging from 0 to 10 VDC |
| 67 | + |
| 68 | +**Programmable I/O** |
| 69 | + |
| 70 | +- 12 programmable digital input/output terminals at +24 VDC |
| 71 | + |
| 72 | +**Communication protocols** |
| 73 | + |
| 74 | +- CAN bus |
| 75 | +- Serial protocols (RS-232, RS-422, and RS-485) |
| 76 | +- USB |
| 77 | + |
| 78 | +**Additional functionalities** |
| 79 | + |
| 80 | +- Real-Time Clock (RTC) |
| 81 | + |
| 82 | +### Improvements Between Libraries |
| 83 | + |
| 84 | +#### Structural and Functional Enhancements |
| 85 | + |
| 86 | +The library has undergone significant structural changes to improve its efficiency and user-friendliness. Some of these key improvements include the following: |
| 87 | + |
| 88 | +- **Library structure revamping**: Transition from a single header file to multiple `.cpp` and `.h` files organized by functionality (check [Library Features](#library-features) section) |
| 89 | +- **Naming refactoring**: Removal of the `machinecontrol namespace`, adopting a more descriptive prefix-based naming system. |
| 90 | +- **Function optimization**: Unused functions have been removed, streamlining the library. |
| 91 | +- **Direct calls to mbed core removed**: This change enhances the library's compatibility and ease of use. |
| 92 | +- **Examples reworking**: Updated examples provide clearer demonstrations of the library's capabilities. |
| 93 | +- **Documentation improvement**: Enhanced documentation offers better support to users. |
| 94 | + |
| 95 | +#### Practical Application Enhancements |
| 96 | + |
| 97 | +The following improvements make the Portenta Machine Control Library more adaptable and efficient for various industrial applications, from machinery control to sensor integration: |
| 98 | + |
| 99 | +- **Input/output management**: Enhanced handling of digital and analog signals for improved accuracy and reliability. |
| 100 | +- **Communication protocol integration**: Improved support for CAN-BUS and serial protocols, facilitating smoother data exchange. |
| 101 | +- **RTC functionality**: More robust RTC management, ensuring precise timekeeping for time-sensitive applications. |
| 102 | + |
| 103 | +## Key Major Improvements |
| 104 | + |
| 105 | +### Naming Conventions |
| 106 | + |
| 107 | +One of the significant updates in the Portenta Machine Control Library is the change in **naming conventions**, which enhances clarity and consistency across the library. |
| 108 | + |
| 109 | +Changes in naming: |
| 110 | + |
| 111 | +- **Removal of `machinecontrol` namespace**: The previous version of the library used the `machinecontrol` namespace. This has been removed to simplify the code and make it more accessible. |
| 112 | +- **Introduction of the `MachineControl_` prefix**: In the updated library, a prefix `MachineControl_` is used for each functionality's singleton object. This change ensures a clearer and more descriptive approach to accessing library functionalities. |
| 113 | + |
| 114 | +#### Example of the Updated Naming |
| 115 | + |
| 116 | +Old naming convention: |
| 117 | + |
| 118 | +```arduino |
| 119 | +// Using the 'machinecontrol' namespace |
| 120 | +using namespace machinecontrol; |
| 121 | +void setup() { |
| 122 | + digital_inputs.init(); |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +New naming convention: |
| 127 | + |
| 128 | +```arduino |
| 129 | +// Prefix 'MachineControl_' used for each functionality |
| 130 | +void setup() { |
| 131 | + MachineControl_DigitalInputs.begin(); |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +This new naming structure aligns with the Arduino programming language standard of using clear and descriptive names, making the library more user-friendly, especially for those new to programming with it. |
| 136 | + |
| 137 | +### Input/Output Modules |
| 138 | + |
| 139 | +In the `Arduino_PortentaMachineControl` Library, interacting with input/output modules has been streamlined for better usability and consistency with Arduino standards. Two new important features are the following: |
| 140 | + |
| 141 | +- **Initializing input/output modules**: Initializing an input/output module is more intuitive in the updated library. The `begin()` function is now used to start a module, providing a clear starting point for module operations. |
| 142 | +- **CamelCase notation**: The library has adopted CamelCase notation for function names, aligning with the standard Arduino library style. This change makes the library more consistent with other Arduino libraries, enhancing readability and ease of use. |
| 143 | + |
| 144 | +#### Example of the Input/Output Module Usage: |
| 145 | + |
| 146 | +Old approach: |
| 147 | + |
| 148 | +```arduino |
| 149 | +// Using the previous library version |
| 150 | +#include <Arduino_MachineControl.h> |
| 151 | +using namespace machinecontrol; |
| 152 | +void setup() { |
| 153 | + analog_out.period_ms(0, 4); |
| 154 | +} |
| 155 | +``` |
| 156 | + |
| 157 | +New approach: |
| 158 | + |
| 159 | +```arduino |
| 160 | +// Using the updated library |
| 161 | +#include <Arduino_PortentaMachineControl.h> |
| 162 | +void setup() { |
| 163 | + MachineControl_AnalogOut.begin(); |
| 164 | + MachineControl_AnalogOut.setPeriod(0, 4); |
| 165 | +} |
| 166 | +``` |
| 167 | + |
| 168 | +In the new approach, the `begin()` function is explicitly called to initialize the `MachineControl_AnalogOut` module. Following the CamelCase notation, the `setPeriod()` method replaces the older `period_ms()` function, making the function's purpose clearer and the code more consistent with Arduino standards. |
| 169 | + |
| 170 | +This update enhances the user experience by making the code more intuitive and aligned with the familiar Arduino programming style. |
| 171 | + |
| 172 | +### Encoders |
| 173 | + |
| 174 | +The handling of encoders has been updated in the `Arduino_PortentaMachineControl` Library for a more streamlined and intuitive approach. One key new feature is the **removal of array object access**. In the previous library version, encoder objects were accessed using array notation (`[]`). The latest version has simplified this approach, enhancing code readability and reducing potential errors. |
| 175 | + |
| 176 | +#### Example of Encoder Usage |
| 177 | + |
| 178 | +Old approach: |
| 179 | + |
| 180 | +```arduino |
| 181 | +// Using the previous library version |
| 182 | +#include <Arduino_MachineControl.h> |
| 183 | +using namespace machinecontrol; |
| 184 | +int rev; |
| 185 | +void setup() { |
| 186 | + rev = encoders[0].getRevolutions(); |
| 187 | +} |
| 188 | +``` |
| 189 | + |
| 190 | +New approach: |
| 191 | + |
| 192 | +```arduino |
| 193 | +// Using the updated library |
| 194 | +#include <Arduino_PortentaMachineControl.h> |
| 195 | +int rev; |
| 196 | +void setup() { |
| 197 | + rev = MachineControl_Encoders.getRevolutions(0); |
| 198 | +} |
| 199 | +``` |
| 200 | + |
| 201 | +In the new approach, the `getRevolutions()` function is called directly on the `MachineControl_Encoders` object rather than accessing an encoder array. The encoder of interest is specified as a parameter in the function call, making the code more explicit and easier to understand. |
| 202 | + |
| 203 | +This update to the encoder module in the Portenta Machine Control Library simplifies the interaction with encoders, making it more straightforward for users to implement in their projects. |
| 204 | + |
| 205 | +### Communication Interfaces |
| 206 | + |
| 207 | +The latest Portenta Machine Control Library update has refined how communication interfaces are managed. The updated library version moves away from using public objects for communication protocols; this change leads to a more encapsulated and modular approach, enhancing the robustness and clarity of the communication code. |
| 208 | + |
| 209 | +#### Example of Communication Protocol Usage |
| 210 | + |
| 211 | +Let's use the **RS-485 interface** as example to show the new communication protocol usage. |
| 212 | + |
| 213 | +Old approach: |
| 214 | + |
| 215 | +```arduino |
| 216 | +// Using the previous library version |
| 217 | +#include <Arduino_MachineControl.h> |
| 218 | +using namespace machinecontrol; |
| 219 | +void setup() { |
| 220 | + comm_protocols.init(); |
| 221 | + comm_protocols.rs485.begin(115200, 0, 500); |
| 222 | + comm_protocols.rs485.receive(); |
| 223 | +} |
| 224 | +``` |
| 225 | + |
| 226 | +New approach: |
| 227 | + |
| 228 | +```arduino |
| 229 | +// Using the updated library |
| 230 | +#include <Arduino_PortentaMachineControl.h> |
| 231 | +void setup() { |
| 232 | + MachineControl_RS485Comm.begin(115200, 0, 500); |
| 233 | + MachineControl_RS485Comm.receive(); |
| 234 | +} |
| 235 | +``` |
| 236 | + |
| 237 | +The new approach directly handles communication initialization and data reception through the `MachineControl_RS485Comm object`. This design eliminates the need to initialize a separate communication protocol object (`comm_protocols`) and then access its members, making maintaining the code more straightforward. |
| 238 | + |
| 239 | +These improvements in the communication module of the Portenta Machine Control Library are part of the effort to make the library more user-friendly and aligned with best practices in software design. |
| 240 | + |
| 241 | +### Temperature Sensors |
| 242 | + |
| 243 | +The Portenta Machine Control Library's latest update has restructured the handling of temperature sensors, dividing the functionalities into two distinct classes for improved clarity and usability. The two separate classes are the following: |
| 244 | + |
| 245 | +1. **Temperature RTD**: Specifically for Resistance Temperature Detectors (RTD). |
| 246 | +2. **Temperature TC**: Dedicated to Thermocouple (TC) temperature sensors. |
| 247 | + |
| 248 | +This separation ensures a more organized and intuitive approach to temperature sensing, allowing users to work more efficiently with the specific type of sensor they are using. |
| 249 | + |
| 250 | +#### Example of Temperature Sensor Usage |
| 251 | + |
| 252 | +Old approach: |
| 253 | + |
| 254 | +```arduino |
| 255 | +// Using the previous library version |
| 256 | +#include <Arduino_MachineControl.h> |
| 257 | +using namespace machinecontrol; |
| 258 | +void setup() { |
| 259 | + temp_probes.tc.begin(); |
| 260 | + temp_probes.enableTC(); |
| 261 | +} |
| 262 | +``` |
| 263 | + |
| 264 | +New approach: |
| 265 | + |
| 266 | +```arduino |
| 267 | +// Using the updated library |
| 268 | +#include <Arduino_PortentaMachineControl.h> |
| 269 | +void setup() { |
| 270 | + MachineControl_TCTempProbe.begin(); |
| 271 | +} |
| 272 | +``` |
| 273 | + |
| 274 | +In the new approach the initialization of the thermocouple temperature probe is done directly through the `MachineControl_TCTempProbe` object. This design removes the need for accessing a general temperature probe object (`temp_probes`) and its specific member for thermocouples, streamlining the code and making it more straightforward. |
| 275 | + |
| 276 | +These enhancements in managing temperature sensors within the Portenta Machine Control Library simplify integrating and utilizing temperature sensors in industrial applications. |
| 277 | + |
| 278 | +## Conclusion |
| 279 | + |
| 280 | +In this tutorial, we have explored the features of the `Arduino_PortentaMachineControl` library, specifically designed for the Portenta Machine Control. Covering everything from the library's enhanced structure to its modules like I/O, encoders, and communication interfaces, our goal has been to provide you with a thorough understanding of these improvements. |
| 281 | + |
| 282 | +Whether you're a newcomer or an experienced Arduino user, these insights are designed to improve your ability to implement this library effectively in your projects. We encourage you to experiment with what you've learned here and share your experiences with the Arduino community. Thank you for following this guide, and we hope it has been a valuable resource in your journey with Arduino. Your feedback is always welcome! |
0 commit comments