Basics of Robotics and communication protocols

Robotics is a rapidly growing field that involves the design, construction, and programming of machines that can perform tasks autonomously. Robots can be used for a wide range of applications, including manufacturing, healthcare, agriculture, and military operations. However, for robots to function effectively, they require a system for communication between their various components. In this article, we will discuss the importance of communication protocols in robotics and explore some examples of communication protocols commonly used in robotics.

 

Before diving into communication protocols, it’s important to define some key terminology used in robotics.

 

  1. Actuators – Actuators are devices used in robots that convert electrical signals into physical motion. Examples of actuators include motors, pneumatic cylinders, and hydraulic cylinders.

  2. Sensors – Sensors are devices used to detect and measure physical properties such as light, temperature, and pressure. Examples of sensors used in robots include cameras, sonar sensors, and proximity sensors.

  3. Microcontroller – A microcontroller is a small computer chip that is used to control the behavior of a robot. It contains a processor, memory, and input/output peripherals.

  4. Communication Protocol – A communication protocol is a set of rules and standards used to facilitate communication between devices. In robotics, communication protocols are used to enable different components of a robot to exchange information with each other.

Communication Protocols

Communication protocols are a vital component of any robotic system. They enable different components of a robot to communicate with each other, allowing the robot to function effectively. There are various communication protocols used in robotics, and we will explore some of the most commonly used ones.

 

Serial Peripheral Interface (SPI)

 

SPI is a synchronous communication protocol commonly used in embedded systems such as microcontrollers. It involves a master-slave architecture, where one device (the master) controls the communication, and one or more devices (the slaves) respond to commands from the master. SPI uses four wires for communication, including a clock signal, data in, data out, and a chip select signal.

 

The following is an example of how SPI communication can be implemented in a microcontroller:

    
#include 

void setup() {
  // Initialize SPI communication
  SPI.begin();
}

void loop() {
  // Send data to a slave device
  SPI.transfer(0x55);

  // Receive data from a slave device
  byte data = SPI.transfer(0x00);
}

    

In this example, the SPI.begin() function is used to initialize the SPI communication. The SPI.transfer() function is used to send and receive data to and from a slave device. The first argument of the transfer() function is the data to be sent, and the second argument is the data to be received.

Example: Try this at home...

You can practice the above communication protocols at home using the following code in Arduino Boards. 

 

Board 1 – Master

    
#include 

void setup() {
  // Initialize SPI communication
  SPI.begin();
  
  // Set Slave Select (SS) pin as output
  pinMode(SS, OUTPUT);
}

void loop() {
  // Select the slave device
  digitalWrite(SS, LOW);
  
  // Send data to the slave device
  SPI.transfer(50);
  
  // Deselect the slave device
  digitalWrite(SS, HIGH);
  
  // Wait for a moment
  delay(1000);
}
    

Board 2 (Slave)

    
#include 

void setup() {
  // Initialize SPI communication
  SPI.begin();
  
  // Set Slave Select (SS) pin as input
  pinMode(SS, INPUT);
  
  // Set data received as output
  pinMode(MISO, OUTPUT);
}

void loop() {
  // Wait for data to be received
  while (digitalRead(SS) == HIGH);
  
  // Receive data from the master
  byte data = SPI.transfer(0);
  
  // Send the received data back to the master
  SPI.transfer(data);
}
    

In this example, two Arduino boards are connected using SPI communication. The SPI.begin() function is used to initialize the SPI communication on both boards. On the master board, the digitalWrite() function is used to select the slave device, and the SPI.transfer() function is used to send data to the slave device. On the slave board, the digitalRead() function is used to wait for data to be received, and the SPI.transfer() function is used to receive and send data back to the master board.

 

 

Inter-Integrated Circuit (I2C)

 

I2C is a serial communication protocol commonly used in embedded systems. It involves a master-slave architecture, where one device (the master) controls the communication, and one or more devices (the slaves) respond to commands from the master. I2C uses two wires for communication, including a clock signal and a data signal.

The following is an example of how I2C communication can be implemented in a microcontroller:

    
#include 

void setup() {
  // Initialize I2C communication
  Wire.begin();
}

void loop() {
  // Send data to a slave device
  Wire.beginTransmission(0x3C);
  Wire.write(0x00);
  Wire.endTransmission();

  // Receive data from a slave device
  Wire.requestFrom(0x3C, 1);
  byte data = Wire.read();
}
    

In this example, the Wire.begin() function is used to initialise the I2C communication. The Wire.beginTransmission() function is used to begin the transmission of data to a slave device with an address of 0x3C. The Wire.write() function is used to send the data 0x00 to the slave device. The Wire.endTransmission() function is used to end the transmission.

 

The Wire.requestFrom() function is used to request data from a slave device with an address of 0x3C. The 1 argument specifies the number of bytes to request. The Wire.read() function is used to read the data received from the slave device.

Example: Try this at home...

You can practice the above communication protocols at home using the following code in Arduino Boards. 

 

Sensor 1 – Master

    
#include 

void setup() {
  // Initialize I2C communication as a master
  Wire.begin();
  
  // Start a communication with the slave sensor
  Wire.beginTransmission(8);
  
  // Send a command to the slave sensor
  Wire.write(0x01);
  
  // End the communication with the slave sensor
  Wire.endTransmission();
}

void loop() {
  // Request data from the slave sensor
  Wire.requestFrom(8, 2);
  
  // Read the received data
  byte msb = Wire.read();
  byte lsb = Wire.read();
  
  // Combine the MSB and LSB into a single value
  int data = (msb << 8) | lsb;
  
  // Print the received data
  Serial.println(data);
  
  // Wait for a moment
  delay(1000);
}

    

Sensor 2:

    
#include 

void setup() {
  // Initialize I2C communication as a slave
  Wire.begin(8);
  
  // Attach a function to handle requests from the master
  Wire.onRequest(sendData);
}

void loop() {
  // Do nothing
}

void sendData() {
  // Generate some data
  int data = analogRead(A0);
  
  // Split the data into MSB and LSB
  byte msb = (data >> 8) & 0xFF;
  byte lsb = data & 0xFF;
  
  // Send the data back to the master
  Wire.write(msb);
  Wire.write(lsb);
}
    

In this example, two sensors are connected using I2C communication. Sensor 1 is acting as the master and Sensor 2 is acting as the slave. The Wire.begin() function is used to initialize I2C communication on both sensors. On Sensor 1, the Wire.beginTransmission() function is used to start a communication with Sensor 2, and the Wire.write() function is used to send a command to Sensor 2.

 

On Sensor 2, the Wire.begin() function is used to initialize I2C communication as a slave, and the Wire.onRequest() function is used to attach a function to handle requests from the master. In the sendData() function, the analogRead() function is used to generate some data, which is then split into MSB and LSB using bit shifting. Finally, the Wire.write() function is used to send the data back to the master.

 

Universal Asynchronous Receiver-Transmitter (UART)

UART is a communication protocol commonly used in embedded systems. It involves a point-to-point architecture, where two devices communicate directly with each other. UART uses two wires for communication, including a data signal and a clock signal. Unlike SPI and I2C, UART does not use a master-slave architecture.

The following is an example of how UART communication can be implemented in a microcontroller:

    
void setup() {
  // Initialize UART communication
  Serial.begin(9600);
}

void loop() {
  // Send data to a device
  Serial.write(0x55);

  // Receive data from a device
  byte data = Serial.read();
}

    

In this example, the Serial.begin() function is used to initialize the UART communication with a baud rate of 9600. The Serial.write() function is used to send data to a device. The Serial.read() function is used to read data received from a device.

Example: Try this at home...

The following is an example of how UART communication can be implemented between two Arduino boards:

 

Board 1 (Transmitter):

    
void setup() {
  // Initialize UART communication at 9600 baud rate
  Serial.begin(9600);
}

void loop() {
  // Send a message to the receiver
  Serial.print("Hello, world!");
  
  // Wait for a moment
  delay(1000);
}

    

Board 2 (Receiver):

    
void setup() {
  // Initialize UART communication at 9600 baud rate
  Serial.begin(9600);
}

void loop() {
  // Check if a message is available
  if (Serial.available() > 0) {
    // Read the incoming message
    String message = Serial.readString();
    
    // Print the message
    Serial.println(message);
  }
}
    

In this example, two Arduino boards are connected using UART communication. On Board 1, the Serial.begin() function is used to initialize UART communication at a baud rate of 9600. The Serial.print() function is used to send a message to Board 2. On Board 2, the Serial.begin() function is used to initialize UART communication at a baud rate of 9600. The Serial.available() function is used to check if a message is available, and the Serial.readString() function is used to read the incoming message.

 

Finally, the received message is printed using the Serial.println() function.

 

Controller Area Network (CAN)

CAN is a communication protocol commonly used in automotive and industrial applications. It involves a multi-master architecture, where multiple devices can communicate with each other. CAN uses two wires for communication, including a CAN high wire and a CAN low wire.

 

The following is an example of how CAN communication can be implemented in a microcontroller:

    
#include SPI.h
#include mcp2515.h

MCP2515 CAN;

void setup() {
  // Initialize CAN communication
  CAN.begin(MCP_ANY, MCP_16MHz, MCP_500kBPS);
  CAN.setMode(MCP_NORMAL);
}

void loop() {
  // Send data on CAN bus
  CAN.sendMsgBuf(0x123, 0, 8, "HelloCAN");

  // Receive data from CAN bus
  if(CAN.checkMessage()) {
    CAN.readMsgBuf(&id, &len, data);
  }
}
    

In this example, the MCP2515 library is used to implement CAN communication. The CAN.begin() function is used to initialize the CAN communication with a baud rate of 500 kbps. The CAN.setMode() function is used to set the mode of the MCP2515 chip to normal mode.

 

The CAN.sendMsgBuf() function is used to send data on the CAN bus. The first argument specifies the CAN message ID, the second argument specifies the message priority, the third argument specifies the message length, and the fourth argument is a pointer to the data to be sent.

 

The CAN.checkMessage() function is used to check if a message has been received on the CAN bus. The CAN.readMsgBuf() function is used to read the message ID, message length, and message data received from the CAN bus.

Communication protocols are a crucial component of any robotic system. They enable different components of a robot to communicate with each other, allowing the robot to function effectively. In this article, we have explored some examples of communication protocols commonly used in robotics, including SPI, I2C, and UART.

 

Understanding these communication protocols is essential for anyone working in the field of robotics. With the right communication protocols in place, robots can perform a wide range of tasks autonomously, making them valuable assets in a variety of industries.

Leave a Comment

Your email address will not be published. Required fields are marked *

×

Hey!

Please click below to start the chat!

× Let's chat?