What is Robot Kinematics?

Kinematics is about how you convert the position and velocity of an end effector (e.g. gripper, robotic hand, etc.) to position and velocity of the joints (i.e. the servo motors along a robotic arm), and vice-versa. 

Let’s take a look at the vocabulary of that sentence.

An end effector is the component on a robot arm that has an effect on the environment. For example, consider this robotic arm below. The end effector is the silver robotic gripper.

6-dof-robotic-arm-2

An end effector could also be a robotic hand or a vacuum gripper.

A joint on a robot is like a joint on the human body (shoulder, elbow, et.). A joint is the moveable part of a robot. 

For a typical robotic arm, a joint is a motor that connects one link to another link. To continue the analogy of the human body, the lower arm and upper arm links are connected by the elbow joint.

In the photo of the robotic arm above, there are 6 joints (i.e. 6 motors). Going from the base of the robot to the gripper, you have a:

  • Base joint (Motor 0)
  • Shoulder joint (Motor 1)
  • Elbow joint (Motor 2)
  • Wrist joint (Motor 3)
  • Hand joint (Motor 4)
  • Gripper Joint (i.e. the silver claw) (Motor 5)

Links (i.e. the black brackets in the robotic arm above) are the stiff components that connect the joints (i.e. servo motors) together.

Robotic Arm With Vacuum Suction Cup for Pick and Place

In this tutorial, we will build a robotic arm with a vacuum suction cup that can enable you to pick up items in one location and place them in another.

Our goal is to build an early prototype of a product that can make it easier and faster for factories and warehouses to do their work.

Real-World Applications

Robotic arm systems have a number of real-world applications. Here are just a few examples: 

Not only do robotic arms help solve labor shortages, but they also help increase productivity when they work alongside humans. In this video from The Wall Street Journal, you can see how robotic arms working side-by-side with humans can enhance productivity.

covariant-ai-robot-small
Source: YouTube – A robot made by Covariant

We will build an early prototype of the products you see above. Let’s get started!

Prerequisites

You Will Need

This section is the complete list of components you will need for this project: 

Robot Arm Kit (Assembled) – Go to ebay and type “Air Pump Robotic Arm Kit” or go to Aliexpress and type “Robotic arm vacuum suction pump”

  • 2 x KS-3620 180° Servo (Suitable Voltage: 4.8 – 6.6V; No-load Current: 80-100mA)
  • 1 x KS-3620 270° Servo  (Suitable Voltage: 4.8 – 6.6V; No-load Current: 80-100mA)
  • 1 x Micro Air (Vacuum) Pump (Suitable Voltage: 3.7V – 6V; Rated current: <0.4A)
  • 1 x Solenoid Valve – Electrically controlled valve that opens up and lets air through when it receives the proper voltage – (Suitable Voltage 3V – 6V; Rated current: 0.14A)
  • 1 x Silicone Tubing Hose (2mm inner diameter)

Robot Suction Cup Vacuum Pump Kit For 25T (i.e. 25 Teeth Servo Spline) Servos (check ebay or Aliexpress)

  • 1 x Set of Silicone Suction Cup (Dual)
  • 2 x PWM Electronic Switches
  • 1 x Vacuum Pump
  • 1 x 800mm Silicone Hose
  • 1 x Tee-Joint Electronic Valve (also known as Solenoid Valve)

Extra Components You’ll Need

Getting Started

Test the Servo Motors

The first thing we need to do is to test the three servo motors. 

Here is the wiring diagram in pdf format. 

We need to power the servo motors with an external power supply because they draw a lot of current…so much current that it would damage your Arduino Mega if you were to connect the power leads directly.

Here are two different programs you can use to test. The first program enables you to control the position of the servos directly using the potentiometers. The second program sweeps each motor back and forth.

2020-07-11-081518

When you launch these programs on the robot, I recommend you turn on the external power first. Then plug the 9V battery into the Arduino.

/*
Program: Control 3 Servos Using Arduino and Potentiometers
File: control_3_servos_with_potentiometer_varspeedservolib.ino
Description: Turn the knob of the potentiometers 
             to control the angle of the 3 servos.
             This program enables you to control the speed of the servos
             as well.
Author: Addison Sears-Collins
Website: https://automaticaddison.com
Date: July 10, 2020
*/
 
#include <VarSpeedServo.h> 

// Define the number of servos
#define SERVOS 3

// Create the servo objects.
VarSpeedServo myservo[SERVOS]; 

// Speed of the servo motors
// Speed=1: Slowest
// Speed=255: Fastest.
const int desired_speed = 255;

// Attach servos to digital pins on the Arduino
int servo_pins[SERVOS] = {3,5,6};

// Analog pins used to connect the potentiometers
int potpins[SERVOS] = {A0,A1,A2}; 

// Variables to read the value from the analog pin
int potpin_val[SERVOS]; 

void setup() {
  
  for(int i = 0; i < SERVOS; i++) {
    
    // Attach the servos to the servo object 
    // attach(pin, min, max  ) - Attaches to a pin 
    // setting min and max values in microseconds
    // default min is 544, max is 2400 
    myservo[i].attach(servo_pins[i], 544, 2400);  
  }
}
 
void loop() {  

  // Update servo position
  for(int i = 0; i < SERVOS; i++) {
    potpin_val[i] = analogRead(potpins[i]);
    potpin_val[i] = map(potpin_val[i], 0, 1023, 0, 180);
    myservo[i].write(potpin_val[i], desired_speed, true);
  }
}    
/*
Program: Control 3 Servos Using Arduino and Sensor Shield v5.0
File: move_3_servo_motors_to_angle.ino
Description: Move servo motors to a specific angle
Author: Addison Sears-Collins
Website: https://automaticaddison.com
Date: July 10, 2020
*/
 
#include <VarSpeedServo.h> 

// Define the number of servos
#define SERVOS 3

// Create the servo objects.
VarSpeedServo myservo[SERVOS]; 

// Speed of the servo motors
// Speed=1: Slowest
// Speed=255: Fastest.
const int desired_speed = 25;

// Attach servos to digital pins on the Arduino
int servo_pins[SERVOS] = {3,5,6};

void setup() {
  
  for(int i = 0; i < SERVOS; i++) {
    
    // Attach the servos to the servo object 
    myservo[i].attach(servo_pins[i]);  
  }
}
 
void loop() {  

  // Move each servo back and forth
  for(int i = 0; i < SERVOS; i++) {
    myservo[i].write(60, desired_speed, true); 
    myservo[i].write(120, desired_speed, true);   
    myservo[i].write(60, desired_speed, true); 
    delay(250);
  }
  delay(250); 
}     

Connect the Components

Now that you’ve tested the motors, it is time to connect everything else.

Get all the components that you need to build this project, and lay them out on a table. Now is a good time to double check that you have everything you need.

2020-07-15-181642

Wire up all the components. You can use either this diagram or this diagram depending on your preference. 

I suggest downloading the wiring diagram and then zooming in so you can see everything. 

Don’t be intimidated by all the connections. Just go one part and one wire at a time. Take it slowly so that you wire everything up correctly.

For the two solenoid wires and the momentary push button switch, it doesn’t matter which one is Ground and which one is VCC (i.e. positive voltage).

Launch Manual Suction Control

We will control our robot manually using the three potentiometers. Load the control_3_servos_with_potentiometer_varspeedservolib.ino program (you made earlier) to your Arduino.

Now plug in everything so that your program is running. The power supply I’m using is 6V for the voltage with a 3A current limit.

2020-07-15-195316
2020-07-15-195334
2020-07-15-195358
2020-07-15-195406
2020-07-15-195426
2020-07-15-195439
2020-07-15-195459
2020-07-15-195450

You may notice in the beginning that the servos jump a bit when you first launch the program. That’s totally normal.

In a real-world setting, you would want to consider programming the robot so that you can press a button and return the servos to the home position before you shut it down. Then, when you restart the program, the robotic arm will initialize in the home position. This way, you won’t have an arm flying around when you launch the arm. 

Using the potentiometers to control the angles of the servos, move the suction cup to a specific object you want to pick up.

When the suction cup reaches the object, push and hold down quickly on the object in order to pick it up.

Then move the servos to your desired drop location.

When ready, release the suction by pushing the momentary push button switch.

Launch Automatic Suction Control

We will now use a force sensitive resistor to control when to deactivate suction. A force sensitive resistor detects physical pressure, squeezing, and weight.

We will use this resistor to automatically detect when the suction cup has made contact with an object. This component will therefore help us automate the process of picking and placing an object.

Test the Force Sensitive Resistor

Let’s begin by writing a small program to test the force sensitive resistor.

Here is the wiring diagram in pdf format.

2020-07-17-155722
2020-07-17-163542

Here is the code. I saved the file as test_force_sensitive_resistor.ino:

/* FSR simple testing sketch. 
 
Connect one end of FSR to power, the other end to Analog 5.
Then connect one end of a 10K resistor from Analog 5 to ground 
 
For more information see www.ladyada.net/learn/sensors/fsr.html */
 
int fsrPin = A5;     // the FSR and 10K pulldown are connected to A5
int fsrReading;     // the analog reading from the FSR resistor divider
 
void setup(void) {
  // We'll send debugging information via the Serial monitor
  Serial.begin(9600);   
}
 
void loop(void) {
  fsrReading = analogRead(fsrPin);  
 
  Serial.print("Analog reading = ");
  Serial.print(fsrReading);     // the raw analog reading
 
  // We'll have a few threshholds, qualitatively determined
  if (fsrReading < 10) {
    Serial.println(" - No pressure");
  } else if (fsrReading < 200) {
    Serial.println(" - Light touch");
  } else if (fsrReading < 500) {
    Serial.println(" - Light squeeze");
  } else if (fsrReading < 800) {
    Serial.println(" - Medium squeeze");
  } else {
    Serial.println(" - Big squeeze");
  }
  delay(200);
} 

For a full description of the force sensitive resistor, check out this post on Adafruit.com

Load the code to your Arduino.

With your USB still plugged in, run the code, and open the Serial Monitor in the Arduino IDE. You will need to click the green magnifying glass in the upper-right of the IDE.

Press the round part of the force sensitive resistor with your finger, and observe the output on the Serial Monitor.

2020-07-17-155359JPG

I highly recommend soldering the Force Sensitive Resistor to male-to-male solder wires. 

If you’ve never soldered before, there are a ton of good YouTube videos on how to do it. You can also check this link where I did some soldering for a robotic car.

Install the Force Sensitive Resistor on the Robot

Cover the head (round part) of the force sensitive resistor in order to protect it. I used some cling wrap and tape to protect it.

2020-07-20-210325

Secure the cling wrap over the force sensitive resistor using some scotch tape.

2020-07-20-210330

Now grab one of the big washers (with 1/2 in. inner diameter and 2 in. outer diameter).

Cut some small pieces of Scotch permanent mounting tape, and place it around the hole.

2020-07-25-160545
2020-07-25-173422

Slide the washer over the tube until it sits on top of the nut above the suction cup.

Now grab the other big washer (with 1/2 in. inner diameter and 2 in. outer diameter).

Tape it to the end-effector of the robotic arm using Scotch permanent mounting tape.

Grab the force sensitive resistor and tape it to the big washer that is attached to the robotic arm (i.e. the upper washer). The two wires should flow out through the back of the robotic arm.

The front of the force sensitive resistor should face upward right against the tape.

2020-07-25-173447

Take the tube and thread it through the hole in the robotic arm.

Place a washer and then a nut down over the tube so that both sit on top of the end effector.

Using your fingers, secure the nut on top of the washer. Do not secure it tightly…just enough so that it isn’t loose (We’ll come back to this screw in the next section)

2020-07-25-173453

Pictures are worth 1000 words, so here is how the setup should look when you’re done.

2020-07-25-173507
2020-07-25-173516
2020-07-25-173557

Calibrate the Robotic Arm With Force Sensitive Resistor

Now, we need to adjust the nut that sits on top of the end effector to the appropriate tightness.

Connect the force sensitive resistor according to the wiring diagram here in pdf format.

Load test_force_sensitive_resistor.ino to your Arduino.

With your USB still plugged in, run the code, and open the Serial Monitor in the Arduino IDE. You will need to click the green magnifying glass in the upper-right of the IDE.

You should see the reading “no pressure” on your Serial Monitor.

Tighten the nut until you see a reading of “Light touch,” “Light squeeze,” or “Medium squeeze.” 

Now, loosen the nut until you see the first reading of “No pressure”. 

To test to see if everything is working properly, with your hand (no need to turn on the motors), guide the robotic arm towards an object. 

Press the suction cup down on an object and then pull it off the object. 

  1. Each time you press the suction cup down on an object you should see either “Light touch,” “Light squeeze,” “Medium squeeze,” or “Big squeeze.”
  2. When the suction cup isn’t touching anything, you should see “No pressure.”

Once you’ve got 1 and 2 above, your robotic arm with force sensitive resistor is calibrated properly.

Have patience. It takes a while to secure the nut to just the right tightness. You want it not too tight but not too loose.

Test the Solenoid Valve With PWM Electronic Switch 

Let’s test our solenoid valve to see if it is working properly. You will need to wire everything up like you see in this diagram.

2020-07-31-155515

If you are using a DC variable power supply, like I am, set it for 0.5A for the current limit and 6V for the voltage.

Where did I get 0.5A from? I know the current ratings for the vacuum pump and the solenoid, so I’m considering a 0.3A max for the vacuum pump and 0.2A max for the solenoid so that I don’t destroy them by allowing too much current to flow through them.

Now, write the following code and upload it to your Arduino. This code makes the vacuum suction cup turn ON for five seconds and then turn OFF for five seconds

/*
Program: Test Solenoid Valve With PWM Electronic Switch
File: test_solenoid_valve.ino
Description: This program tests a solenoid valve 
  with electronic switch to see if it is working
  properly.
Author: Addison Sears-Collins
Website: https://automaticaddison.com
Date: July 29, 2020
*/

#include <VarSpeedServo.h> 

// Create a solenoid valve control object
VarSpeedServo my_solenoid_valve;

// Attach solenoid to digital pin on the arduino
int solenoid_pin = 9;

void setup() {
  // Attach the solenoid to the solenoid valve control object
  my_solenoid_valve.attach(solenoid_pin);

  // We assume the vacuum pump is turned ON
  // When the vacuum pump is ON, and the solenoid valve OFF:
  // --Suction is ON
  // When the vacuum pump is ON, and the solenoid valve ON:
  // --Suction is OFF
  // Start with solenoid valve OFF (0 is OFF, 180 is ON)
  my_solenoid_valve.write(0);
}

// The vacuum suction cup turns ON for five seconds and then 
// turns OFF for five seconds. 
void loop() {
  my_solenoid_valve.write(0); // Turn the solenoid valve OFF (Suction is ON)
  delay(5000); // Wait five seconds
  my_solenoid_valve.write(180); // Turn the solenoid valve OFF (Suction is OFF)
  delay(5000); // Wait five seconds
  
}

Test the Vacuum Pump With PWM Electronic Switch 

Let’s test our vacuum pump to see if it is working properly. You will need to wire everything up like you see in this diagram.

2020-07-31-161848

If you are using a DC variable power supply, like I am, set it for 0.5A for the current limit and 6V for the voltage

Now, write the following code and upload it to your Arduino. This code makes the vacuum suction cup turn ON for five seconds and then turn OFF for five seconds. 

/*
Program: Test Vacuum Pump With PWM Electronic Switch
File: test_vacuum_pump.ino
Description: This program tests a vacuum pump 
  with electronic switch to see if it is working
  properly.
Author: Addison Sears-Collins
Website: https://automaticaddison.com
Date: July 29, 2020
*/

#include <VarSpeedServo.h> 

// Create a vacuum pump control object
VarSpeedServo my_vacuum_pump;

// Attach vacuump pump to digital pin on the arduino
int vacuum_pump_pin = 10;

void setup() {
  // Attach the vacuum pump to the vacuum pump control object
  my_vacuum_pump.attach(vacuum_pump_pin);

  // Start with vacuum pump ON (0 is OFF, 180 is ON)
  my_vacuum_pump.write(180);
}

// The vacuum suction cup turns ON for five seconds and then 
// turns OFF for five seconds. 
void loop() {
  my_vacuum_pump.write(180); // Turn the vacuum pump ON (Suction is ON)
  delay(5000); // Wait five seconds
  my_vacuum_pump.write(0); // Turn the vacuum pump OFF (Suction is OFF)
  delay(5000); // Wait five seconds
  
}

Test the Vacuum Pump and Solenoid Valve With PWM Electronic Switches

Let’s test our vacuum pump and solenoid valve together to see if they work properly as a unit. You will need to wire everything up like you see in this diagram.

2020-07-31-163513
2020-07-31-163526
2020-07-31-163532

If you are using a DC variable power supply, like I am, set it for 0.5A for the current limit and 6V for the voltage

Now, write the following code and upload it to your Arduino. This code makes the vacuum suction cup turn ON for five seconds and then turn OFF for five seconds. 

/*
Program: Test Vacuum Pump and Solenoid Valve With PWM Electronic Switches
File: test_vacuum_pump_and_solenoid.ino
Description: This program tests the vacuum pump and solenoid valve 
  together to see if they work properly as a unit. 
Author: Addison Sears-Collins
Website: https://automaticaddison.com
Date: July 29, 2020
*/

#include <VarSpeedServo.h> 

// Create a solenoid valve control object
VarSpeedServo my_solenoid_valve;

// Create a vacuum pump control object
VarSpeedServo my_vacuum_pump;

// Attach solenoid to digital pin on the arduino
int solenoid_pin = 9;

// Attach vacuump pump to digital pin on the arduino
int vacuum_pump_pin = 10;

void setup() {
  // Attach the solenoid to the solenoid valve control object
  my_solenoid_valve.attach(solenoid_pin);

  // Attach the vacuum pump to the vacuum pump control object
  my_vacuum_pump.attach(vacuum_pump_pin);

  // We assume the vacuum pump is turned ON
  // When the vacuum pump is ON, and the solenoid valve OFF:
  // --Suction is ON
  // When the vacuum pump is OFF, and the solenoid valve ON:
  // --Suction is OFF
  // Start with vacuum pump ON (0 is OFF, 180 is ON)
  my_vacuum_pump.write(180);
  // Start with solenoid valve OFF (0 is OFF, 180 is ON)
  my_solenoid_valve.write(0);
}

// The vacuum suction cup turns ON for five seconds and then 
// turns OFF for five seconds. 
void loop() {

  // Suction is ON
  my_solenoid_valve.write(0); 
  my_vacuum_pump.write(180); 
  
  delay(5000); // Wait five seconds

  // Suction is OFF
  my_solenoid_valve.write(180);
  my_vacuum_pump.write(0); 
  
  delay(5000); // Wait five seconds  
}

Test Force Sensitive Resistor With Vacuum Pump and Solenoid Valve

Let’s add our force sensitive resistor to our setup.

You will need to wire everything up like you see in this diagram.

If you are using a DC variable power supply, like I am, set it for 0.5A for the current limit and 6V for the voltage

Now, write the following code and upload it to your Arduino. This code runs in two stages:

  • Stage 0 (Pick up object)
    • Starts with the suction turned OFF (i.e. vacuum is OFF and solenoid is ON)
    • Gives you time to move the robotic arm in position so that the vacuum suction cup is touching the object you want to pick up.
    • Checks to see if the vacuum suction cup is touching the object you want to pick up.
    • If the vacuum suction cup is touching the object you want to pick up, suction is turned ON (i.e. vacuum is switched ON and solenoid is switched OFF).
  • Stage 1 (Place object)
    • Gives you time to move the robotic arm in position to place the object in your desired location.
    • Suction is turned OFF, and the object is released.
/*
Program: Test Force Sensitive Resistor With Vacuum Pump and Solenoid Valve
File: test_vacuum_solenoid_force_sensor.ino
Description: This program tests the vacuum pump, solenoid valve, and 
  force sensitive resistor to see if they work properly as a unit. 

  Connect one end of FSR to power, the other end to Analog 5.
  Then connect one end of a 10K resistor from Analog 5 to ground.
Author: Addison Sears-Collins
Website: https://automaticaddison.com
Date: July 29, 2020
*/

#include <VarSpeedServo.h> 

// Create a solenoid valve control object
VarSpeedServo my_solenoid_valve;

// Create a vacuum pump control object
VarSpeedServo my_vacuum_pump;

// Attach solenoid to digital pin on the arduino
int solenoid_pin = 9;

// Attach vacuum pump to digital pin on the arduino
int vacuum_pump_pin = 10;

int fsrPin = A5;     // the FSR and 10K pulldown are connected to A5
int fsrReading = 0;     // the analog reading from the FSR resistor divider
int stage = 0; // Keep track of the stage we are in

void setup() {
  
  // Attach the solenoid to the solenoid valve control object
  my_solenoid_valve.attach(solenoid_pin);

  // Attach the vacuum pump to the vacuum pump control object
  my_vacuum_pump.attach(vacuum_pump_pin);

  // When the vacuum pump is ON, and the solenoid valve OFF:
  // --Suction is ON
  // When the vacuum pump is OFF, and the solenoid valve ON:
  // --Suction is OFF
  // Start with Suction OFF
  my_solenoid_valve.write(180); // 0 is OFF, 180 is ON
  my_vacuum_pump.write(0); // 0 is OFF, 180 is ON
}

void loop() {

  /* Stage 0 - Pick up an object */
  while(stage == 0) {

    // Check to see if contact has been made with an object
    fsrReading = analogRead(fsrPin);
    fsrReading += analogRead(fsrPin);
    fsrReading += analogRead(fsrPin);
    fsrReading = fsrReading / 3;
    if (fsrReading > 300) {
      // Suction is ON
      my_solenoid_valve.write(0); 
      my_vacuum_pump.write(180); 
      stage = 1;
    }
  }

  // Move the robotic arm into position to place the object.
  // Delay is in milliseconds. Change this value as you see fit.
  delay(3000); 

  /* Stage 1 - Place an object */
  // Suction is OFF
  my_solenoid_valve.write(180); 
  my_vacuum_pump.write(0); 
  stage = 0;
}

Putting It All Together for Pick and Place

Now, to finish off all this, let’s add the servo motors so that we can control the robotic arm with the potentiometers.

You will need to wire everything up like you see in this diagram.

2020-08-01-190609

If you are using a DC variable power supply, like I am, set it for 3.5A for the current limit and 6V for the voltage

Now, write the following code and upload it to your Arduino. Control the robotic arm using the three potentiometers. The code is similar to the code from the last section with just a few new lines of code.

/*
Program: Robot With Vacuum Pump and Automatic Suction Control
File: automatic_suction_control.ino
Description: This program uses a vacuum pump, solenoid valve, and 
  force sensitive resistor to create automatic suction control. 

  Connect one end of FSR to power, the other end to Analog 5.
  Then connect one end of a 10K resistor from Analog 5 to ground.
Author: Addison Sears-Collins
Website: https://automaticaddison.com
Date: July 30, 2020
*/

#include <VarSpeedServo.h> 

/********************** SERVOS ***********************/
// Define the number of servos
#define SERVOS 3

// Create the servo objects.
VarSpeedServo myservo[SERVOS]; 

// Speed of the servo motors
// Speed=1: Slowest
// Speed=255: Fastest.
const int desired_speed = 255;

// Attach servos to digital pins on the Arduino
int servo_pins[SERVOS] = {3,5,6};

// Analog pins used to connect the potentiometers
int potpins[SERVOS] = {A0,A1,A2}; 

// Variables to read the value from the analog pin
int potpin_val[SERVOS]; 

/****************** SOLENOID VALVE *******************/

// Create a solenoid valve control object
VarSpeedServo my_solenoid_valve;

// Attach solenoid to digital pin on the arduino
int solenoid_pin = 9;

/******************* VACUUM PUMP *********************/

// Create a vacuum pump control object
VarSpeedServo my_vacuum_pump;

// Attach vacuum pump to digital pin on the arduino
int vacuum_pump_pin = 10;

/*************** FORCE SENSITIVE RESISTOR *************/

int fsrPin = A5;     // the FSR and 10K pulldown are connected to A5
int fsrReading = 0;     // the analog reading from the FSR resistor divider
int stage = 0; // Keep track of the stage we are in

void setup() {

  // Set up servos
  for(int i = 0; i < SERVOS; i++) {
    
    // Attach the servos to the servo object 
    // attach(pin, min, max  ) - Attaches to a pin 
    // setting min and max values in microseconds
    // default min is 544, max is 2400 
    myservo[i].attach(servo_pins[i], 544, 2400);  
  }
  
  // Attach the solenoid to the solenoid valve control object
  my_solenoid_valve.attach(solenoid_pin);

  // Attach the vacuum pump to the vacuum pump control object
  my_vacuum_pump.attach(vacuum_pump_pin);

  // When the vacuum pump is ON, and the solenoid valve OFF:
  // --Suction is ON
  // When the vacuum pump is OFF, and the solenoid valve ON:
  // --Suction is OFF
  // Start with Suction OFF
  my_solenoid_valve.write(180); // 0 is OFF, 180 is ON
  my_vacuum_pump.write(0); // 0 is OFF, 180 is ON

}

void loop() {

  /* Stage 0 - Pick up an object */
  while(stage == 0) {

    // Move the robotic arm into position to pick up the object
    // Modify the number of time steps as you see fit.
    for(int j = 0; j < 50; j++) {

      // Update servo position
      for(int i = 0; i < SERVOS; i++) {
        potpin_val[i] = analogRead(potpins[i]);
        potpin_val[i] = map(potpin_val[i], 0, 1023, 0, 180);
        myservo[i].write(potpin_val[i], desired_speed, true);
      }
    }

    // Check to see if contact has been made with an object
    fsrReading = analogRead(fsrPin);
    fsrReading += analogRead(fsrPin);
    fsrReading += analogRead(fsrPin);
    fsrReading = fsrReading / 3;
    if (fsrReading > 300) {
      // Suction is ON
      my_solenoid_valve.write(0); 
      my_vacuum_pump.write(180); 
      stage = 1;
    }
  }

  // Move the robotic arm into position to place the object
  // Modify the number of time steps as you see fit.
  for(int j = 0; j < 3000; j++) {
    
    // Update servo position
    for(int i = 0; i < SERVOS; i++) {
      potpin_val[i] = analogRead(potpins[i]);
      potpin_val[i] = map(potpin_val[i], 0, 1023, 0, 180);
      myservo[i].write(potpin_val[i], desired_speed, true);
    }
  }

  /* Stage 1 - Place an object */
  // Suction is OFF
  my_solenoid_valve.write(180); 
  my_vacuum_pump.write(0); 
  stage = 0;
}

MATLAB Cookbook – Code Examples for the Most Common Tasks

In this post, I will write example code for the most common things you’ll do in MATLAB. MATLAB is a software package used for numerical computation and visualization.

My goal is to write bare-bones, skeleton recipes that can be easily modified and adapted to your own projects.

Prerequisites

  • You have MATLAB installed on your computer. I’m using MATLAB Release 2020a.

Select a Current Directory

Open MATLAB.

In the command window, select your desired Current Folder (i.e. working directory). The syntax is:

cd 'path_to_folder'

For example., in the Command Window, you would type the following command and press Enter in your keyboard:

cd 'C:\Program Files\My_Documents'

Create New Scripts

To create a new script (i.e. the most basic Matlab file with the ‘.m’ extension), run the following command in the Command window.

edit matlab_cookbook_1.m

If this is your first time creating a file in MATLAB, you might see a prompt that asks you “Do you want to create it?”

Highlight “Do not show this prompt again,” and click Yes.

Accept User Input

Write the following code inside matlab_cookbook_1.m.

% Get rid of blank lines in the output
format compact

% Accept a string as input
% Semicolon prevents every variable and output from appearing
% in the command window
name = input("What's your first name : ", "s");

% Check if the user entered something as input
if ~isempty(name)
    fprintf("Hi %s\n", name) 
end

Save the code.

Click Run to run the code.

Type in your name into the Command Window.

Press Enter.

Here is the output:

2-user-input-output

To stop a script from running at any time, you can type CTRL+C.

Now, let’s create a new file named matlab_cookbook_2.m.

edit matlab_cookbook_2.m

Add the following code:

% Get rid of blank lines in the output
format compact

% Accept vector input
vector_input = input("Enter a vector : ");

% Display the vector to the Command Window
disp(vector_input)

Click Run.

Enter your vector. For example, you can enter:

[1 2 3]

Here is the output:

3-enter-your-vector

Declare and Initialize Variables and Data Types

Let’s work with variables and data types (i.e. classes).

Create a new script.

edit matlab_cookbook_3.m

Type the following code.

format compact

% Initialize a character variable
char_1 = 'A'

% Determine the class of a character
class(char_1)

% Initialize a string variable
str_1 = "This is a string"

% Determine the class
class(str_1)

% Evaluate a boolean expression
5 > 2

% Initialize a boolean varable to true
bool_1 = true

% Initialize a boolean variable to false
bool_2 = false

% Check out the maximum and minimum values that can be 
% stored in a data type
intmin('int8')
intmax('int8')

% See the largest double value that can be stored
realmax

% See the largest integer that can be stored
realmax('single')

Run it.

Here is the output:

4-matlab-cookbook3-output1
4-matlab-cookbook3-output2

How do you create an expression that spans more than one line?

Open a new script.

edit matlab_cookbook_4.m
format compact

% An expression that spans more than one line
var_1 = 5 + 5 + 1 ...
      + 1

Save and then run the code. 

5-matlab-cookbook4-output

Casting Variables to Different Data Types

Let’s explore how to cast variables to different data types.

Create a new script.

edit matlab_cookbook_5.m

Type the following code.

format compact

% Create a double (double is the default)
var_1 = 9

% Output the data type
class(var_1)

% Caste the double to an int8 data type
var_2 = int8(var_1)

% Check that the variable was properly converted
class(var_2)

% Convert a character to a double
var_3 = double('A')

% Convert a double to a character 
var_4 = char(64)

Run it.

Here is the output:

6-matlab-cookbook5-output

Formatting Data into a String

Let’s explore how to format data into a string.

Create a new script.

edit matlab_cookbook_6.m

Type the following code.

format compact

% Format output into a string.
% Sum should be a signed integer - %d
sprintf('9 + 2 = %d\n', 9 + 2)

% Format output into a string.
% Sum should be a float with two decimal places
sprintf('9 + 2 = %.2f\n', 9 + 2)

Run it.

Here is the output:

7-matlab-cookbook6-output

Basic Mathematical Operations

Let’s explore how to do basic mathematical operations in MATLAB.

Create a new script.

edit matlab_cookbook_7.m

Type the following code.

% Supress the display of blank lines
format compact 

% Display formatted text
% Addition
fprintf('9 + 2 = %d\n', 9 + 2)

% Subtraction
fprintf('9 - 2 = %d\n', 9 - 2)

% Multiplication
fprintf('9 * 2 = %d\n', 9 * 2)

% Display float with two decimal places
fprintf('9 * 2 = %0.2f\n', 9 / 2)

% Exponentiation
fprintf('5^2 = %d\n', 5^2)

% Modulus
fprintf('5%%2 = %d\n', mod(5,2))

% Generate a random number between 50 and 100
randi([50,100]) 

Run it.

Here is the output:

8-matlab-cookbook7-output

Basic Mathematical Functions

Let’s take a look at some basic mathematical functions in MATLAB.

Create a new script.

edit matlab_cookbook_8.m

Type the following code.

format compact

% This code has some basics mathematical functions
% in MATLAB

% Absolute Value
fprintf('abs(-7) = %d\n', abs(-7))

% Floor
fprintf('floor(3.23) = %d\n', floor(3.23))

% Ceiling
fprintf('ceil(3.23) = %d\n', ceil(3.23))

% Rounding
fprintf('round(3.23) = %d\n', round(3.23))

% Exponential (e^x)
fprintf('exp(1) = %f\n', exp(1)) 

% Logarithms
fprintf('log(100) = %f\n', log(100))
fprintf('log10(100) = %f\n', log10(100))
fprintf('log2(100) = %f\n', log2(100))

% Square root
fprintf('sqrt(144) = %f\n', sqrt(144))

% Convert from degrees to radians
fprintf('90 Deg to Radians = %f\n', deg2rad(90))

% Convert from radians to degrees
fprintf('pi/2 Radians to Degrees = %f\n', rad2deg(pi/2))

%%%% Trigonometric functions%%%
% Sine of argument in radians
fprintf('Sine of pi/2 = %f\n', sin(pi/2))

% Cosine of argument in radians
fprintf('Cosine of pi/2 = %f\n', cos(pi/2))

% Tangent of argument in radians
fprintf('Tangent of -pi/4 = %f\n', tan(-pi/4))

Run it.

Here is the output:

9-matlab-cookbook8-output

To see a big list of the built-in mathematical functions, you can type the following command:

help elfun

Relational and Logical Operators

Create a new script.

edit matlab_cookbook_9.m

Type the following code.

format compact

%{ 
Relational Operators: 
  -- Greater than >
  -- Less than < 
  -- Greater than or equal to >=
  -- Less than or equal to <=
  -- Equal to ==
  -- Not equal to ~=

Logical Operators: 
  -- OR || 
  -- AND &&
  -- NOT ~
%} 

% Example
age = 19

if age < 18
    disp("You are not in college yet")
elseif age >= 18 && age <= 22
    disp("You are a college student")
else
    disp("You have graduated from college")
end   

Run it.

Here is the output:

10-matlab-cookbook9-output

Now, let’s work with switch statements.

edit matlab_cookbook_10.m

Here is the output:

format compact

size = 12

switch size
    case 2
        disp("Too small")
    case num2cell(3:10) % If number is between 3 and 10, inclusive
        disp("Just right")
    case {11, 12, 13, 14} % If number is any of these numbers
        disp("A bit large")
    otherwise
        disp("Too big")
end  

Vectors

edit matlab_cookbook_11.m

Here is the output:

format compact

% Create a vector
vector_1 = [6 9 1 3 8]

% Calculate the length of the vector
vector_1_length = length(vector_1)

% Sort a vector in ascending order
vector_1 = sort(vector_1)

% Sort a vector in descending order
vector_1 = sort(vector_1, 'descend')

% Create a vector that has the numbers 3 through 9
vector_2 = 3:9

% Create a vector of numbers from 10 through 15 in steps of 0.5
vector_3 = 10:0.5:15

% Concatenate vectors
vector_4 = [vector_2 vector_3] 

% Get the first item in the vector above. Indices start at 1.
vector_4(1)
edit matlab_cookbook_12.m
format compact

% Create a vector
vector_1 = [6 9 1 3 8]

% Get the last value in a vector
last_val_in_vector = vector_1(end)

% Change the first value in a vector
vector_1(1) = 7

% Append values to end of vector
vector_1(6) = 99

% Get the first 3 values of a vector
vector_1(1:3)

% Get the first and second value of a vector
vector_1([1 2])

% Create a column vector
col_vector_1 = [6;9;1;3;8]

% Multiply a column vector and a row vector
vector_mult = col_vector_1 * vector_1

% Take the dot product of two vectors
% 2 * 5 + 3 * 9 + 4 * 7 = 65
vector_2 = [2 3 4]
vector_3 = [5 9 7]
dot_product_val = dot(vector_2, vector_3)

Here is the output:

13-matlab-cookbook12-output

Matrix Basics

edit matlab_cookbook_13.m
format compact

% Initialize a matrix
matrix_1 = [4 6 2; 6 3 2]

% Get the number of values in a row
num_in_a_row = length(matrix_1)

% Get the total number of values in a matrix
num_of_vals = numel(matrix_1)

% Size of matrix (num rows   num cols)
matrix_size = size(matrix_1)

[num_of_rows, num_of_cols] = size(matrix_1)

% Generate a random matrix with values between 20 and 30
% Matrix has two rows.
matrix_2 = randi([20,30],2)

% Modify a value inside a matrix (row 1, column 2)
% Remember matrices start at 1
matrix_2(1, 2) = 33

% Modify all row values in the first row
matrix_2(1,:) = 26

% Modify all column values in the first column
matrix_2(:,1) = 95

% Get the first value in the last row
first_val_last_row = matrix_2(end, 1)

% Get the second value in the last column
second_val_last_col = matrix_2(2, end)

% Delete the second column
matrix_2(:,2) = [];

Loops

For loops

edit matlab_cookbook_14.m
format compact

% Loop from 1 through 5
for i = 1:5
    disp(i) % Display
end

% Add a space
disp(' ')

% Decrement from 5 to 0 in steps of 1
for i = 5:-1:0
    disp(i)
end

% Add a space
disp(' ')

% Loop from 1 through 3
for i = [1 2 3]
    disp(i)
end

% Add a space
disp(' ')

% Create a matrix
matrix_1 = [1 4 5; 6 2 7];

% Nested for loop to run through all values in a matrix
for row = 1:2
    for col = 1:3
        disp(matrix_1(row, col))
    end
end

% Go through an entire vector
vector_1 = [2 8 3 5]
for i = 1:length(vector_1)
    disp(vector_1(i))
end

Output:

14-matlab-cookbook14-output

While loops

edit matlab_cookbook_15.m
format compact

% Create a while loop
i = 1
while i < 25
    % If the number is divisible by 5
    if(mod(i,5)) == 0
        disp(i)
        i = i + 1;
        continue
    end
    % Else
    i = i + 1;
    if i >= 14
        % Prematurely leave the while loop
        break
    end
end

Output:

15-matlabcookbook15-output

Matrix Operations

edit matlab_cookbook_16.m

Here is the first part of the output.

format compact

% Initialize a 3x3 matrix
matrix_1 = [4 6 2; 3 6 14; 5 2 9]

matrix_2 = [2:4; 7:9]
matrix_3 = [5:7; 9:11]
matrix_4 = [1:2; 3:4; 2:3]

% Add two matrices together
matrix_2 + matrix_3

% Multiply corresponding elements of two matrices together
matrix_2 .* matrix_3

% Multiply two matrices together
matrix_2 * matrix_4

% Perform the square root on every value in a matrix
sqrt(matrix_1)

% Double everything in a matrix
matrix_2 = matrix_2 * 2

% Sum everything in each column
sum(matrix_2)

% Convert a matrix to a boolean array
% Any value greater than 5 is 1
greater_than_five = matrix_1 > 5

Cell Arrays

edit matlab_cookbook_17.m
format compact

% Create a cell array
cell_array_1 = {'Automatic Addison', 25, [6 32 54]}

% Preallocate a cell array to which we will later assign data
cell_array_2 = cell(3)

% Get the first value in the cell array
cell_array_1{1}

% Add more information 
cell_array_1{4} = 'John Doe'

% Get the length of the cell array
length(cell_array_1)

% Display the values in a cell array
for i = 1:length(cell_array_1)
    disp(cell_array_1{i})
end

Here is the output:

17-matlabcookbook17-output

Strings

edit matlab_cookbook_18.m

format compact

% Initialize a string
my_string_1 = 'Automatic Addison'

% Get the length of the string
length(my_string_1)

% Get the second value in the string
my_string_1(2)

% Get the first three letters of the string
my_string_1(1:3)

% Concatenate
longer_string = strcat(my_string_1, ' We''re longer now')

% Replace a value in a string
strrep(longer_string, 'now', 'immediately')

% Split a string based on space delimiter
string_array = strsplit(longer_string, ' ')

% Convert an integer to a string
num_string = int2str(33)

% Convert a float to a string
float_string = num2str(2.4928)

Here is the output:

18-matlabcookbook18-outputJPG

Structures

Here is how to create your own custom data type using structures. Structures consist of key-value pairs (like a dictionary).

edit matlab_cookbook_19.m
format compact

automatic_addison = struct('name', 'Automatic Addison', ...
    'age', 35, 'item_purchased', [65 23])

% Get his age
disp(automatic_addison.age)

% Add a field
automatic_addison.favorite_food = 'Oatmeal'

% Remove a field
automatic_addison = rmfield(automatic_addison, 'favorite_food')

% Store a structure in a vector
clients(1) = automatic_addison

Here is the output:

19-matlabcookbook19-output

Tables

edit matlab_cookbook_20.m
format compact

name = {'Sam'; 'Bill'; 'John'};
age = [32; 52; 19];
salary = [45000; 90000; 15000]
id = {'1', '2', '3'}

% The name of each row will be the id
employees = table(name, age, salary, ...
    'RowName', id)

% Get the average salary
avg_salary = mean(employees.salary)

% 'help table' command helps you find what you can do with tables

% Add a new field
employees.vacation_days = [10; 20; 15]

Here is the output:

20-matlabcookbook20-output

File Input/Output

edit matlab_cookbook_21.m
format compact

% Generate a random 8x8 matrix
random_matrix = randi([1,5],8)

% Save the matrix as a text file
save sample_data_1.txt random_matrix -ascii

% Load the text file
load sample_data_1.txt

disp sample_data_1 

type sample_data_1.txt

Here is the output:

21-matlabcookbook21-output

Functions

edit matlab_cookbook_22.m
format compact

% Input vector
values = [9.7, 63.5, 25.2, 72.9, 1.1];

% Calculate the average and store it
mean = average(values)

% Define a function named average.m that 
% accepts an input vector and returns the average
function ave = average(x)
    % Take the sum of all elements in x and divide 
    % by the number of elements
    ave = sum(x(:))/numel(x); 
end

Here is the output:

22-matlabcookbook22-output

Creating a Plot

edit basic_plot.m
% Graph a parabola
x = [-100:5:100];
y = x.^2;
plot(x, y)

Here is the output:

23-basic-plotJPG