You are on page 1of 21

Report on Control and Computing

Lab experiments
Akshay Khadse
Roll No. 153079011,
IIT Bombay

Shantanu Wasu
Roll No, 153070020,
IIT Bombay

November 9, 2015

Experiment I

Control of a Line Following


Robot
1

Objective

This Experiment aims at designing and implementing PID Controller for a


Spark V robot. Spark V robot kit has six IR sensors, of which three serve as
output. The two motors are responsible for the movement of robot, speeds
of which are the outputs.

Methodology
1. Sample code was analysed for basic robot movements.
2. Code was written for reading ADC values.
3. Line following action was then implemented using available functions
without PID
4. PID algoritm was implemented infollowing steps

3
3.1

Problems faced:
Sensors and Errors

Spark V kit provides six onboard IR sensors. Three are facing sideways
and three to the bottom. For line following robot use of three sensors facing
bottom is sufficient.
A problem we faced was how to get error values. There are two approaches
for getting error. First, take the error to be differerence between left and right
sensors. Although this approch looks good, it fails to take into account small
discontinuties in the path. Also, the left and right sensors may not read same
value of intensity even after great calibration effort.
So, we assigned a common threshold to all sensors to differentiate between
black and white. Further, we gave weights to each sensor. The sum of the
weighted values gives the error.

3.2

Veloctiy and Direction

The output of PID algorithm is an integral value in this case. The next
problem we faced was how to rotate the motors using this single value. The
sign of the error value gives us direction to move and the magnitude of PID
value gives us the velocity with which the motors should rotate.
We required a function to control both the dirction and speed of motors
as such function was not present in any sample code. So we analysed the
available functions and hardware manual. The instructions responsible for
this were:
OCR1AL = left motor; Speed of left motor
OCR1BL = right motor; Speed of right motor
PORTB = 0x0A; PB0, PB1, PB2, PB3 determine direction of both motors
These instructions were suitably used in program.

3.3

PID Tuning

PID tuning was done by trial and error. First Kd and Ki values were set
to zero and Kp was gradually increased and movement of robot was observed
on test track. Whenever Kp value is too low, the robot failed to follow the
path on account of large turning radius. If the Kp is too high the robot
follows the path but with rapid jerks. A sutible value of Kp was found.
Then Kp was kept fixed and Kd was increased until a suitable value of Kd
was found. Simlarly, Ki was found.

4
1
2
3
4
5
6
7
8
9
10
11
12

Code

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include lcd.c
#define Th 185
unsigned char ADC Conversion(unsigned char);
unsigned char ADC Value;
unsigned char l = 0;
unsigned char c = 0;
unsigned char r = 0;
float kp,kd,ki,e,pe,P,I,D,O;
unsigned char PortBRestore = 0;

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

//Function to configure LCD port


void lcd port config (void)
{DDRC = DDRC | 0xF7; //all the LCD pins direction set as output
PORTC = PORTC & 0x80; // all the LCD pins are set to logic 0 except PORTC 7
}
//ADC pin configuration
void adc pin config (void)
{DDRA = 0x00; //set PORTF direction as input
PORTA = 0x00; //set PORTF pins floating
}
//Function to configure ports to enable robots motion
void motion pin config (void)
{DDRB = DDRB | 0x0F; //set direction of the PORTB3 to PORTB0 pins as output
PORTB = PORTB & 0xF0; //set initial value of the PORTB3 to PORTB0 pins to logic 0
DDRD = DDRD | 0x30; //Setting PD5 and PD4 pins as output for PWM generation
PORTD = PORTD | 0x30; //PD5 and PD4 pins are for velocity control using PWM
}//Function to Initialize PORTS
void port init()
{lcd port config();
adc pin config();
motion pin config();
}
void timer1 init(void)
{TCCR1B = 0x00; //stop
TCNT1H = 0xFF; //setup
TCNT1L = 0x01;
OCR1AH = 0x00;
OCR1AL = 0xFF;
OCR1BH = 0x00;
OCR1BL = 0xFF;
ICR1H = 0x00;
ICR1L = 0xFF;
TCCR1A = 0xA1;
TCCR1B = 0x0D; //start Timer
}
//Function to Initialize ADC
void adc init()
{ADCSRA = 0x00;
ADMUX = 0x20; //Vref=5V external ADLAR=1 MUX4:0 = 0000
ACSR = 0x80;
ADCSRA = 0x86; //ADEN=1 ADIE=1 ADPS2:0 = 1 1 0
}
//This Function accepts the Channel Number and returns the corresponding Analog Value

56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

unsigned char ADC Conversion(unsigned char Ch)


{unsigned char a;
Ch = Ch & 0x07;
ADMUX= 0x20| Ch;
ADCSRA = ADCSRA | 0x40; //Set start conversion bit
while((ADCSRA&0x10)==0); //Wait for ADC conversion to complete
a=ADCH;
ADCSRA = ADCSRA|0x10; //clear ADIF (ADC Interrupt Flag) by writing 1 to it
return a;
}
void init devices (void)
{cli(); //Clears the global interrupts
port init();
timer1 init();
adc init();
sei(); //Enables the global interrupts
}
//Main Function
int main(void)
{init devices();
lcd set 4bit();
lcd init();
kp=10;
kd=0.1;
ki=1/1000;
while(1)
{l=ADC Conversion(3);
c=ADC Conversion(4);
r=ADC Conversion(5);
lcd print(1, 1, l, 3);
lcd print(1, 5, c, 3);
lcd print(1, 9, r, 3);
if(c>Th)
{e=0;}
else if(l>Th)
{e=1;}
else if(r>Th)
{e=1;}
P=kpe;
I=I+e;
I=Iki;
D=(epe)kd;
O=P+I+D;

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135

pe=e;
if(O>510)
{O=510;}
if(O<510)
{O=510;}
if (e==0)
{PORTB = 0x06;
OCR1AH = 0x00;
OCR1AL = 255;
}
if (e>0.0)
{if (O>255)
{PORTB = 0x05;
OCR1AH = 0x00;
OCR1AL = O255;
}
else
{PORTB = 0x0A;
OCR1AH = 0x00;
OCR1AL = 255O;
}
}
else if (O<0.0)
{if (O<255)
{PORTB = 0x0A;
OCR1BH = 0x00;
OCR1BL = (O+255);
}
else
{PORTB = 0x05;
OCR1BH = 0x00;
OCR1BL = 255+O;
}
}
delay ms(10);
}
}

Results

The Spark V line follower robot completed the track in 26.80 seconds. Kp,
Ki and Kd values used were 10, 0.1 and 1/1000 respectively.
7

Experiment II

Gripper Arm
6

Objective

The objective of this experiment was to set up Arduinoet up Arduino to be


able to move robotic arm in any direction allowed. Calibrate using initial
condition so that some information about position of arm is knownbased
on angle rotated w.r.t. original position as reference. Basic code for servo
motors was provided.

Methodology
1. Matlab code was analysed for green objects detection and improvements were made.
2. Arduino Code was written for Gripper arm movements.
3. Algorithm was implemented for calculation of angles.
4. Serial communication was set up between the Matlab and Arduino.
5. Entire implementation was tested and necessary alterations were done
to respective codes.

8
8.1

Problems Faced:
Detection of Object

The MATLAB program was not able to detect the object even under very
small ambient light variations. This limited the operation of Gripper Arm
to a very specific illuminance variation.
To resolve this problem the threshold for colour was altered until it detected
green objects. Then, to reduce the noise due to surrounding objects and
improve 8-Point connectivity, area threshold was tweaked to get required
performance.

8.2

Calculation of Angles

After getting the image and determining centeroid, calculation of angles


was a problem because three motors were used and thier range was not known
in accordance to the area in which the object was going to be in.
We moved the Motor 2 (closer to base) in steps of 10o until the claw touched
the table surface. Next, the Motor 3 was turned by 10o and procedure for
9

Motor 2 so that now we had the range of angles for two motors responsible
for radial movement of claw.
The next part was to calculate exact angle values based on postion of
object with respect to both extremities. For this we found out the width of
the annular region by placing object on both extremities of annular region
and obseving the distance from the red tape mark. Both the required angles
were calculated by taking ratio of the objects radial distance from inner
circle with respect to width of annular region.

8.3

Communication

After initial implementation, it was observed that Gripper Arm was only
able to track objects in limited angular region. Its satisfactory operation was
confined to middle areas only.
The root cause of this problem was the attempt to communicate angle
values as Charachter Data. The issue resolved after angles were passed as
integers to Arduino. The int16 data type was used.

Code

Only specific parts of codes are included as major part of code was as in
sample.

9.1
6
7
8
9
10
11
12
13

MATLAB Code for Object Detection

%
% Set up Video input device
%
vid = videoinput(winvideo,1,YUY2 640x480);
triggerconfig(vid,manual);
set(vid,FramesPerTrigger,1 );
set(vid,TriggerRepeat, Inf);
set(vid,ReturnedColorSpace,rgb);

10

9.2

arduino=serial(COM10,BaudRate,9600);
fopen(arduino);
n=1
while n<3
fwrite(arduino,a1,int16);
pause(1);
n=n+1;
end

125
126
127
128
129
130
131
132

9.3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

MATLAB Code for Serial Communication

Arduino Code

#include <Servo.h>
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
unsigned long s;
float r;
int flag=0;
int a1,b1,a2,b2,a3,b3,a4,b4,b=0;
void setup()
{servo1.attach(2);
servo2.attach(3);
servo3.attach(4);
servo4.attach(5);
servo1.write(0);
servo2.write(180);
servo3.write(70);
servo4.write(0);
b2=180;
b3=55;
b4=120;
Serial.begin(9600);
}
void loop()
{while(flag==0)
{if(Serial.available()>0)
{b1=Serial.read();}
}
for (a4=0;a4<b4;a4++)

11

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

{servo4.write(a4);
delay(25);
}
for (a2=180;a2>90;a2)
{servo2.write(a2);
delay(25);
}
for (a3=70;a3>0;a3)
{servo3.write(a3);
delay(25);
}
for (a1=0;a1<b1;a1++)
{servo1.write(a1);
delay(25);
}
for (a3=0;a3<b3;a3++)
{servo3.write(a3);
delay(25);
}
for (a2=90;a2<b2;a2++)
{servo2.write(a2);
delay(25);
}
for (a4=120;a4>0;a4)
{servo4.write(a4);
delay(25);
}
for (a2=180;a2>90;a2)
{servo2.write(a2);
delay(25);
}
for (a3=55;a3>0;a3)
{servo3.write(a3);
delay(25);
}
for (a1=b1;a1>0;a1)
{servo1.write(a1);
delay(25);
}
for (a3=0;a3<70;a3++)
{servo3.write(a3);
delay(25);
}

12

73
74
75
76
77
78
79
80
81
82

for (a2=90;a2<180;a2++)
{servo2.write(a2);
delay(25);
}
for (a4=0;a4<120;a4++)
{servo4.write(a4);
delay(25);
}
while(1){};
}

10

Results

The MATLAB and Arduino Code was written to achive serial communication between them. Enviornmental fluctuations were also taken into account
and detection, tracking and movement was also achieved by this code. The
above implementation was successfully tested.

13

Experiment III

DC MOTOR POSITION
CONTROL VIA ARDUINO
MEGA
OBJECTIVE
a)Design and implement an embedded (PID) feedback controller using Arduino Mega
b)Design and implement a motor driver circuit and interface the Arduino
Mega with this circuit.

EXPERIMENTAL SETUP
Figure given below shows the experimental setup

14

METHODOLOGY
Block diagram given above shows the main components of the experiments.
Problem statement says that, for a step command of 180 degrees, the DC
motor should satisfy less than 1sec rise time, 1.5 sec settling time and 10%
overshoot.This can be achieved as follows
1)Take initial position of the DC motor as input from the potentiometer.
2)By adding or subtracting suitable offset, get the required set point.
3)Arduino then generates an output control signal by continuously tracing
the position of DC motor through potentiometer.
The output control signal can be calculated from the following formula
output = Kp error + Kd dif f erential error + Ki integral error
4)This output PWM signal is used by motor driver IC as control signal to
control the speed of DC motor.
PID controller constants used are

CODE FOR ARDUINO


1
2

int analogPin=2; // read POT value


int pos,e,sp,P,kp,D,kd,in,I,ki,O,pe,pos0,o1=0;

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

void setup()
{
Serial.begin(9600);
pos0=analogRead(analogPin);
if(pos0>570)
{
sp=pos0+570;
if(sp>=1024)
{
sp=sp1024;
}
}
else
{sp=pos0+570;
if(sp>=1024)
{
sp=sp1024;
}

15

22

23
24
25
26
27

kp=5;
kd=0.1;
ki=0.0001;
}

28
29
30
31
32
33
34

void loop()
{
pos=analogRead(analogPin);
Serial.println(pos);
Serial.print(\t);
Serial.println(millis());

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

e=sppos;
P=kpe;
D=kd(epe);
in=in+e;
I=kiin;
O=P+D+I;
o1=O;
pe=e;
if(O>255)
{
o1=255;
}
else if(O<255)
{
o1=255;
}

52
53
54
55
56
57
58
59
60
61
62
63
64

if(e<5&&e>5)
{
analogWrite(11,0);
analogWrite(12,0);
}
if(e>5)
{
analogWrite(12,o1);//clkwise
analogWrite(11,0);
}
else if(e<5)
{

16

analogWrite(12,0);
analogWrite(11,0o1);
}

65
66
67
68

RESULT
Observed plot is as shown below

All the specifications found are in desired range


Rise time=0.59 sec
Settling time=0.89 sec
Overshoot= 1.08%

17

Experiment IV

DC MOTOR POSITION
CONTROL VIA DAQ CARDS
AND MATLAB REAL TIME
WINDOWS TARGET
OBJECTIVE
a) Design and implement an embedded linear (PID) feedback controller using
the matlab real time windows target
b) Interface the embedded controller with a motor driver circuit using the
NI DAQ cards installed on the computer
c) Design and implement a motor driver circuit using L293D or equivalent
to drive the dc motor in both directions

EXPERIMENTAL SETUP
The experimental setup is shown in the following figure:

18

DESIGN AND IMPLEMENTATION


Follwing figure shows the simulation in matlab

Part 1
a)First task is to generate set point after receiving the analog input.For
this purpose analog input is compared with the voltage level 2.5 which decides the direction of rotation.
b)If the input voltage is greater than 2.5 then sp = ip 2.5 and if less
than 2.5 then sp = ip + 2.5
c) next task is to store the set point, which is done by the block enabled
subsystem.
d)After generating set point, error = sp ip is applied to the PID controller.
e) Output of PID controller, is control signal which is then passed through
PWM circuit and given to the motor for controlling the speed of motor.
19

Part 2

RESULTS
All the specifications found to be in the desired range
Rise time 0.4 sec
Settling time 0.54 sec
Overshoot 0.6%

20

Graph obtained is as shown above

21

You might also like