AI Agent Structure in Arduino IDE for a 4WD robot (Part 1-Sensors / Actuators)



Artificial Intelligence define an agent as anything that can be viewed as perceiving its environment through sensors and acting upon that environment through actuators. Human body has eyes and ears as sensors and hands and legs as actuators. The same way a robot may has a camera as a sensor and motors as actuators.

There are several types of agents that process inputs from sensors and outputs to actuators with different ways. The simplest kind of agent is the simple reflex agent. It selects actions for the actuators based only on the current perceive of the environment through the sensors as shown on Fig.01. The decision to take a certain action is based on pre-programed conditions. This is called condition-action rule and leads to a specific action only if a pre-defined condition-rule is fulfilled.

Current blog is the first part of a series that describe the development of a simple reflex agent for a wheeled robot which perceive its environment and activate its motors in ways to avoid obstacles. The physical form of the agent (hardware) will be an Arduino® board which is a very easy to use microcontroller suitable for robotic applications. The software used to program the agent is Arduino IDE® platform.

Fig. 01 - Schematic diagram of a simple reflex agent.

Before we start the study of a wheeled robot and the types of sensors and actuators that we are going to use, we will explain the general principles of sensors, actuators and how we process input and output signals from hardware (Arduino board pins) to software (Arduino IDE software).

First a review of types of signals we use to communicate either with sensors or actuators.

Sensors

Use of an electrical, electronic or mechanical device to monitor an environment condition which provide the agent with a perception of the world around him.
This device normally will provide a measured or transformed electrical signal of the following types:
  • Digital Input - A binary 0 or 1 signal in the form of a certain voltage rate (ex. 0 or 3V).
  • Analogue Input - A voltage rate within a certain range of values, pre-calibrated to define the measured element (ex. 0 to 5V).

Examples of sensors and input signal types

Sensor
Input Signal
Temperature
Analogue
Humidity
Analogue
Button
Digital
IR
Digital
Ultrasonic
Analogue
Joystick (x-y axes)
Analogue

-    Actuators

Transfer of signals to electrical, electronic or mechanical devices which allow the agent to efficiently perform an action within the world it operates.
These devices normally will receive an electrical signal of the same types:
  • Digital Output - A binary 0 or 1 signal in the form of a certain voltage rate (ex. 0 or 3V).
  •  Analogue Output - A voltage rate within a certain range of values, pre-calibrated to define the expected activation intense (ex. 0 to 5V).

Examples of actuators and output signal types

Actuator
Output Signal
LED light
Digital
Buzzer
Digital
Electrical DC Motor
Digital or Analogue
Servo Motor
Analogue
Step Motor
Analogue

Agent Hardware

The physical form of an agent with connection outlets for sensors and actuators wiring in case of Arduino® projects is any microcontroller board like UNO, Nano, Mega etc. This hardware provides Digital I/O and Analogue Input connection pins for sensor inputs and actuator outputs. Also has an embedded microprocessor which allows the loading and run of software coded to measure inputs, perform functions and provide signals to outputs.

IDE Code commands to configure I/O

The Arduino Software (IDE) is an open-source coding environment that allows you to write programs and upload them to any Arduino® board. (Refer to Arduino Software for more information and download options).
The following list of commands is an easy way to identify proper functions that can be used to define digital or analogue I/O and how they are structured into simple control scripts for any AI agent.
Below we will go through a step by step series of available build in code functions that we can always use to define how the hardware of the AI agent (Arduino® board) will identify and communicate with the sensors (digital or analogue inputs) and the actuators (digital or analogue outputs).


STEP 1 - Selection of board pins to be utilized and definition of operation mode

Digital IN/OUT (I/O)

We decide which pins of the Arduino board will act as digital inputs or outputs and define them. Arduino boards have several pins, noted with numbers, dedicated to digital I/O. For example an Arduino UNO® has 13 no digital I/O.

So we assign the pins that we are going to connect (wiring) according to the following:

  • We list the sensors that the AI agent will receive information from (environment perception), in digital mode and define them as digital INPUT. We assign one pin for each digital INPUT.
  • We list the actuators that the AI agent will command to action through digital output signals and define them as digital OUTPUT. Again we assign one pin for each digital OUTPUT.

The function to be used is:


pinMode(pin, mode)

pin: the Arduino pin number to be set the mode of.
mode: INPUT, OUTPUT is the description of the mode functionality.

Example code

void setup() {
    pinMode(11, OUTPUT); // sets the digital pin 13 as output
    pinMode(2, INPUT); // sets the digital pin 2 as input
    pinMode(4, INPUT); // sets the digital pin 4 as input
}


Analogue IN

We decide which pins of the Arduino board will act as analogue inputs and define them. Arduino boards have several pins, noted with numbers, dedicated to analogue IN. For example an Arduino UNO® has 5 no (A1-A5) analog IN.

So we assign the pins that we are going to connect (wiring) by listing the sensors that the AI agent will receive information from (environment perception), in analog mode and define them as analog INPUT. We assign one pin for each analog INPUT.

The way to make this assignment is by using a variable. The most common data type of variable is an integer or floating number and can be coded as below:

Example code

int var_analog_1 = A2; // a sensor input named as ‘var_analog_1’ is connected to // analog pin 2 as integer number


Analogue OUT

Last we have to manage the most complicated part which is the pins of the Arduino board that will drive the analog outputs and how do we define them.

Arduino boards do not provide dedicated analog out pins. The way produced signals are coded to analog forms and exported from the microcontroller are through the so called PWM® pins. In general Pulse Width Modulation (PWM) is a modulation technique used to encode a message into a pulsing signal. The pin will generate a steady rectangular wave of the specified duty cycle.

Fig. 02 - A typical PWM signal is defined from the duty cycle time which is percentage of the duration time.

Fig. 03 - Duty cycle can range from 0 to 100% and simulate a PWM wave. (photo source: Sparkun.com)

Note that 100% duty cycle would be the dame as setting the voltage to 5V (HIGH) and 0% duty cycle would be the same as grounding the signal. PWM signals are widely used for speed controls of DC motors, dimming LEDs and more.

So we have to identify those pins that are also PWM type. For example in Arduino UNO® pins no 3, 5, 6, 9, 10, 11 are also PWM classified. (For complete PWM pins reference of various Arduino boards check here).

The way to assign one of these pins as an analog OUT is by using the pinMode function mentioned before.


pinMode(pin, mode)

pin: the Arduino PWM pin number to be set the analog output mode of.
mode: OUTPUT is the description of the mode functionality.

Example code

void setup() {
    pinMode(3, OUTPUT); // sets pin 3 as analog output
    pinMode(9, OUTPUT); // sets pin 9 as analog output
}



STEP 2 – How to READ or WRITE from / to the Inputs and Outputs

Once the the AI agent has final definitions about which sensors and actuators signals are assigned to pins, the next step is to add code on how the will READ the inputs and how will WRITE signals to outputs.

READ a Digital IN

The function that we use to read the value from a specific digital pin (assigned before) will be as below:


digitalRead(pin)

pin: the Arduino pin number you want to read.

The function will return a LOW or HIGH depending on the digital input signal status (0 or 1).

Example code

void setup() {
    pinMode(2, INPUT); // sets pin 3 as digital input
}

Void loop() {
    digitalRead(2); // read the input pin
}


WRITE a Digital OUT

The function that we use to write a digital signal (0 or 1) to a specific digital pin (assigned before) will be as below:


digitalWrite(pin, value)

pin: the Arduino pin number.
value: LOW or HIGH (digital signal 0 or 1)

So by using this function, the AI agent provide a digital signal to one of the actuators with a corresponding value 5V if configured to HIGH and 0V (ground) for LOW.

Example code

void setup() {
    pinMode(11, OUTPUT); // sets pin 11 as digital output
}

Void loop() {
    digitalWrite(11, HIGH); // sets the digital pin 11 ON
    delay(1000); //waits for a second
    digitalWrite(11, LOW); // sets the digital pin 11 OFF
}


READ an Analog IN

The function that we use to read the value from a specific analog pin (assigned before) will be as below:


analogRead(pin)

pin: the name of the analog input pin to read from (A0 to A5 on most boards).

The function will return an ‘int’ data type (0-1023 for 10 bits or 0-4095 for 12 bits) as reade from the input signal.

Example code

int var_analog_1 = A2; // a sensor input named as ‘var_analog_1’ is connected to
                      // analog pin 2 as integer number

void setup() {
    Serial.begin(9600); // setup serial
}
Void loop() {
    analogRead(var_analog_1); // read the input pin
}


WRITE an Analog OUT

The function that we use to write an analog signal in the form of a PWM value wave to a specific pin (assigned before) will be as below:


analogWrite(pin, value)

pin: the Arduino pin number to write to. Allowed data types: int
value: the duty cycle: between 0 (always off) and 255 (always on)

So by using this function, the AI agent provide an PWM wave analog signal to one of the actuators with a corresponding duty cycle.

Example code

void setup() {
    pinMode(3, OUTPUT); // sets pin 3 as analog output
}
void loop() {
    analogWrite(3, 125); // sets the digital pin 3 to 50% duty cycle
}

The above code could be the command to a DC motor to start at 50% of the full speed.

At the next part we will explore how we build the structure of a simple reflex agent  with the proper condition-action rules required to function.


If you did like current part please share it through the social so that more people can have access to it. If you have any questions or would like to discuss any special case, please leave your comments below. I will be happy to answer!

 

2 comments:

  1. Nice blog.Thankyou for sharing information .
    In the applied Engineering Applications Of Artificial Intelligence B.tech program, graduate students develop a strong and deep learning with a thorough understanding of a variety of engineering applications.The institute emphasizes artificial intelligence engineering applications and impact through extensive interdisciplinary collaborations with best engineering placement college and several major centers.
    computer science engineering
    bachelor in commerce
    bachelor of computer science
    mba admission 2020
    BTech admission
    best business schools in india
    artificial intelligence online course
    Machine Learning & Artificial Intelligence
    b tech admission 2020
    best artificial intelligence course
    mba colleges delhi ncr
    artificial intelligence engineering

    ReplyDelete
  2. Nice articles and your information valuable and good articles thank for the sharing information robotic signals

    ReplyDelete

AI & Deep Learning in MEP - Buildings Services design

AI and deep learning - The new trend Progress of new digital technologies is something that all people experience e...

Powered by Blogger.