+ All Categories
Home > Documents > AT-BOT_E120703

AT-BOT_E120703

Date post: 14-Mar-2016
Category:
Upload: innovative-experiment-coltd
View: 217 times
Download: 3 times
Share this document with a friend
Description:
 
Popular Tags:
162
Robo-Creator : AT-BOT ctivity book 1 AT-BOT The 4-wheel autonomous robot programmed with C/C++ language Activity book
Transcript
Page 1: AT-BOT_E120703

Robo-Creator : AT-BOT ctivity book 1

AT-BOTThe 4-wheel autonomous robot

programmed with C/C++ language

Activity book

Page 2: AT-BOT_E120703

2Robo-Creator : AT-BOT activity book

AT-BOT activity book

All rights reserved under the Copyrights Act B.E. 2537

Do not copy any part of this book without our permission

Who should use this handbook?

1. Students and other people who are interested in applying to microcontrollers for

testing working process of automatic system or people who are fascinated in learning

and examining the microcontrollers in new approaches such as using an autonomous

robot as a form of an interactive media.

2. Academic institutes such as schools, colleges and universities, where provide

electronic subjects or electronic and computing engineering departments.

3. Lecturers and teachers who would like to study and prepare lesson plans for

microcontroller courses, including applied science which focuses on integrating

electronics, microcontrollers, computer programming and scientific examination in high

school education, vocational education, and bachelor’s degree.

Published and distributed by

Innovative Experiment Co.,Ltd.

108 Soi Sukumvit 101/2 Sukumvit Road, Bangna, Bangkok 10260 THAILAND

Details and illustrations used in this handbook are thoroughly and carefully to provide

the most accurate and comprehensive information under the conditions and time we

have before publishing. Innovative Experiment Co.,Ltd. shall not be responsible for

any damages whatsoever resulting from the information of this book as constant

revisions and updates will be published after this edition.

Page 3: AT-BOT_E120703

Robo-Creator : AT-BOT ctivity book 3

All Illustrations and Technical information found in this handbook are in our best

interest to simplify working processes and equipment principles so that it could be easily

understood by any user interested in robotics.

Therefore, The translation from THAI language to English and the usage of technical

terms may not follow the provision of the Royal Academy where they are many words

not described officially. Our team would be allowed to produce new technical terms.

The main reason of this explanation comes from data collection of equipment in

embedded computer system and robotic technology. Thai language is quite hard to

translate into English and thus our writer team gathered the required data and

investigated to make sure that the understanding in working processes has the limited

error.

When we compose the information in Thai, many technical terms have

complicated meanings. Definition of vocabulary occurred from the practice

coordinated with linguistic meaning. If there are any errors or mistakes shown, the team

of writer will accept and if we get explanation or suggestion from any expert, we will

clarify and improve those errors as soon as possible.

In order to develop the academic media especially with new technological

knowledge, it will be able to proceed continually under the participation of experts in

all fields.

Innovative Experiment Co.,Ltd.

Clarification from writer/complier team

Page 4: AT-BOT_E120703

4Robo-Creator : AT-BOT activity book

Chapter 1 AT-BOT : The 4-wheel autonomous robot………..................................…....……5

Chapter 2 AT-BOT Development tools..............................................................…………….17

Chapter 3 Wiring IDE introduction....................................................................…………….23

Chapter 4 ATX Library file..................................................................................……………..35

Chapter 5 The ATX controller board hardware experiment.....………………….....……49

Chapter 6 AT-BOT movement..............……..........................................………………………79

Chapter 7 Object avoiding by contact...……………………………...……………………..93

Chapter 8 AT-BOT Line tracking.........................…………………....................................….105

Chapter 9 AT-BOT with touchless object avoiding..........................................…………..147

Chapter 10 AT-BOT with Servo motor .....................................................…………………….155

Table of contents

Page 5: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 5

Chapter 1

AT-BOT : The 4-wheel autonomous robot

AT-BOT (All-Terrain mobile robot) is an autonomous robot performed by DC motors

with the set of 4 DC motor gearboxes come with spiked wheels in order to aid passing

through rough surface more efficiently. Also, it can move up on the slope in the level of 0

to 25 degree and if it is necessary to stop immediately to change the direction of motion,

it can do. The possibility is that AT-BOT is capable of supporting any mission in either a

smooth competition court with lines appeared for determining directions of motion or

rough court with barriers or participating with the World RoboCup Junior-Rescue.

Programming development of AT-BOT robots uses C/C++ language in the open

source software are named Wiring (www.wiring.org.co).

AT-BOT controller boad is called ATX board, which is able to drive 6 of DC motors

and 6 of Servo motors together. It has many ports for interfacing with both digital and

analog sensors, basic digital outpuit port and using the data communication via 2 lined

bus system called I2C bus and the standard UART serial bus.

The main driving system is consisted of 4 DC motor gearboxes with spiked wheels.

This results the AT-BOT with four-wheel automatic robot has a high driving power and high

speed. It identifies that this is a presentation of the different robot driving system from the

old system for studying.

AT-BOT is one of the robotic activities of Robo-Creator, the robotic kit for creative

education.

Page 6: AT-BOT_E120703

6Robo-Creator : AT-BOT activity book

1.1 AT-BOT part list1. ATX controller board

2. USB-miniB cable

3. Light reflector (ZX-03R) 4 sets

4. Touch sensor (Switch input boards) 2 sets

5. BO-1 DC motor gearbox 48:1 ratio with mounting and cable 4 sets

6. Standard servo motor x 1

7. Spike wheel sets (diameter 65mm., width 26mm. also include the hub for BO-1

gearbox) 4 sets

8. 5-AA battery holder with wire

9. Plasitc joiner set and Strip joiner set

10. Right angle metal shaft set

11. Nuts and screws set

12. CD-ROM (software, example code and documentations)

13. Activity manual and construction sheet

14. Line tracking demonstration paper

1.2 ATX controller board featuresIn the figure 1-1, it is shown components of the ATX controller board and there are

significant technical features as follows:

Main microcontroller is Atmel’s ATmega128. It features 8-ch 10-bit Analog to

Digial Converter, 128-KByte Flash memory , 4-KByte EEPROM, 4-KByte RAM. Operated with

16MHz clock from external crystal

Define all ports compatible with Wiring I/O standard hardware (www

.wiring.org.co). The number of port are 0 to 50.

13 programmable port in JST connector type. Includes Digital I/O port (2 :

port 14 and 15), A/D port (7 : port 40/ADC0 to port 46/ADC6), Two-wire interface or TWI (2

: port 0/SDA and port 1/SCL) and UART serial port communication (2 : port 2/RX1 and port

3/TX1). Both TWI and UART ports can config to digital input/output port for more I/O

applications.

Analog input (ADC0 to ADC6) supports 0 to +5Vdc input. The converter

resolution is 10-bit. The result value is 0 to 1,023 range.

Page 7: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 7

One variable resistor is connected with the Analog input ADC7 of main

microcontroller for simple ADC experiment.

2 of button switches with resistor pull-up are connected with port 49 and 50 of

the Wiring I/O controller board for simple digital input experiment.

One LED with a current limited resistor. It is connected with port 48

One piezo speaker at port 4

16x2 characters LCD for monitoring

On-board digital compass; HMC6352 from Honeywell. It is interfaced by I2C

bus or TWI

UART port for interfacing serial module device such as camera module (ZX-

CCD, CMUCAM1, CMUCAM2, mCAM), servo controller board (Parallax servo controller,

ZX-SERVO16U), Real-time clock (ZX-17 : serial real-time clock moduel)

6-ch DC motor driver with indicators. Support 4.5V to 9V DC motor. Maximum

current output 3A and 1.2A continuous.

6-RC servo motor output; support 4.8 to 7.2V standard and continuous servo

motor types.

Motor driver power indicator; nomally turned on. It will off when motor is short-circuit.

Figure 1-1 : ATX controller board layout

ON

MOTOR

BATTERY LEVEL1312111098

SERVO PORT

7.2

-9V B

ATT.

E2

RESET

+

-S

14 15PC6 PC7

C r e

c o n

a t o r e > > > >

t r o l l r b o a

>

e r dR

>

4850SW249SW1ADC7KNOB

TWI UART1

0 SCL 1 SDA 2 RX1 3 TX1

44 ADC446 ADC640 ADC041 ADC142 ADC2 START

45 ADC5

> >

USB DATA

43 ADC3

32

10

45

Page 8: AT-BOT_E120703

8Robo-Creator : AT-BOT activity book

5-status battery level monitor circuit :

- Last left yellow LED displays that the input supply voltage is 6.75V. When the

battery voltage is lower than 6.75V, this LED will start to blink.

- Next yellow LED displays the input supply voltage at 7.0V.

- First green LED displays the input supply voltage at 7.25V.

- Second green LED displays the input supply voltage 7.5V.

- Last right green LED displays that the input supply voltage is higher than at 7.75V.

Download and interface with computer via USB port by using USB to UART

converter chip; FT232RL. USB connection indicator is available.

Set the operation by Mode/Reset switch

Supply voltage range +7.2 to +9V 2400mA for 4 motor loads.

2 voltage regulator on-board; +5Vdc for microcontroller and all digital circuit,

+6Vdc for all motor driver circuits. By using voltage regulator; the motor driver circuit can

drives DC motor with constant speed when battery voltage is full and reduce to 60%. It

features constant speed without effect by battery voltage until it is lower 60% of full.

Page 9: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 9

1.3 Output device features

1.3.1 DC motor gearbox

DC motor gearbox of Robo-Creator kit is the BO1 model. The technical features are as

follows :

Requires the supply voltage +4.8 to +9Vdc 130mA @6V and no load

Gear ratio 48:1

Speed 250 round per minute @6V and no load

Weight 30 gram

Torque 0.5kg.-cm.

Figure1-2 : Details and gear diagram of BO-1 DC motor gearbox

Follower gear (1)36 teeth

Driver gear (1) 8 teeth

Driver gear (2) 9 teeth

Driver gear (3) 17 teeth

Driver gear (4) 17 teeth

Follower gear (2)36 teeth

Follower gear (3)28 teeth

Follower gear (4)28 teeth

Page 10: AT-BOT_E120703

10Robo-Creator : AT-BOT activity book

1.3.2 Standard servo motor

Servo motor has 3 wires; Signal (S), Supply voltage(+V) and Ground (G) The technical

features is as follows :

Requires the supply voltage +4.8 to +6Vdc

Weight 45 gram

Torque 3.40kg-cm. or 47 oz-inches.

Size (width x length x height) 40.5 x 20 x 38 mm. or 1.60 x 0.79 x 1.50 inches.

DATAR3220

R210k

R1510

LED1

S1

Switch GND

+V

Figure 1-4 : The touch sensor or Switch input board picture andschematic diagram

1.4 Sensor features

1.4.1 Touch sensor/Switch input board

The circuit is shown in the figure 1-4 including a switch with a LED and considered

output as logic ‘0’ when switch is pressed.

If the switch is pressed : the logic ‘0’ will be sent and the red LED is on.

If no press : LED is off and the logic is ‘1’.

(A)(B) (C)

Figure 1-3 : Details of standard RC servo motor of Robo-128

(A) outside body (B) Gear system (C) Electronic circuit board

Page 11: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 11

Figure 1-5 : Light reflector sensor layout andshcematic diagram

10k

LED1

220

+V

GND

OUTSFH310

1.4.2 The light reflection sensor : ZX-03R

The circuit and layout of this sensor are displayed in the figure 1-5. The circuit is used

to detect the reflected lights from surface or lines.

During apply the power supply, the red LED is bright at all the time. Meanwhile, the

light receptor is SFH310 photo-transistor and it will get red lights from the reflection of objects

or surface. The amount of the reflected light will be more or less depending whether there is

an obstacle or not and how well the object can reflect red light. The reflection of red lights

is based on the surface texture and colour of objects. It is said that the white smooth objects

are able to reflect light well so the infrared receptor gets a lot of reflected light and the

output voltage will be high. As black objects reflect less light, the light receptor sends low

voltage. With such features, the sensor circuit board is often used to trap the reflected light

on the surface and lines. It is necessary to install the circuit at the lower part of a robot.

With the use of red light as a main light to detect, this allows the sensor to measure

the colour difference on the surface printed by IR or UV resistant ink.

Due to ZX-03R light detection circuit gives the result as DC voltage, applying on AT-

BOT robot has to connect the signal to 7 channels of analog input on the ATX controller

board, from ADC0 to ADC6. After reading the analog signal value, use this value to check

the value on reflected light detection circuit and then apply to detect lines.

1.4.3 GP2D120 module detecting distance with Infrared

GP2D120 is a module that detects distance with Infrared and with the packet of 3

extension parts, including 3 pins; +Vcc, GND and Vout. Reading voltage value from GP2D120

must do after the preparation period of the module, which takes 32.7 to 52.9 millisecond (1

millisecond equal 0.001 second). So reading the value should wait for the suitable time as

mentioned above and shown the basic data in the figure 1-6.

Output voltage of GP2D120 at the distance of 30 centimetres, the power supply at

+5V as in the range of 0.25 to 0.55V has the mean as 0.4V and the range of output voltage

change at the distance of 4 centimetres is 2.25V±0.3V.

Light reflection sensor

Output port LED

Page 12: AT-BOT_E120703

12Robo-Creator : AT-BOT activity book

How to measure distance

The infrared light is sent out from a transmitter to the object

in front, by passing through a condense lens so that the

light intensity is focused on a certain point. Refraction

occurs once the light hits the surface of the object. Part of

the refracted light will be sent back to the receiver end, in

which another lens will combine these lights and determine

the point of impact. The light will then be passed on to an

array of photo-transistors. The position in which the light falls

can be used to calculate the distance (L) from the

transmitter to the obstacle using the following formula:

L F

A X

Therefore, L equals

F AL

X

Thus, the distance value from the phototransistors will be

sent to the Signal Evaluation Module before it is changed

to voltage, resulting in a change of voltage according to

the measured distance.

Figure 1-6 : shown shape, arrangement of pins, diagram of time ofoperation, and graph shown the operation of GP2D120 sensor

Infrared LED transmitter Infrared Receiver

GNDVout Vcc

GP2D120

4 8 12 16 20 24 28 3200

0.4

0.8

1.2

1.6

2.0

2.4

2.8

Output voltage (V)

Distance (cm)

1st measure 2nd measure

Not stable 1st output 2nd output n output

n measure

38.3ฑ9.6 ms

5 ms

Measurement

Vout

Supply

* Use Kodak R-27 gray-white paper.

The white side has a 90%

reflection rate, made from a

material that reflects light for range

measurement.

L

A

F

X

Object

GP2D120

Transmit LED Photo array

Page 13: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 13

1.5 Mechanical compoments

1.5.1 Plastic wheels for BO-1 DC motor gearbox and rubber tire

A circular wheel has the diameter of 65 millimeters. It is able to fit with the axis of BO-

1 gear motor directly without any additional modification and tighten with a 2 mm tapping

screw. A wheel tire is made up of rubber and its texture has treads in order to enhance in

sticking over the surface.

1.5.2 Spike wheel

The wheels are unique. The wheel surface is rubber and has triangular buttons with

rounded end and thorn-like protrusions to aid in adhesion and movement across smooth

surface, rugged surface, slope or zippers with the slope less than 20 degree.

To install this kind of plastic wheels with the axis of the gear motor of model BO-1 will

be required to use special joints to help and attach with a screw and 3 mm nuts.

1.5.3 Track and wheel set

It is a track set constructed for track with

wheels or crawler track with wheels in different

sizes and it is compatible with a plate set. The

set includes tracks with 30 joints (2 pieces), 10

joints (4 pieces), 8 joints (4 pieces), driving

wheels (2 pieces), large support wheels (2

pieces), medium support wheels (10 pieces),

poly caps (12 pieces), stainless axis with the

diameter of 3 mm, length of 110 mm (4 pieces)

and necessary screw set.

Page 14: AT-BOT_E120703

14Robo-Creator : AT-BOT activity book

1.5.4 Base plate and Plate set

It is a multi-purpose material set for making the base or the chassis frame. The set

provides plastic plates produced from 2 types of ABS materials. The first type is called AT-

plate in black and the other is a plate with the size of 160 x 60 mm. There are holes on the

plates and each hole is 3 mm in size and they are seperated by 5 mm in distance and the

total holes are 341. The set also contains brackets for long axis (2 pieces), brackets for

short axis (2 pieces) and a screw set necessary for fastening.

1.5.5 Grid plates

Plates are plastic manufactured from ABS materials in the size of 80 x 60 mm and 80 x

80 mm each. Each hole has the size of 3 mm and the distance between each hole is 5 mm.

1.5.6 Plastic joiners

They are stiffed plastic components and there are three designs, including, straight

joiners, right angle joiners, and obtuse angle joiners. Each piece can be inserted together

and they are used to construct a decorated structure (or decoration). A set contains all

three types, available 5 colors and 4 pieces for each, and 60 pieces in total.

Page 15: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 15

1.5.7 Plastic strip joiners

They are rigid and tough and there are holes in the size of 3 mm for each piece for

installing or connecting with other structure components by a screw. At the end of each

rod can insert with plastic joiners. There are three different sizes including 3, 5 and 12 holes

and each size has 4 pieces in a set.

1.5.8 Metal angle bar

Metal angle bars are metal components with the width of 7.5 mm and they are

cut into the right angle shape. There are holes with the size of 3 mm for using a screw to

install or construct with other structure parts. Three different sizes are provided in a set,

including 1x2 holes, 1x2 holes, and 2x5 holes and each size contains 4 pieces.

1.5.9 Screws and nuts

Screws and nuts are equipment for fastening many components together. They

compose of 2 mm-tapping screws (4 pieces), 3x8 mm-tapping screws (4 pieces), 3x10 mm-

tapping screws (30 pieces), 3x15 mm-tapping screws (4 pieces), 3x40 mm-tapping screws

(4 pieces), 3x8 mm-flat head screws (4 pieces), 3x5 mm-hand driven screws (4 pieces), 3x20

mm-hand driven screws (2 pieces), and 3 mm-nuts (30 pieces).

1.5.10 Metal stands-off

This kind of materials helps to hold different components together and support

boards, grid plates and base plates. They are made of rustproof nickle-plated metal and

have a characteristic of cylinder with the length of 25 mm. Inside of a cylinder, there is a

spiral hole along its body for a 3 mm-screw to fasten and 3 poles are provided in a set.

Page 16: AT-BOT_E120703

16Robo-Creator : AT-BOT activity book

1.5.11 Plastic stands-off

Plastic stands-off help fasten different parts and prop up boards, grid plates, and

base plates. They are made from cohesive ABS plastic but able to be cut. The shape of

the rod is cylindrical and there is a hole throughout the rod to insert 3 mm-screws. You can

get different sizes and number of support poles, including, 3 mm. (4 pieces), 10 mm. (4

pieces), 15 mm. (4 pieces), and 25 mm. (4 pieces) in the set.

1.5.12 5-AA Battery holder

It is used to carry 5 AA batteries. There is a wire which connects the anode and the

cathode to the main controller board immediately.

1.5.13 JST3AA-8 cable

This is an INEX standard cable, 3-wires combined with 2mm. The JST connector is at

each end. 8 inches (20cm.) in length. Used for connecting between controller board and

all sensor modules in the Robo-Creator kit. The wire assignment is shown in the diagram

below.

2mm. pitch

GNDS

+5V

2mm. pitchGND

S/Data

+5V

Page 17: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 17

Robo-Creator robot kit supports the operation controller program which can be

developed from Assembly, BASIC or C programming languages. For here, we will use

C/C++ programming language with the open-source software called Wiring, which is

the name of a development project of a small control system in order to apply the

software and hardware together.

We focus on concrete utilization as well as the connection of devices with

electronic system so that the system can work according to the statement written

correctly, collectively, Physical computing or the computer system which concentrates

on physical signal connection, connecting external sensor devices or controlling display

of LEDs, light, and sound, etc.

The official website of Wiring here is www.wiring.org.co. At this website, there is

data of both hardware and software allowed to download with free of charge. Also, it

is the open-source project to give an opportunity to developers who will be able to join

the project and expand the project freely.

The founder of Wiring is Hernando Barragan (Architecture and Design School,

Universidad de Los Andes, Comlumbia). Wiring started at the Interaction Design Institute

Ivrea in Italy and it is currently developed at the Universidad de Los Andes, Architecture

and Design School in Colombia.

2.1 Supported operating systemThe software for the program development is Wiring Development Environment

or sometimes called Wiring IDE and it can work with these operating systems or platforms.

Mac OS X 10.4 or higher (both models using Powerpc and Intel CPU)

Windows XP, Windows Vista and 7

Linux, both Fedora Core and Debian (including Ubuntu as well)

Other platforms which support the operation of Java 1.4 up

2.2 Wiring hardware The main hardware of Wiring is Wiring I/O board which is a small circuit board

with ATmega128 microcontroller. ATmega microcontroller is very important to control

all kinds of work in which the main controller or microcontroller will be programmed

Chapter 2

AT-BOT Development tools

Page 18: AT-BOT_E120703

18Robo-Creator : AT-BOT activity book

through USB port in Wiring IDE software. There is a connection point to recieve signals

from both analog and digital external sensors that allows the board to acquire information

from the surrounding environment (temperature, light, distance to an object etc.).

Moreover, there is another point to send signals out to control external devices, such

as LEDs, loudspeakers, servo motors, and liquid crystal display or LCD, etc.

Robo-Creator robot kit provides the hardware of controller board compatible

with Wiring hardware and the system of Wiring IDE so you will be able to develop the

program comfortably.

2.3 Introduction to Wiring 1.0 IDEWiring 1.0 is the software for developing C/C++ programming language in order

to create a program controlling ATmega1281 microcontroller on Wiring I/O hardware

and then use the program in AT-BOT robot in Robo-Creator kit as well.

In the kit, tools used in development of the program are contained completely in

the format of IDE (Integrated Development Environment), either the text editor for coding

or C complier. Downloading the program and the window of serial monitor for receiving

and sending serial information to AT-BOT robot. Wiring is designed to use easily and the

C/C++ language is for programming which can work on the operating system of

Windows XP up, Linux and MAC OSX and installation files for each platform are separated.

2.3.1 Software installation

(1) Insert the CD Rom, come with Robo-Creator robot kit. Click the file named

Wiring1000_RoboCreatorR1_Setup.exe (the number of the installation file may be

changeable) and then the window of welcoming to the Wiring 1.0 setup will appear.

Page 19: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 19

(2) Next, click to agree in each step of the setup as installation of other applications

of Windows until completion.

(3) Installing the Wiring 1.0 software by using the CD rom is bundled with Robo-

Creator robotic kit is the setup of both Wiring 1.0 software and USB driver to connect

with ATX controller board in the same time.

(4) Test to start the program by select START > All Programs > Wiring. Then for a few

moment, the window of Wiring IDE will be present.

After That you can use the Wiring IDE in the program development for AT-BOT

robots.

Page 20: AT-BOT_E120703

20Robo-Creator : AT-BOT activity book

2.3.2 Checking the USB Serial port for the AT-BOT

(1) Plug the USB cable connecting ATX control board with the USB port of the

computer. Turn on and wait for the blue LED at the position of USB on the circuit board

is on as the figure 2-1.

(2) Click the START button and go to the Control Panel.

(3) Then double-click the System

(4) Go to the tab of Hardware and click on the Device Manager button

Figure 2-1 : The steps of preparation for checking USB serial portpositions of AT-BOT robot

MOTOR

BATTERY LEVEL1312111098

SERVO PORT

7.2

-9V B

ATT.

E2

RESET

+

-S

14 15PC6 PC7

R o b

> o R

o - C r e a o r >

u n n i n . . o a

t

g r d.

>

4850SW249SW1ADC7KNOB

TWI UART1

0 SCL 1 SDA 2 RX1 3 TX1

44 ADC446 ADC640 ADC041 ADC142 ADC2 STAR

T

45 ADC5

> >

USB DATA

43 ADC3

32

10

45

Connect with USB port

Turn on power

2

4 Display RUN mode

1

Wait until the USB LED is on3

Page 21: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 21

(5) Check the hardware listing at Port. You should see USB Serial port . Check the

position. Normally it is COM3 or higher (for example; COM10). You must use this COM

port with the Wiring IDE software.

Page 22: AT-BOT_E120703

22Robo-Creator : AT-BOT activity book

2.3.3 AT-BOT with Wiring IDE interface

(1) Open Wiring IDE. Wait for a while. The main window of Wiring IDE will appear.

(2) Choose the suitable hardware by select menu Tools > Board > INEX > Robo-

Creator R1 > ATmega1281 @16MHz

(3) Select menu Tools > Serial Port to choose the USB serial port of AT-BOT. It is

COM4 (for example).

Must do this step for every new connection of the AT-BOT with Wiring IDE

Now the AT-BOT is ready for interfacing and code development with the Wiring

IDE.

Page 23: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 23

This chapter presents preliminary information of Wiring, which is the software tool

for developing the operation of AT-BOT robots or one of the activities to build robots in

Robo-Creator kit. For the details of program structure of C/C++ language Wiring supports,

you can read in the Wiring IDE help.

3.1 Components of Wiring IDEWiring IDE consist of two important parts which are text editor and C/C++

compiler. There are many tools and command buttons to help the program

development as appeared in the figure 3-1.

3.1.1 Menu bar

Including File, Edit, Sketch, Tools and Help menu, will affect work files doing at

the present only.

Figure 3-1 : Main window of Wiring IDE software used in theprogram development

Chapter 3

Wiring IDE introduction

Page 24: AT-BOT_E120703

24Robo-Creator : AT-BOT activity book

3.1.1.1 File

New (Ctrl+N) : Create new files. This is called sketch in Wiring and given name

following the recent date in the format sketch_YYMMDDa, such as sketch_080407a or

click the button on the tool bar.

Open (Ctrl+O) : Open the exist sketch file or click on the button .

Close (Ctrl+W) : Choose to close the sketch file.

Save (Ctrl+S) : Save the open sketch file in the old name and work similarly to

click the button on the tool bar.

Save as…(Ctrl+Shift+O) : Save the open sketch file in the new name and the old

file will not disappear.

Upload to Wiring hardware (Ctrl+U) : Exports the program to the Wiring I/O Board

(inthis document is the ATX controller board). After the files are exported, the directory

containing the exported files is opened. There is more information about uploading

below. It works in the same way as click the button on the tool bar.

Preference : Customize the operation of Wiring IDE

Quit (Ctrl+Q) : Quit the Wiring program and close all windows of Wiring program.

Page 25: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 25

3.1.1.2 Edit

The menu contains commands used to edit the sketch file that develops on the

Wiring IDE.

Undo (Ctrl+Z) : cancel the previous action of a command or the lastest typing.

You can cancel Undo command by click Edit > Redo.

Redo (Ctrl+Y) : To return to make a statement made before the Undo command

is available only when done Undo already.

Cut (Ctrl+X) : Delete and copy the selected text to store at the clipboard, which

functions as the temporary memory unit to preserve information.

Paste (Ctrl+V) : Place the data in the clipboard on the desired position or replace

the selected text.

Select All (Ctrl+A) : Select all letters or text in the open file in the text editor at that

time.

Find (Ctrl+F) : Search for any text in the open file in the text editor. In addition, it is

also able to find and replace another text.

Find Next (Ctrl+G) : Find text or words we use to search for the next one within

the open file in the text editor.

Page 26: AT-BOT_E120703

26Robo-Creator : AT-BOT activity book

3.1.1.3 Sketch

Sketch menu is a command menu relating to compile a sketch file.

Verify/Compile (Ctrl+R) : It is a command of program compilation and its function

is similar to pressing the button on the tool bar.

Import Library : Open the included library of Wiring.

Show Sketch Folder : Show the folder of the current sketch file.

Add File: Add the required program file to the sketch file.

3.1.1.4 Tools

Tools menu is a command menu relating to selection of tools helped to develop

a program. Important commands you should know are as follows.

Auto Format : Try to format program code in the completed form.

Serial Monitor : Open the serial data terminal.

Board : Choose the interfaced hardwarere with the Wiring 1.0.

Serial Port : Choose the interfaced port of the Wiring I/O hardware.

Page 27: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 27

3.1.1.5 Help

Getting Started : Open the window about the using Wiring of the Wiring website.

Examples : Open a sketch file of an example program.

Reference : Open Reference window of the Wiring website. It consists of

language, programming environment, libraries, and language comparison. You have

to connect with the internet if you would like to see the information.

Find in Reference (Ctrl+Shift+F) : Choose text in your program code. Here you will

drag black bar and click on it. The program will take the text you have chosen to find in

reference and if it cannot find anything, there will be a warning message in the window

of the program.

Wiring Hardware : Browse the information of Wiring I/O hardware via internet.

Troubleshooting : Open the window about solutions in performance of the Wiring

of the Wiring website.

Visit wiring.org.co (Ctrl+5) : Open the web browser to visit the homepage of

Wiring at http://wiring.org.co.

About Wiring : Show the copyright on the Wiring software

Page 28: AT-BOT_E120703

28Robo-Creator : AT-BOT activity book

3.1.2 Toolbar

There are six buttons of basic functions and initial operation as follows.

Run or Compile : This button is used to compile the program code.

New : This button is used to create a sketch file.

Open : Open the exist sketch file

Save : Save the open sketch file in the old name. If would like to

change filename, use Save As command instead.

Upload to Wiring hardware : Exports the program to the ATX

controller board). This procedure is called UPLOAD.

Serial monitor : opens the serial data communication between the

Wiring I/O hardware and the monitor of Wiring IDE through serial ports (or COM port) to

check the information sent back from Wiring I/O hardware (here it is ATX control board,

which is very useful for the detection of the program’s operation).

3.1.3 Serial monitor

Wiring IDE has a Serial monitor. It is a serial data communication tool. User can

transmit, receive and show the serial data via this monitor with USB serial port of computer.

In the developed sketch code, must put two imporatant commnands as follows :

1. Serial.begin() : Set the baud rate of serial data communication. Normally the

baud rate value is 9600 bit per second. Must add this command into Setup() of sketchbook.

2. Serial.println() : Assign the sending message to Serial monitor on the Wiring IDE.

Openning the Serial monitor is very easy. Click on the button at Toolbar. The

Serial monitor window is appeared following the figure below.

Page 29: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 29

3.2 How to develop the program(1) Check the installation of the hardware and software of Wiring. Include the

setting USB serial port that connected with Wiring I/O hardware; the ATX control board

of AT-BOT robot in Robo-Creator kit.

(2) Create a new sketch file by a click of the New button on the Tools bar or

choose from menu File > New

(3) Type the example code as as follows :

#include <atx.h> // Include main libraryint ledPin = 48; // LED connected to pin 48 (bootloader)void setup(){

lcd("Hello Robot!"); // Title message on LCDpinMode(ledPin, OUTPUT); // Sets the digital pin as output

}void loop(){

digitalWrite(ledPin, HIGH); // Sets the LED ondelay(1000); // Waits for a seconddigitalWrite(ledPin, LOW); // LED offdelay(1000);

}

This program is used to test a basic hardware of AT-BOT robot. At the LCD display

shows message Hell Robot ! and blink the LED at port 48 of the ATX controller board with

one second rate.

Page 30: AT-BOT_E120703

30Robo-Creator : AT-BOT activity book

(4) Go to the File menu to choose the Save as command to save the file in the

name of Test. Now, there is test.pde file happening in the folder called test.

(5) Verify the sketch by click onthe Run button of choose from menu Sketch >

Compile/Verify

If there is any error occurring from compilation, a warning message will

be appeared in the messsage area. Therefore, you will have to correct the program.

If all are correct, the message area will display Done compiling message.

Page 31: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 31

After the compilation is finished, in the folder of test there will be a new

folder are in named Build and within this folder it contains the source file of C++

programming language and a supplementary file.

(6) Connect the ATX board with USB port. Turn-on power. Wait until USB connection

is completely ( blue LED at USB is turned-on) .

(7) Press and hold the START button on the ATX controller board 3 seconds. The

LCD module shows Entry program mode message following the figure 3-2.

(8) Click on the Upload to Wiring Hardware. Code uploading is started. Wait

until uploading complete. The message Done uploading. RESET to start the new program.

is shown in the status bar of Wiring IDE.

Figure 3-2 : Illustration of AT-BOT robot controlled to enter theprogram mode

MOTOR

BATTERY LEVEL1312111098

SERVO PORT

7.2-9

V B

ATT

.

E2

RESET

+

-S

14 15PC6 PC7

R o b

> o B

o - C r e a o r >

o o t l o d r o a

t

a r de

>

4850SW249SW1ADC7KNOB

TWI UART1

0 SCL 1 SDA 2 RX1 3 TX1

44 ADC446 ADC640 ADC041 ADC142 ADC2 START

45 ADC5

> >

USB DATA

43 ADC3

32

10

45

Connect to USB port

Turn-on power

2

5 Display the programming mode

Press and hold the START button3 seconds.The robot enter to programmingmode

4

1

The blue LED of USB ready is on3

LED at port 48 is onfor programming mode

Page 32: AT-BOT_E120703

32Robo-Creator : AT-BOT activity book

If there is error occurring from uploading, a warning message will be

appeared in the messsage area as follows

This mostly occurs if the serial port is invalid or not selected the board to

work in the program mode. Correction can be read in the topic of Troubleshooting of

uploading error.

(9) Press on the START button on the ATX control board to start the operation of

the program.

At the display of ATX board; it shows message Hello Robot! and the LED at

the port 48 lights up.

Page 33: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 33

3.3 Troubleshooting of uploading error.

3.3.1 In case that you have clicked the Upload buttonalready but no any action

Cause :

Wiring software cannot link with ATX control board of AT-BOT robot because it is

not in the program mode.

Solution :

(1) Press the Ctrl, Alt and Delete key simultaneously and then the Window Security

window will pop up. Next, click on Task Manager to choose. In some computers, the

program may lead to the Window Task manager window immediately, in this case, you

can choose the Processes tab and search for the file named avrdude.exe. Finally, click

on that file and the End Process button respectively.

(2) Wiring IDE program will resume in a normal status and supply power to the

board again. Select the correct COM port and then set the ATX board to the

programming mode in order to upload the program again.

Page 34: AT-BOT_E120703

34Robo-Creator : AT-BOT activity book

3.3.2 In case if you click the Upload button, there is an errormessage that not find any hardware for uploading

Cause :

Wiring software cannot connect with ATX control board or AT-BOT robot because

selecting a COM port is not correct.

Solution :

You need to choose an another COM port used for the connection again and

correctly by doing at the Tools > Serial port.

Page 35: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 35

Developing C/C++ programming language with Wiring for AT-BOT robots is managed

under the support of atx.h library file in order to reduce steps and complexity in programming

to control parts of the hardware because it is required to give the priority for the program

development controlling AT-BOT robots to programming to support competitions.

The structure of atx.h library file shown as the diagram and details of all sub files are

consisted of as follows.

atx.h library file

Chapter 4

ATX Library file

Page 36: AT-BOT_E120703

36Robo-Creator : AT-BOT activity book

4.1 lcd.h library file of LCD module displayingThis library file supports the instruction set about the display of messages at the LCD

module. Before activating the function of this library, you should append the library file in

the first part of the program with the statement.

#include <lcd.h> or #include <atx.h>

The main function of this library is lcd., which contains the function for message

display at the LCD module in the type of 16 characters and 2 lines.

Syntax

void lcd(char *p,...)

Parameter

p - Type of display data. Support the special character for setting display method.

Command Operation

%c or %C Display 1 character

%d or %D Display the decimal value -32,768 to +32,767

%l or %L Display the decimal value -2,147,483,648 to +2,147,483,647

%f or %F Display floating point 3 digits

#c Clear message before next display

#n Display message on the second line (bottom line)

Example 4-1

lcd(“Hello LCD”); // Displays Hello LCD message at LCD module

Result :

Hel

Wir

l ooLCD boa

i ngI/ b obo

r

O tdR

rdr

Example 4-2

lcd(“abcdefghijklmnopqrstuvwxyz”);

// Display string. If over 16 charactes, the next character will// show on the second line automatically.

Result :

abc

qrs

d efghi klm

t uvwx z obo

j

y tdR

nop

Page 37: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 37

Example 4-3

lcd(“Value: %d unit “,518);// Display message with number date (518)

Result :

Val

qrs

u e:g51 kun

t uvwx z obo

8

y tdR

itp

Example 4-4

lcd(“Value: %d “,analog(4));

// Display analog value from analog port 4 (PA4)

Result :

Val

qrs

u e:gxx kun

t uvwx z obo

x

y tdR

itp

therefore xxx as reading value 0 to 1023

Example 4-5

char c_test=’j’;

lcd(“abcd%cxyz”,c_test);

// Display character j with any message

Result :

abc

qrs

d jxyzx kun

t uvwx z obo

x

y tdR

itp

Example 4-6

lcd(“Value: %f “,125.450);

// Display message with floating number 3 digit

Result :

Val

qrs

u e:g12 .45

t uvwx z obo

5

y tdR

0tp

Page 38: AT-BOT_E120703

38Robo-Creator : AT-BOT activity book

Example 4-7

lcd(“count1: %d #ncount2: %d”,12,48);// Display message with 2 control code and special key #n

// for moving all message after #n to line 2 or bottom line of// LCD screen

Result

cou

cou

nt1:11 .45

nt2:x 8 obo

2

4 t dR

0t p

4.2 sleep.h : The delay time libraryThis library file supports all instructions for time delaying. This library must be included

at the top of the program with the command #include as follows :

#include <sleep.h> or #include <atx.h>

The important function is sleep . It delay time in millisecond unit.

Syntax

void sleep(unsigned int ms)

Parameter

ms - Set the delay time in millsecond unit. Range is 0 to 65,535.

Example 4-8

sleep(20); // Dealy 20 miliisecond

sleep(1000); // Delay 1 second

Page 39: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 39

4.3 in_out.h : Digital input/output port libraryThis library file supports all instructions for readind and writing data to digital port of

controller board. This library must be included at the top of the program with the command

#include as follows :

#include <in_out.h> or #include <atx.h>

Important functions of this library file are consisted of :

4.3.1 inRead data from the specific digital port

Syntax

char in(x)

Parameter

x - Choose digital port number. it is 0 to 50

Return value

0 or 1

Example 4-9

char x; // Declare x variable for keeping reading input data

x = in(49); // Read port 49 and store data to x variable.

Example 4-10

char x; // Declare x variable for keeping reading input data

x = in(50); // Read port 50 and store data to x variable.

4.3.2 outWrite or send the data to the specific digital port

Syntax

out(char _bit,char _dat)

Parameter

_bit - Choose digital port number. it is 0 to 50

Example 4-11

out(43,1); // Write port 43 with logic “1”

out(45,0); // Write port 45 with logic “0”

4.3.3 sw1_pressThis function loops to check the SW1 pressing. It returns value after switch is released.

Syntax

void sw1_press()

Example 4-12

................

sw1_press(); // Wait until the SW1 is pressed and released

................

Page 40: AT-BOT_E120703

40Robo-Creator : AT-BOT activity book

4.3.4 sw2_press

This function loops to check the SW2 pressing. It returns value after switch is released.

Syntax

void sw2_press()

Example 4-13

..................

Sw2_press(); // Wait until the SW2 is pressed and released

.................

4.3.5 sw1

This function check the SW1 pressing in any time.

Syntax

char sw1()

Return value

“0” - SW1 is pressed

“1” - SW1 is not pressed

Example 4-14

char x; // Declare x variable for keeping the value

x = sw1(); // Get SW1 status and store to x variable

4.3.6 sw2

This function check the SW2 pressing in any time.Syntax

char sw2()

Return value

“0” - SW2 is pressed

“1” - SW2 is not pressed

Example 4-15

char x; // Declare x variable for keeping the value

x = sw2(); // Get SW2 status and store to x variable

Page 41: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 41

4.4 analog.h : Analog port libraryThis library file supports all instructions for reading the analog input port of the ATX

controller board. This library must be included at the top of the program with the command

#include as follows :

#include <analog.h> or #include <atx.h>

4.4.1 analog

This gets digital data from the analog to digital converter module of any analog

port; ADC0 to ADC7.

Syntax

unsigned int analog(unsigned char channel)

Parameter

channel - Analog input (ADC0 to ADC7)

Return value

Digital data from analog to digital converter module. The value is 0 to 1023 (in

decimal)

4.4.2 knob

This function gets data from ADC7 port. This port isconnected with variable resistor

on-board. It is called KNOB.

Syntax

unsigned int knob()

Return value

Digital data from analog to digital converter module. The value is 0 to 1023 (in

decimal)

Example 4-16

int val=0; // Declare variable to keep the converted data

val = analog(2); // Get data from analog input ch. 2 (ADC2)// and store data to val variable.

Example 4-17

int val=0; // Declare variable to keep the converted data

val = knob(); // Get data from analog input ch. 7// (ADC7 or KNOB) and store data to val// variable

Page 42: AT-BOT_E120703

42Robo-Creator : AT-BOT activity book

4.5 motor.h : DC motor driving libraryThis library file supports all instructions for driving and controlling 6 DC motor outputs

of the ATX controller board. This library must be included at the top of the program with

the command #include as follows :

#include <motor.h> or #include <atx.h>

4.5.1 motor

It is DC motor driving function.

Syntax

void motor(char _channel,int _power)

Parameter

_channel - DC motor output of ATX board; value is 0 to 5

_power - Power output value; it is -100 to 100

If set _power as positive value (1 to 100); motor is driven one direction.

If set _power as negative value (-1 to -100); motor is driven opposite direction.

If _power as 0; motor is stop. This value is not recommended. Use motor_stop

function to stop motor better.

Example 4-18

motor(1,60); // Drive motor ch.1with 60% of maximum power

motor(1,-60); // Drive motor ch.1with 60% of maximum power and turn back

direction.

Example 4-19

motor(2,100); // Drive motor ch.2 with maximum power

4.5.2 motor_stop

This function is driving off a motor or stop.

Syntax

void motor_stop(char _channel)

Parameter

_channel - DC motor output of ATX board; value is 0 to 5 and all (for driving off

all channels)

Example 4-20

motor_stop(1); // Stop motor ch.1

motor_stop(4); // Stop motor ch.4

Example 4-21

motor_stop(ALL); // All motor are stop

Page 43: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 43

4.6 servo.h : Servo motor libraryThis library file supports all functions for controlling 6 servo motor outputs of the ATX

controller board. This library must be included at the top of the program with the command

#include as follows :

#include <servo.h> or #inclue <atx.h>

There is one function. It is servo.

Syntax

void servo(unsigned char _ch, int _pos)

Parameter

_ch - Servo motor output (8 to 13)

_pos - Set the sevo motor shaft poistion (0 to 180 and -1)

If set to -1, disable selected servo motor output

4.7 sound.h : Sound generating libraryThis library file supports all functions for sound generating of the ATX controller

board and AT-BOT. This library must be included at the top of the program with the

command #include as follows :

#include <sound.h> or #inclue <atx.h>

4.7.1 beep

It is beep sound generating function. The beep frequency is 500Hz and 100

millisecond duration time.

Syntax

void beep()

4.7.2 sound

This is programmable sound generating function.

Syntax

void sound(int freq,int time)

Parameter

freq - Set frequency with value 0 to 32,767

time - Set duration time in millisecond unit from 0 to 32,767

Example 4-22

beep(); // Drives beep sound with 100 millisecond duration

sound(1200,500); // Drives sound with 1200Hz 500 millisecond

Page 44: AT-BOT_E120703

44Robo-Creator : AT-BOT activity book

4.8 serial.h : Serial data communication libraryThis library file supports all functions for sending and receiving the serial data via

UART port of the ATX controller board and AT-BOT. This library must be included at the top

of the program with the command #include as follows :

#include <serial.h> or #include <atx.h>

4.8.1 Hardware connection

UART0 port

UART0 port is connected via USB to Serial converter chip; FT232RL. For connecting

with computer, must connect via USB port on the ATX controller board. This connector is

same port for downloading.

UART1 port

Connect via RXD1 (port 2 ) and TXD1 (port 3)

4.8.2 uart

This is serial data sending function via UART0 port. The default baudrate is 115,200

bit per second.

Syntax

void uart(char *p,...)

Parameter

p - Type of data. Support the special character for setting display method.

Command Operation

%c or %C Display 1 character

%d or %D Display the decimal value -32,768 to +32,767

%l or %L Display the decimal value -2,147,483,648 to +2,147,483,647

%f or %F Display floating point 3 digits

\r Set the message left justify of the line

\n Display message on the new line

4.8.3 uart_set_baud

This is baud rate setting function for UART0.

Syntax

void uart_set_baud(unsigned int baud)

Parameter

baud - Baud rate of UART0 2400 to 115,200

Example 4-23

uart_set_baud(4800); // Set baud rate as 4,800 bit per second

Page 45: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 45

4.8.3 uart_available

This is receiveing data testing function of UART0.

Syntax

unsigned char uart_available(void)

Return value

- “0” : no data received

- more than 0 : received character

Example 4-24

char x =uart_available();

// Check the recieving data of UART0.

// If x value is more than 0; it means UART0 get any data.

// Read it by using uart_getkey function in the order next immediately.

4.8.4 uart_getkey

This is data reading function from receiver’s buffer of UART0

Syntax

char uart_getkey(void)

Return value

- “0” : no data received

- data : received character in ASCII code

Example 4-25

#include <robot.h> // Get functionvoid setup(){}void loop() // Main loop{

if(uart_available()) // Check incoming data{

if(uart_getkey()==’a’) // Is key ‘a’ pressed ?{

lcd(“Key a Active!”); // Display message when get ‘a’sleep(1000); // Delay 1 second

}else{

lcd(“#c”); // Clead display}

}}

Note : Default baud ratre of UART library is 115,200 bit per second. Data format

is 8-bit and no parity.

Page 46: AT-BOT_E120703

46Robo-Creator : AT-BOT activity book

4.8.5 uart1

This is serial data sending function via UART1 port. The default baud rate is 9,600 bit

per second.

Syntax

void uart1(char *p,...)

Parameter

p - Type of data. Support the special character for setting display method. See

details in uart0 function.

4.8.6 uart1_set_baud

This is baud rate setting function for UART1.

Syntax

void uart1_set_baud(unsigned int baud)

Parameter

baud - Baud rate of UART0 2400 to 115,200

Example 4-26

uart1_set_baud(19200); // Set baud rate as 19,200 bit per second

4.8.7 uart1_available

This is receiving data testing function of UART0.

Syntax

unsigned char uart1_available(void)

Return value

- “0” : no data received

- more than 0 : received character

Example 4-27

char x =uart1_available(); // Check the receiving data of UART1.

4.8.8 uart1_getkey

This is data reading function from receiver’s buffer of UART1.

Syntax

char uart1_getkey(void)

Return value

- “0” : no data received

- data : received character in ASCII code

Page 47: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 47

4.9 Digital compass libraryIt is compass.h file. This library file is not included in robot.h library file. Must include

the specific library file before using.

This library file supports all functions for interfacing the HMC6352 digital compass of

the ATX controller board. This library must be included at the top of the program with the

command #include as follows :

#include <compass.h>

4.9.1 compass_read

This reads the angle of the HMC6352 digital compass.

Syntax

int compass_read()

Return value

Angle value 0 to 359 defree

4.9.2 compass_set_heading

This is reference angle setting function. With this function, the current angle that

read from digital compass is set to 0 degree reference.

Syntax

void compass_set_heading()

4.9.3 compass_read_heading

This is reference angle reading function. Use this function after set the new reference

angle from compass_set_heading function.

Syntax

int compass_read_heading()

Return value

1 to 180 : positive angle (clock wise direction) of digital compass.

-1 to -180 : negative angle (Counter-Clockwise direction) of digital compass.

Page 48: AT-BOT_E120703

48Robo-Creator : AT-BOT activity book

Page 49: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 49

This chapter presents examples of the hardware experiment with the ATX controller

board of the Robo-Creator robotic kit. There are 7 experiments as follows.

Experiment 1 Shows message on the display of the ATX board

Experiment 2 Using SW1 and SW2 switch

Experiment 3 Reading analog from KNOB button of the ATX board

Experiment 4 Sound activity

Experiment 5 DC motors control

Experiment 6 Servo motor control

Experiment 7 Serial data communication with computers

Chapter 5

The ATX controller board

hardware experiment

Page 50: AT-BOT_E120703

50Robo-Creator : AT-BOT activity book

Programming and Hardware experiment steps(1) Open Wiring IDE and create a new sketch file.

(2) Type the code on the test editor of the sketch file

(3) Compile by click at the button or choose at the menu Compile > Verify.

(4) Connect the ATX controller board with a USB port. Turn on power and wait until

the connection between the computer and ATX board is completed. This can be noticed

from the blue LED at the position of USB power on.

(5) Set the ATX board to program mode by pressing START switch and hold it for 3

seconds.

At the ATX board display, it shows messages

Robo-Creator

> Bootloader

and the red LED at the port 48 is powered on.

(6) Upload the code by click at the button or click at the menu file > Upload to

Wiring hardware.

(7) Wait until the uploading is successful. Then press the START switch again. Finally,

the ATX controller circuit board will be running the latest uploaded program immediately.

Page 51: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 51

Experiment 1

Shows message on the display of the ATX board

Experiment 1.1 Simple message displaying on the ATX board

This experiment demonstrates the simle programming for showing the message at

the display of the ATX board.

Procedure

L1.1.1 Create the new sketch file. Type the Listing L1-1 and save as lcd_01.pde file

L1.1.2 Compile and upload the sketch to the ATX board.

L1.1.3 Run the program.

At the LCD display of the ATX controller board show message Hello Robot! as follows.

MOTOR

BATTERY LEVEL1312111098

SERVO PORT

7.2

-9V B

ATT.

E2

RESET

+

-S

14 15PC6 PC7

Hel

> o B

loCRob t!>

o o t l o d r o a

o

a r de

>

4850SW249SW1ADC7KNOB

TWI UART1

0 SCL 1 SDA 2 RX1 3 TX1

44 ADC446 ADC640 ADC041 ADC142 ADC2 START

45 ADC5

> >

USB DATA

43 ADC3

32

10

45

Listring L1-1 : lcd_01.pde, the sketch file for simple displaying

message on the ATX board.

#include <atx.h> // Include the main libraryvoid setup(){ lcd("Hello Robot!"); // Display mesage on the ATX display}void loop(){}

Code explanation

This code runs within the setup function. There is one command. Display the message;

Hello Robot! on the screen. After that the program will jump to run in the loop function. No

any command in this function. The operation is stop finally.

Program L1-1

Page 52: AT-BOT_E120703

52Robo-Creator : AT-BOT activity book

Experiment 1.2 Display message 2 lines of the ATX boarddisplay

This experiment demonstrates the displaying message 2 lines of the ATX board

display.

Procedure

L1.2.1 Create the new sketch file. Type the Listing L1-2 and save as lcd_02.pde file

L1.2.2 Compile and upload the sketch to the ATX board.

L1.2.3 Run the program.

At the LCD display of the ATX controller board show message as follows :

MOTOR

BATTERY LEVEL1312111098

SERVO PORT

7.2

-9V B

ATT.

E2

RESET

+

-S

14 15PC6 PC7

Lin

Lin

e1CRob t!>

e2t l o d r o a

o

a r de

>

4850SW249SW1ADC7KNOB

TWI UART1

0 SCL 1 SDA 2 RX1 3 TX1

44 ADC446 ADC640 ADC041 ADC142 ADC2 START

45 ADC5

> >

USB DATA

43 ADC3

32

10

45

#include <atx.h> // Include the amin libraryvoid setup(){}void loop(){

lcd(“Line1#nLine2”); // Display message 2 lines}

Code explanation

The program starts running in the setup function then it repeats working in the loop function

to display message on both lines of the LCD. The result comes from the operation of control

code #n. The overall results have been shown the display of 2-line text.

Listring L1-2 : lcd_02.pde, the sketch file for displaying message 2

lines on the ATX board display.

Page 53: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 53

Experiment 1.3 Shows message and number

This experiment demonstrates showing messages mixed with numbers at the ATX

board display. It shows the values of counting in every 1 second.

Procedure

L1.3.1 Create the new sketch file. Type the Listing L1-3 and save as lcd_03.pde file

L1.3.2 Compile and upload the sketch to the ATX board.

L1.3.3 Run the program.

At the LCD display of the ATX controller board show message as follows :

Count: xxx

therefore xxx is counting value that increse every second.

MOTOR

BATTERY LEVEL1312111098

SERVO PORT

7.2

-9V B

ATT.

E2

RESET

+

-S

14 15PC6 PC7

Cou

Lin

nt:R20 t!>

e2t l o d r o a

0

a r de

>

4850SW249SW1ADC7KNOB

TWI UART1

0 SCL 1 SDA 2 RX1 3 TX1

44 ADC446 ADC640 ADC041 ADC142 ADC2 STA

RT

45 ADC5

> >

USB DATA

43 ADC3

32

10

45

#include <atx.h> // Include the main libraryint i = 0; // Declare the counting variablevoid setup(){}void loop(){ lcd("Count: %d ",i); // Display the counter on the ATX board display sleep(1000); // Delay 1 second i++; // Increase counter}

Code explanation

The program starts running in the setup function and then it repeats working in the loop

function. It shows the counting value that increased every 1 second. The variable i stores the

count values.

Listring L1-3 : lcd_03.pde, the sketch file for displaying

message and number on the ATX board.

Page 54: AT-BOT_E120703

54Robo-Creator : AT-BOT activity book

Experiment 2

Using SW1 and SW2 switch

Experiment 2.1 Using SW1 to start the counter

This expeirment demonstrates how to use the SW1 on the ATX controller board to

start the counter. The counting values also should be display on the ATX board display.

Procedure

L2.1.1 Create the new sketch file. Type the Listing L2-1 and save as switch_01.pde file

L2.12 Compile and upload the sketch to the ATX board.

#include <atx.h> // Include the main libraryint i=0; // Declare the counter variablevoid setup(){ lcd("SW1 Press!"); // Display the title message sw1_press(); // Wait the SW1 pressing lcd("#c"); // Clear display before show the next message}void loop(){ lcd("Count: %d ",i); // Display the counting value sleep(1000); // Delay 1 second i++; // Increase counter}

Code description

The program starts running in the setup function to wait for the pressing of SW1 and

there is an alert by the text of SW1 Press! at the display after the switch is pressed. The

program will repeat working in the loop function to display the count value that is increased

in every 1 second. The i variable is used to stores the count values.

Listing L2-1 : switch_01.pde, the sketch file for checking the SW1

of the ATX board pressing to start the counter

Page 55: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 55

L2.1.3 Run the program.

At the LCD display of the ATX controller board show message as follows :

SW1 Press!

L2.1.4 Press the SW1 on the ATX board and release.

Counting is start. The counter value is displayed on the ATX board display as follows.

Count: xxx

therefore xxx is counting value that increase every second.

MOTOR

BATTERY LEVEL1312111098

SERVO PORT

7.2

-9V B

ATT.

E2

RESET

+

-S

14 15PC6 PC7

Cou

Lin

nt:R20 t!>

e2t l o d r o a

0

a r de

>

4850SW249SW1ADC7KNOB

TWI UART1

0 SCL 1 SDA 2 RX1 3 TX1

44 ADC446 ADC640 ADC041 ADC142 ADC2 STA

RT

45 ADC5

> >

USB DATA

43 ADC3

32

10

45

Press the switchSW1 to start

Experiment 2.2 Checking the SW1 and SW2 pressing anytime

This experiment demonstrates the checking of SW1 and SW2 of the ATX board

pressing. It used to increase and decrease the variable value. Also display the value on

the LCD module of ATX board.

Procedure

L2.2.1 Create the new sketch file. Type the Listing L2-2 and save as switch_02.pde file

L2.2.2 Compile and upload the sketch to the ATX board.

L2.2.3 Run the program.

At the LCD display of the ATX controller board show message as follows :

Count: xxx

therefore xxx is the counting value. Start from 10.

Page 56: AT-BOT_E120703

56Robo-Creator : AT-BOT activity book

#include <atx.h> // Include the main library

int i=10; // Declare the counter variable

// and set to start from 10

void setup()

{}

void loop()

{

lcd("Count: %d ",i); // Display the counting value

if(sw1()==0) // SW1 is pressed ?

{

i++; // If SW1 is pressed, increase counter.

sleep(200); // Delay for switch debouncing

}

if(sw2()==0) // SW2 is pressed ?

{

i-- ; // If SW2 is pressed, decrease counter

sleep(200); // Delay for switch debouncing

}

}

Code description

The program begins operating in the setup function. Then it repeats in the loop function.

It loop to check up pressing the switch of SW1 and SW2 all the time and display the count

values of the variable I at the LCD module as well.

Conditions of verification in the loop as following these:

1. If the SW1 is pressed ( sw1 ( ) function returns the value as 0)

The program responds by increase value of the variable i

2. If the SW2 is pressed (sw2 ( ) function returns the value as 0 )

The program responds by decrease value of the variable i.

Listing L2-2 : switch_02.pde, the sketch file for checking both SW1

and SW2 of the ATX board pressing anytime

Page 57: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 57

L2.2.4 Press the SW1 switch on the ATX board. Observe the operation of the ATX board

display.

Each time you press the SW1, the count value is added up one value

L2.2.5 Press the SW2 switch on the ATX board. Observe the operation of the ATX board

display.

Each time you press the SW2, the count value is deduct one value.

MOTOR

BATTERY LEVEL1312111098

SERVO PORT

7.2

-9V B

ATT.

E2

RESET

+

-S

14 15PC6 PC7

Cou

Lin

nt:R20 t!>

e2t l o d r o a

0

a r de

>

4850SW249SW1ADC7KNOB

TWI UART1

0 SCL 1 SDA 2 RX1 3 TX1

44 ADC446 ADC640 ADC041 ADC142 ADC2 START

45 ADC5

> >

USB DATA

43 ADC3

32

10

45

Press SW1 to increase value

Press SW2 todecrease value

Page 58: AT-BOT_E120703

58Robo-Creator : AT-BOT activity book

Experiment 3

Reading analog from KNOB button of the ATX board

Experiment 3.1 Knob value reading

This experiment demonstrates how to read the analog value from KNOB button of

the ATX controller board. It is basic example of analog sensor reading. The result is 0 to

1023.

Procedure

L3.1.1 Create the new sketch file. Type the Listing L3-1 and save as knob_01.pde file

L3.1.2 Compile and upload the sketch to the ATX board.

L3.1.3 Run the program.

At the LCD display of the ATX controller board show message as follows :

KNOB: xxx

therefore xxx as KNOB position value from 0 to 1023

- Adjust to last left position. Value is 0.

- Adjust to last right position. Value is 1023.

- Adjust to center position. Value is 512.

MOTOR

BATTERY LEVEL1312111098

SERVO PORT

7.2

-9V B

ATT.

E2

RESET

+

-S

14 15PC6 PC7

KNO

Lin

B::101 t!>

e2t l o d r o a

0

a r de

>

4850SW249SW1ADC7KNOB

TWI UART1

0 SCL 1 SDA 2 RX1 3 TX1

44 ADC446 ADC640 ADC041 ADC142 ADC2 STA

RT

45 ADC5

> >

USB DATA

43 ADC3

32

10

45

Adjust the KNOBshaft to reading

Page 59: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 59

#include <atx.h> // Include the main libraryvoid setup(){}void loop(){ lcd("KNOB: %d ",knob()); // Display the KNOB value sleep(100); // Delay 0.1 second for displaying}

Code explanation

The program begins operating in the setup function. Then it repeats in the loop function

to display the readable values from KNOB at the LCD module.

Knob value is read by using knob function of the atx.h library. It is analog to digital

converter function.

Listing L3-1 : knob_01.pde, the sketch file for reading the KNOB

button of the ATX board value.

Experiment 3.2 Use KNOB button to Mode selector

This experiment demonstrates about programming to use KNOB as the directional

determinant of counting value. If KNOB is adjusted to the left to compare with the middle

position, this will be the selection to count down values but if it is opposite, it will be the

selection to count values up. Additionally, the display of the count value is shown at the

LCD module.

Procedure

L3.2.1 Create the new sketch file. Type the Listing L3-2 and save as knob_02.pde file

L3.2.2 Compile and upload the sketch to the ATX board.

L3.2.3 Run the program.

At the LCD display of the ATX controller board show message as follows :

Count: xxx

Count Up

in the Count up mode or

Count: xxx

Count Down

in the Count down mode

therefore xxx is thge counting value. Start from 100.

Page 60: AT-BOT_E120703

60Robo-Creator : AT-BOT activity book

#include <atx.h> // Include the main libraryint i=100; // Declare the counter variable. Start from 100int k; // Declare the KNOB value variablevoid setup(){}

void loop(){ lcd("Count: %d ",i); // Display counter k = knob(); // Read the KNOB value to store to the k variable if(k>512) // Check the KNOB’s value is through

// the middle to right or not ? { i++; // Increase the count value lcd("#nCount Up "); // Display the Count up mode message

// on the lower line of LCD } else { i--; // If the KNOB’s value is through

// the middle to left, count down lcd("#nCount Down "); // Display the Count down mode message

// on the lower line of LCD } sleep(1000); // Delay 1 second}

Code explanation

The program begins operating in the setup function. Then it repeats in the loop function

to verification of adjustment position of KNOB continually. At the same time, the count value of

the variable i and the counting mode at the LCD module as well. There are conditions for the

verification as follows:

1. If the KNOB value is greater than 512

The program will respond to adding up the count value of the variable i to one value

and show the Count up mode message on the display.

2. If the KNOB value is less than 512 (working in the section of else).

The program will respond to deducting the count values of the variable i to one value

and show the Count down mode message on the display.

Listing L3-2 : knob_02.pde, the sketch file for using the KNOB button

of the ATX board to Mode selector

Page 61: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 61

The program wil count up and down depending on the value of KNOB, which is

result from the rotation of spindle of the variable resistor at the position of KNOB of ATX

board.

MOTOR

BATTERY LEVEL1312111098

SERVO PORT

7.2

-9V B

ATT.

E2

RESET

+

-S

14 15PC6 PC7

Cou

Cou

nt:120 t!>

nttUp d r o a

0

a r de

>

4850SW249SW1ADC7KNOB

TWI UART1

0 SCL 1 SDA 2 RX1 3 TX1

44 ADC446 ADC640 ADC041 ADC142 ADC2 STA

RT

45 ADC5

> >

USB DATA

43 ADC3

32

10

45

Adjust the KNOB shaft tochange the counting mode

Left direction to countdown mode

Right direction to countup mode

L3.2.4 Adjust the KNOB shaft to go through the middle to the left (or may adjust to the left)

This will be found that the counter is in Count down mode. The count value will be

deducted 1 value in each second.

L 3.2.5 Adjust the KNOB shaft to go through the middle to the right (or maybe adjust to the

far right)

The counter is in Count up mode. The count value will be added up 1 value in

each second.

Page 62: AT-BOT_E120703

62Robo-Creator : AT-BOT activity book

Experiment 4

Sound activity

Exeperiment 4.1 The signal selector

This experiment demonstrates about using the SW1 and SW2 on the ATX controller

board to generate the different sound frequency. If SW1 is pressed, ATX board drives

500Hz signal with 0.1 second duration. If the SW2 is pressed, it generate 2000Hz (2kHz)

signal with 0.5 second instead.

Procedure

L4.1.1 Create the new sketch file. Type the Listing L4-1 and save as sound_01.pde file

L4.1.2 Compile and upload the sketch to the ATX board.

L4.1.3 Run the program. Press the SW1 on the ATX controller board.

Everytime to press the switch SW1, you will hear the sound with the frequency of

500 Hz for 0.1 second from the piezo speaker on the ATX board.

L 4.1.4 Press the switch SW2

Everytime to press the SW2, you will hear the sound of frequency 2000Hz for 0.5

second.

MOTOR

BATTERY LEVEL1312111098

SERVO PORT

7.2

-9V B

ATT.

E2

RESET

+

-S

14 15PC6 PC7

Cou

Lin

nt:R20 t!>

e2t l o d r o a

0

a r de

>

4850SW249SW1ADC7KNOB

TWI UART1

0 SCL 1 SDA 2 RX1 3 TX1

44 ADC446 ADC640 ADC041 ADC142 ADC2 START

45 ADC5

> >

USB DATA

43 ADC3

32

10

45

Press SW1 to generate 500Hz signal

Press SW2 togenerate 2000Hzsignal

Page 63: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 63

#include <atx.h> // Include the main libraryvoid setup(){}void loop(){

if(sw1()==0) // The SW1 is pressed ?{

beep(); // If SW1 is pressed,// generate the 500Hz signal 0.1 second duration

sleep(100); // Delay for switch debouncing}if(sw2()==0) // The SW2 is pressed ?{

sound(2000,500); // If SW2 is pressed,// generate the 2000Hz signal 0.5 second duration

sleep(100); // Delay for switch debouncing}

}

Code explanation

The program operates in the loop function to check the pressing of the SW1 and SW2

switches and there are conditions as follows.

1. If the SW1 is pressed (sw1( ) function returns the value as 0)

The program will respond to generate the 500Hz signal for 0.1 second.

1. If the SW2 is pressed (sw2( ) function returns the value as 0)

The program will respond to generate the 2000Hz signal for 0.5 second.

Listing L4-1 : sound_01.pde, the sketch file for using the SW1 and

SW2 on the ATX board to set the condition for signal generating

Page 64: AT-BOT_E120703

64Robo-Creator : AT-BOT activity book

Experiment 4.2 Tuning frequency by KNOB

This experiment demonstrates about using KNOB button to adjust the sound

frequency and show the frequency value at the ATX board display

Procedure

L4.2.1 Create the new sketch file. Type the Listing L4-2 and save as sound_02.pde file

L4.2.2 Compile and upload the sketch to the ATX board.

L4.2.3 Run the program.

At the LCD display of the ATX controller board show message as follows :

Freq: xxx Hz

therefore xxx is the generated signal frequency

L4.2.4 Asjust the KNOB button slowly from left to right direction. See the display operation

and listen the sound signal from ATX board.

The frequency of the sound displaying at the LCD module will increase from 0 to

2046Hz (as 2 times of the value read from KNOB). The sound you have listen will be

dramatically sharpened following the adjustment of frequency increasing.

MOTOR

BATTERY LEVEL1312111098

SERVO PORT

7.2

-9V B

ATT.

E2

RESET

+

-S

14 15PC6 PC7

Fre

Lin

q::120 tHz

e2t l o d r o a

0

a r de

>

4850SW249SW1ADC7KNOB

TWI UART1

0 SCL 1 SDA 2 RX1 3 TX1

44 ADC446 ADC640 ADC041 ADC142 ADC2 START

45 ADC5

> >

USB DATA

43 ADC3

32

10

45

Adjust the KNOB button to change the sound frequency

Page 65: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 65

#include <atx.h> // Include the main library

int k; // Declare the KNOB value variable

int f; // Declare the frequency variable

void setup()

{

}

void loop()

{

k = knob(); // Read the KNOB value to store in the k variable

f = 2*k; // Increase the value 2 times

lcd("Freq: %d Hz ",f); // Display the sound frequency

sound(f,200); // Generate the sound signal// from the variable data for 0.2 second

sleep(1000); // Delay 1 second

}

Code explanation

The program starts working in the setup function and repeats to work in the loop function

to repeat reading from the KNOB button. Then, the value you have got multiply by 2 to use for

the frequency value desired so the value will be in the range of 0 to 2046Hz and the frequency

value will be shown at the LCD module.

For the sound generation at the Piezo speaker, the program will space in each second

briefly.

Listing L4-2 : sound_02.pde, the sketch file for using the KNOB button

on the ATX board to adjust the sound signal frequency

Page 66: AT-BOT_E120703

66Robo-Creator : AT-BOT activity book

Experiment 5

DC motors control

Experiment 5.1 Direction control for DC motor driver

This experiment demonstrates about direction control for DC motor driver output 0

and 1 of the ATX board. The motor driver will be drive DC motor forward and backward

every 3 seconds continually.

Hardware connection

Connect the DC motor #1 with Motor-0 output of the ATX board.

Connect the DC motor #2 with Motor-1 output of the ATX board.

ON

MOTOR

BATTERY LEVEL1312111098

SERVO PORT

7.2-9

V B

ATT.

E2

RESET

+

-S

14 15PC6 PC7

ATX

con

1.0r>> >>>

troll r boa

>

e rdR

>

4850SW249SW1ADC7KNOB

TWI UART1

0 SCL 1 SDA 2 RX1 3 TX1

44 ADC446 ADC640 ADC041 ADC142 ADC2 START

45 ADC5

>>

USB DATA

43 ADC3

32

10

45

DC motor #2

DC motor #1

Procedure

L5.1.1 Create the new sketch file. Type the Listing L5-1 and save as motor_01.pde file

L5.1.2 Compile and upload the sketch to the ATX board.

L5.1.3 Run the program.

DC motor at output 0 and 1 start and reverse direction every 3 seconds.

Page 67: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 67

#include <atx.h> // Include the main library

void setup()

{

}

void loop()

{

motor(0,70); // Drive motor-0 with 70% power

motor(1,70); // Drive motor-1 with 70% power

sleep(3000); // Delay 3 seconds before change the direction

motor(0,-70); // Drive motor-0 backward with 70% power

motor(1,-70); // Drive motor-1 backward with 70% power

sleep(3000); // Delay 3 seconds before change the direction

}

Code explanation

The program repeats to work in the loop function to drive the DC motor of the channel 0

and 1 simultaneously with 70% power equally both. And every 3 seconds reversing direction of

rotation will be operated continuously.

Listing L5-1 : motor_01.pde, the sketch file for direction control of

DC motor driver of the ATX board. DC motor will reverse direction

every 3 seconds continually

Experiment 5.2 Timing control motor

In this experiment demonstrates the programming for driving both DC motor at the

Motor-0 and 1 output of the ATX board with timing control. Both motors will operate 3

seconds and then stop 3 seconds alternately and continuously.

Hardware connection

Connect the DC motor #1 with Motor-0 output of the ATX board.

Connect the DC motor #2 with Motor-1 output of the ATX board.

Procedure

L5.2.1 Create the new sketch file. Type the Listing L5-2 and save as motor_02.pde file

L5.2.2 Compile and upload the sketch to the ATX board.

L5.2.3 Run the program.

DC motors at output 0 and 1 start and stop 3 seconds alternately and continually.

Page 68: AT-BOT_E120703

68Robo-Creator : AT-BOT activity book

#include <atx.h> // Include the main library

void setup()

{

}

void loop()

{

motor(0,90); // Drive motor-0 with 90% power

motor(1,90); // Drive motor-1 with 90% power

sleep(3000); // Delay 3 seconds

motor_stop(0); // Stop motor-0

motor_stop(1); // Stop motor-1

sleep(3000); // Delay 3 seconds

}

Code explanation

The program repeats to work in the loop function to drive the DC motor at channel 0 and

1 simultaneously with the 90% power driving equally both. After 3 seconds, all motors will stop

for 3 seconds also and start to rotate again. This will work together seamlessly.

Listing L5-2 : motor_02.pde, the sketch file for timimg control of DC

motor driver of the ATX board. DC motor will start and stop every 3

seconds continually

Page 69: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 69

Experiment 6

Servo motor control

Experiment 6.1 Control position of the servo motor

This experiment demonstrates about programming to control the servo motor shaft

position. The servo motor is driven to move the shaft to 60 degrees position and stop to

lock at this position for 5 minutes. Next, change to the position to 120 degrees and stop to

lock at this position for 5 minutes as well. Then, the shaft will be moved to the position of 60

degrees again so that the positions will be switched back and forth like this constantly.

Hardware connection

Connect a standard servo motot to servo motor output port 12 of the ATX board

ON

MOTOR

BATTERY LEVEL1312111098

SERVO PORT

7.2

-9V B

ATT.

E2

RESET

+

-S

14 15PC6 PC7

ATX

con

1.0r >> >>>

troll r boa

>

e rdR

>

4850SW249SW1ADC7KNOB

TWI UART1

0 SCL 1 SDA 2 RX1 3 TX1

44 ADC446 ADC640 ADC041 ADC142 ADC2 START

45 ADC5

>>

USB DATA

43 ADC3

32

10

45

STANDARDSERVO MOTOR

Servo motor

Procedure

L6.1.1 Create the new sketch file. Type the Listing L6-1 and save as servo_01.pde file

L6.1.2 Compile and upload the sketch to the ATX board.

L6.1.3 Run the program.

The servo motor shaft will move between 60 and 120 degrees position every 5

seconds

Page 70: AT-BOT_E120703

70Robo-Creator : AT-BOT activity book

#include <atx.h> // Include the main library

void setup()

{}

void loop()

{

servo(12,60); // Drive the servo motor at port 12 to move its shaft to// 60 degrees position

sleep(5000); // Delay 5 seconds

servo(12,120); // Drive the servo motor at port 12 to move its shaft to// 120 degrees position

sleep(5000); // Delay 5 seconds

}

Code explanation

The program repeats to work in the loop function to drive a servo motor to move its shaft

between the position of 60 and 120 degrees in every 5 seconds.

Experiment 6.2 Switch-controlled the servo motor position

This experiment demonstrates about control the servo motor shaft position by 2 of

switch on the ATC board. The SW1 is used to increase the position angle ans SW2 is used to

decrease the position angle.

Hardware connection

Connect a standard servo motot to servo motor output port 12 of the ATX board

Procedure

L6.2.1 Create the new sketch file. Type the Listing L6-2 and save as servo_02.pde file

L6.2.2 Compile and upload the sketch to the ATX board.

L6.2.3 Run the program.

At the LCD display of the ATX controller board show message as follows :

Servo: xxx

therefore xxx is servo motor shaft position. Start at 90

Listing L6-1 : servo_01.pde, the sketch file for driving a servo motor

to control the movement position

Page 71: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 71

#include <atx.h> // Include the main libraryint p=90; // Declare the position variable

// and set the default value as 90void setup(){}void loop(){ servo(12,p); // Drive a servo motor to move the shaft to position

// that defined by the p variable lcd("Servo: %d ",p); // Show the current position of servo motor shaft if(sw1()==0) // Is the SW1 switch pressed ? { p++; // Increase the position value if(p>180) // Is the position more than 180 ? { p=0; // If the position value more than 180,

// set the position value back to 0 } sleep(100); // Delay 0.1 second } if(sw2()==0) // Is the SW2 switch pressed ? { p-- ; // Decrease the postion value if(p<0) // Is the position lower than 0 ? { p=0; // If the position value lower than 0,

// set the position value as 0 } sleep(100); // Delay 0.1 second }}

Code explanation

The program repeats to work in the loop function to drive a servo motor to move its shaft

to the position stored in the variable p along with displaying a position value at the LCD module.

Furthermore, there are conditions of verification within the loop as follows:

1. If the SW1 switch is pressed (sw1( ) function returns the value as o)

The program responds by adding the p variable value to 1 value and check that

exceed 180 or not. If it is exceeded, set the position to start with the new 0. Next, time delay will

proceed to not cause the addition of values too fast.

2. If the SW2 switch is pressed (sw2( ) function returns the value as 0)

The program responds by reducing the p variable value to 1 value and verifies that

the value is below 0 or not. If it is, set the position to start with the new 0 and then time delay.

Listing L6-2 : servo_02.pde, the sketch file for driving a servo motor

to control the movement position by SW1 and SW2 switch on the

ATX board

Page 72: AT-BOT_E120703

72Robo-Creator : AT-BOT activity book

L6.2.4 Press the SW1 switch

The shaft position of the servo motor will be increased 1 value. At the same time,

the servo motor shaft will be moved according to the increasing value. When adding the

value to 180, the position value will be back to start at the new 0.

L6.2.5 Press the SW2 switch

The shaft position of the servo motor will be decreased 1 value. At the same time,

the servo motor shaft will be moved according to the decreasing value. When the value

is lower than 0, the position value will be back to start at the new 0.

Page 73: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 73

Experiment 7

Serial data communication with computers

Experiment 7.1 Transmit the serial data to computer

This experiment demonstrates about computer interfacing of the ATX controller

board. Begins with transmitting the serial data to USB port via the USB to serial converter

circuit on the ATX board. The data will be display on the Serial Monitor of Wiring IDE.

Connect to USB port

ON

MOTOR

BATTERY LEVEL1312111098

SERVO PORT

7.2

-9V B

ATT.

E2

RESET

+

-S

14 15PC6 PC7

ATX

con

1.0r >> >>>

troll r boa

>

e rdR

>

4850SW249SW1ADC7KNOB

TWI UART1

0 SCL 1 SDA 2 RX1 3 TX1

44 ADC446 ADC640 ADC041 ADC142 ADC2 STAR

T

45 ADC5

>>

USB DATA

43 ADC3

32

10

45

Procedure

L7.1.1 Create the new sketch file. Type the Listing L7-1 and save as uart_01.pde file

L7.1.2 Compile and upload the sketch to the ATX board. Still connect the USB cable with

USB port of computer.

Page 74: AT-BOT_E120703

74Robo-Creator : AT-BOT activity book

L7.1.3 Run the program. Click on the button to open the Serial Monitor of Wiring IDE

The Serial Monitor window appears and dsiplay message Hello Robot! every 2

seconds.

#include <atx.h> // Include the main library

void setup()

{}

void loop()

{

uart("Hello Robot!\r\n"); // Transmit the serial data// to computer with carrier return

sleep(2000); // Delay 2 seconds

}

Code explanation

The program reapeats working in the loop function to transmit some text “Hello Robot!” to

display on the Serial Monitor of the computer every 2 seconds continuously.

Listing L7-1 : uart_01.pde, the sketch file for transmitting the serial

data to computer of the ATX controller board

Page 75: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 75

#include <atx.h> // Include the main libraryvoid setup(){}void loop(){ uart("KNOB: %d \r\n",knob()); // Transmit the serial data

// to computer with carrier return sleep(100); // Delay 0.1 second}

Code explanation

The program repeats to work in the loop function to transmit the KNOB value to KNOB to

display on the Serial Monitor of the computer every 0.1 seconds continuously.

Experimenrt 7.2 KNOB data monitoring

This experiment demonstrates about transmitting the serial data from reading

theKNOB value of the ATX board to shows on the Serial Monitor. It is the simple sensor data

monitoring application.

Procedure

L7.2.1 Create the new sketch file. Type the Listing L7-2 and save as uart_02.pde file

L7.2.2 Compile and upload the sketch to the ATX board. Still connect the USB cable with

USB port of computer.

L7.2.3 Run the program. Click on the button to open the Serial Monitor of Wiring IDE

L7.2.4 Adjust the KNOB button and see the operation at the Serial Monitor of Wiring IDE.

The Serial Monitor displays message KNOB: xxx every 0.1 second. Therefore xxx

value us 0 to 1023.

Listing L7-2 : uart_02.pde, the sketch file for transmitting the KNOB

button of the ATX board to computer

Page 76: AT-BOT_E120703

76Robo-Creator : AT-BOT activity book

Experiment 7.3 Receiving the serial data from computer

In this experiment presents about receiving the data from computer via USB port.

The data will be transmitted out from computer by inputing with the SErial Monitor window

of Wiring IDE to control the sound generation of the ATX board.

Procedure

L7.3.1 Create the new sketch file. Type the Listing L7-3 and save as uart_03.pde file

L7.3.2 Compile and upload the sketch to the ATX board. Still connect the USB cable with

USB port of computer.

L7.3.3 Run the program. Click on the button to open the Serial Monitor of Wiring IDE

L7.3.4 Type character of number 1 at the transmit box. Choose the serial data parameter

box to No line ending. After that, click on the Send button of the Serial Monitor.

L7.3.5 Observe the operation of the ATX controller board.

The ATX board receives the number 1 data to generates the 500Hz signal with 0.1

second.

7.3.6 Type character of number 2 at the transmit box then click on the Send button of the

Serial Monitor.

L7.3.7 Observe the operation of the ATX controller board.

The ATX board receives the number 2 data to generates the 2000Hz signal with 0.5

second.

Page 77: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 77

#include <atx.h> // Include the main librarychar c; // Declare the data variablevoid setup(){}void loop(){ if(uart_available()) // Check the serial data receiving { c = uart_getkey(); // Store the serial data to the data variable

if(c=='1') // Is it number 1 data ? { beep(); // If yes, generate the 500Hz signal with 0.1 second } if(c=='2') // Is it number 2 data ? { sound(2000,500); // If yes, generate the 2000Hz signal with 0.5 second } }}

Code explanation

The program repeats to work in the loop function to receive the serial data from computer.

After the receiving occur, it will be stored data to the c variable and check the value as follows :

1. If the data is the number 1 (from pressing the key 1)

The program will respond by generating the 500Hz signal for 0.1 second.

2. If the data is the number 2 (from pressing the key 2)

The program will respond by generating the 2000 Hz signal for 0.5 second.

3. If it is another data

No response

Listing L7-3 : uart_03.pde, the sketch file for receiving the serial data

from computer of the ATX board

Page 78: AT-BOT_E120703

78Robo-Creator : AT-BOT activity book

Page 79: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 79

After try out with some experiment to verify the operation of the hardware in the

chapter 5. Next, it will be the part of understanding to the mechanical structure of AT-BOT

robots and how to move AT-BOT robots. The movement of AT-BOT robots is different from

familiar two wheeled mobile robots because an AT-BOT has 4 sets of DC motor gearbox

and spike wheels. Therefore, programming to drive robots also needs to be considered

the functions of all 4 motors. In this chapter, the content will be presented with examples

of programming under C/C++language of Wiring to manage AT-BOT to move in the basic

patterns, either moving straight, backward and turning or turning around in various ways.

Chapter 6

AT-BOT movement

Figure 6-1 : Illustration of determination of motors and wheels

positions in an AT-BOT

Right back wheelMotor-2

Left back wheelMotor-1

Right front wheelMotor-3

Left front wheelMotor-0

ON

MO

TO

R

BATTER

Y LE

VEL

13

12

11

10

98

SERV

O P

ORT

7.2-9V BATT.

E2

RESET

+

- S

14

15PC6

PC7

ATX

con

1.0r>>>>>

trollrboa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KN

OB

TW

IU

ART1

0SCL

1SD

A2

RX1

3T

X1

44

AD

C4

46

AD

C6

40

AD

C0

41

AD

C1

42

AD

C2

START

45

AD

C5

>>

USB

DAT

A

43

AD

C3

3210 4 5

Page 80: AT-BOT_E120703

80Robo-Creator : AT-BOT activity book

6.1 Mechanical structure of AT-BOT An AT-BOT robot has 4 sets of driving wheels and they are arranged according to

the positions as in the figure 6-1. Generally, driving AT-BOT robots with all 4 sets of driving

wheels can be efficient based on one factor you need to consider. The factor is the

friction between touching surface and wheels of a robot. In AT-BOT, we will use the 48:1

DC motor gearboxes and splike wheels. This will make motion good and fast on virtually all

surfaces. Torque obtained from the motor sets will be a lot and enough to drive a robot

moving up the slop of 20 degrees or rough surfaces.

Connection of each motor with a DC motor driving circuit on the ATX board of an

AT-BOT robot detailed as follows

The left front wheel is connected with the Motor-0 output.

The left back wheel is connected with the Motor-1 output

The right back wheel is connected with the Motor-2 output

The right front wheel is connected with the Motor-3 output

6.2 Principles of the movement of AT-BOT

6.2.1 Moving forward

To drive an AT-BOT robot to move forward can be done by controlling the DC

motor gearboxes of all 4 wheels to turn in the direction that forces the robot to move

forward with a stable driving power, such as driving with the power of 70% or 100%, etc. to

not cause turning around.

ON

MO

TO

R

BATTERY LE

VEL

13

1211

109

8

SERVO

PO

RT

7.2-9V BATT.

E2

RES

ET

+

- S

14

15

PC6

PC7

Cre

con

atore>

>>>

troll

rboa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KNO

B

TW

IU

ART1

0SC

L1

SD

A2

RX1

3TX1

44

AD

C4

46

AD

C6

40

AD

C0

41

AD

C1

42

AD

C2

START

45

AD

C5

>>

USB

DATA

43

AD

C3

3210 4 5

ON

MO

TO

R

BATTERY LE

VEL

13

1211

109

8

SERVO

PO

RT

7.2-9V BATT.

E2

RES

ET

+

- S

14

15

PC6

PC7

Cre

con

atore>

>>>

troll

rboa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KNO

B

TW

IU

ART1

0SC

L1

SD

A2

RX1

3TX1

44

AD

C4

46

AD

C6

40

AD

C0

41

AD

C1

42

AD

C2

START

45

AD

C5

>>

USB

DATA

43

AD

C3

3210 4 5

Page 81: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 81

6.2.2 Moving backwards

To drive an AT-BOT robot to move backwards can do by handling the DC motor

gearbox of all 4 wheels to turn in the direction that forces the robot to move backwards

with a stable driving power.

ON

MO

TO

R

BATT

ERY LE

VEL

13

1211

109

8

SERVO

PO

RT

7.2-9V BATT.

E2

RESET

+

- S

14

15PC6

PC7

Cre

con

atore>

>>>

troll

rboa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KNO

B

TW

IU

ART1

0SCL

1SD

A2

RX1

3TX1

44

AD

C4

46

AD

C6

40

AD

C0

41

AD

C1

42

AD

C2

START

45

AD

C5

>>

USB

DATA

43

AD

C3

3210 4 5

ON

MO

TO

R

BATT

ERY LE

VEL

13

1211

109

8

SERVO

PO

RT

7.2-9V BATT.

E2

RESET

+

- S

14

15PC6

PC7

Cre

con

atore>

>>>

troll

rboa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KNO

B

TW

IU

ART1

0SCL

1SD

A2

RX1

3TX1

44

AD

C4

46

AD

C6

40

AD

C0

41

AD

C1

42

AD

C2

START

45

AD

C5

>>

USB

DATA

43

AD

C3

3210 4 5

6.2.3 Turning to the left

There are two formats of the movement, including turning with two wheels and

turning around.

6.2.3.1 Turning to the left with two wheels

This movement uses 2 sets of motors and wheels in driving an AT-BOT to turn left by

controlling the right front wheel and the right back wheel to rotate forward. Meanwhile,

the left front wheel and the left back wheel are stopped, so the robot will be able to turn

left and the rotation point is between the left front wheel and the left back wheel as the

diagram below.

ON

MO

TO

R

BAT

TERY LE

VEL

13

12

11

10

98

SE

RVO

PO

RT

7.2-9V BATT.

E2

RESET

+

- S

14

15

PC6

PC7

Cre

con

atore>

>>>

troll

rboa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KN

OB

TW

IU

ART1

0S

CL

1SD

A2

RX1

3TX1

44

AD

C4

46

AD

C6

40

AD

C0

41

AD

C1

42

AD

C2

START

45

AD

C5

>>

USB

DATA

43

ADC

3

3210 4 5

ON

MO

TO

R

BAT

TERY LE

VEL

13

12

11

10

98

SE

RVO

PO

RT

7.2-9V BATT.

E2

RESET

+

- S

14

15

PC6

PC7

Cre

con

atore>

>>>

troll

rboa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KN

OB

TW

IU

ART1

0S

CL

1SD

A2

RX1

3TX1

44

AD

C4

46

AD

C6

40

AD

C0

41

AD

C1

42

AD

C2

START

45

AD

C5

>>

USB

DATA

43

ADC

3

3210 4 5

Turningpoint

Page 82: AT-BOT_E120703

82Robo-Creator : AT-BOT activity book

6.2.3.2 Turning to the left by spining

To drive an AT-BOT robot to turn left with another pattern is available by managing

the right front wheel and the right back wheel turning forward but the left front wheel and

the left back wheel will turn backwards or in the opposite direction. Therefore, AT-BOT

robot will turn left with spining and the turning point is at the middle of the robot’s body.

Turning itself to the left by this method will give a high power of the movement but the

robot needs more energy as well.

ON

MO

TO

R

BATTERY L

EVEL

13

12

11

10

98

SERVO

PO

RT

7.2-9V BATT.

E2

RESET

+

- S

14

15

PC6

PC7

Cre

con

atore>

>>>

troll

rboa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KN

OB

TW

IU

ART

1

0SCL

1SD

A2

RX1

3TX

1

44

AD

C4

46

ADC

640

AD

C0

41

AD

C1

42

AD

C2

START

45

AD

C5

>>

USB

DATA

43

ADC

3

3210 4 5

ON

MO

TO

R

BATTERY L

EVEL

13

12

11

10

98

SERVO

PO

RT

7.2-9V BATT.

E2

RESET

+

- S

14

15

PC6

PC7

Cre

con

atore>

>>>

troll

rboa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KN

OB

TW

IU

ART

1

0SCL

1SD

A2

RX1

3TX

1

44

AD

C4

46

ADC

640

AD

C0

41

AD

C1

42

AD

C2

START

45

AD

C5

>>

USB

DATA

43

ADC

3

3210 4 5

Turningpoint

6.2.4 Turning to the right

There are 2 formats of the movement, including turning by 2 wheels and spining.

6.2.4.1 Turning to the right by two wheels

To make AT-BOT robot move in this pattern of turning to the right will be done

differently from turning left. It is said that controlling the left front wheel and the left back

wheel to turn forward but the right front wheel and the right back wheel to stop. Therefore,

the robot will be able to turn left and has the rotation point between the right front wheel

and the right back wheel.

ON

MO

TO

R

BAT

TERY

LEVE

L1

31

211

10

98

SERVO

PO

RT

7.2-9V BATT.

E2

RES

ET

+

- S

14

15

PC6

PC7

Cre

con

atore>

>>>

troll

rboa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KN

OB

TW

IU

ART1

0S

CL

1SD

A2

RX1

3TX1

44

AD

C4

46

AD

C6

40

AD

C0

41

AD

C1

42

AD

C2

START

45

AD

C5

>>

USB

DATA

43

AD

C3

3210 4 5

ON

MO

TO

R

BAT

TERY

LEVE

L1

31

211

10

98

SERVO

PO

RT

7.2-9V BATT.

E2

RES

ET

+

- S

14

15

PC6

PC7

Cre

con

atore>

>>>

troll

rboa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KN

OB

TW

IU

ART1

0S

CL

1SD

A2

RX1

3TX1

44

AD

C4

46

AD

C6

40

AD

C0

41

AD

C1

42

AD

C2

START

45

AD

C5

>>

USB

DATA

43

AD

C3

3210 4 5

Turningpoint

Page 83: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 83

6.2.4.2 Turning to the right by spining

This driving will be done in the opposite way with turning to the left of the pattern 2

(topic 6.2.4). By controlling the left front wheel and the left back wheel to turn forward but

the right front wheel and the right back wheel to be turn backward. The AT-BOT will turn

right with spining and the turning point is at the middle of the robot’s body. With this method

will give a high power in the movement but need to use more energy as well.

ON

MO

TO

R

BATTERY L

EVEL

13

12

11

10

98

SERVO

PO

RT

7.2-9V BATT.

E2

RESET

+

- S

14

15

PC6

PC7

Cre

con

atore>

>>>

troll

rboa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KN

OB

TW

IU

ART

1

0SCL

1SD

A2

RX1

3TX

1

44

AD

C4

46

ADC

640

AD

C0

41

AD

C1

42

AD

C2

START

45

AD

C5

>>

USB

DATA

43

ADC

3

3210 4 5

ON

MO

TO

R

BATTERY L

EVEL

13

12

11

10

98

SERVO

PO

RT

7.2-9V BATT.

E2

RESET

+

- S

14

15

PC6

PC7

Cre

con

atore>

>>>

troll

rboa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KN

OB

TW

IU

ART

1

0SCL

1SD

A2

RX1

3TX

1

44

AD

C4

46

ADC

640

AD

C0

41

AD

C1

42

AD

C2

START

45

AD

C5

>>

USB

DATA

43

ADC

3

3210 4 5

Turningpoint

Page 84: AT-BOT_E120703

84Robo-Creator : AT-BOT activity book

Experiment 8

Wheel driving setting

This experiment must be done first for programming to control AT-BOT to move around

by presenting the adjustment step of the motor driving to test the basic movement.

AT-BOT are defined all motor connections as follows:

The left front wheel is connected with the Motor-0 output.

The left back wheel is connected with the Motor-1 output

The right back wheel is connected with the Motor-2 output

The right front wheel is connected with the Motor-3 output

In order that this test is in the same way, need to determine the direction of the

wheel rotation in each position. In here, a motor of any wheel is driven with a positive

value power (+). From the motor function, the motor will turn to the direction that forces a

robot to move forward. For example, when you drive the motor from the Motor-1 output

with a positive power, the motor shaft will rotate to the direction that makes a robot move

forward. If it rotates in the reverse direction, change the polarity connecting of the motor

cables at the Motor-1 output to opposite polarity. And then examine remaining 3 motors

and wheels with the same method.

ON

MO

TO

R

BATTERY LE

VEL

1312

11

10

98

SERVO

PO

RT

7.2-9V BATT.

E2

RESET

+

- S

1415

PC6

PC7

Cre

con

atore>

>>>

trollr

boa

>

erd

R

>

48

50

SW

249

SW

1A

DC7

KN

OB

TW

IU

AR

T1

0SCL

1SD

A2

RX1

3TX1

44AD

C4

46

AD

C6

40ADC0

41

ADC1

42

AD

C2

START

45

ADC5

>>

USB

DATA

43

AD

C3

3210 4 5

Page 85: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 85

#include <atx.h>

// Include the main library

void setup()

{}

void loop()

{

motor(0,50);

motor(1,50);

motor(2,50);

motor(3,50);

}

Procedure

L8.1 Remove all motor cable.

L8.2 Create the new sketch file. Type the Listing L8-1 and save as motor_test.pde file

L8.3 Compile and upload the sketch to the ATX board.

L8.4 Connect the left front motor cable to motor-0 output

If polarity is correct, robot’s wheel will rotate to the direction that drive the robot

forward. If not, swap the connection of motor cable

ON

MO

TO

R

BAT

TERY LE

VEL

13

1211

109

8

SERVO

PO

RT

7.2-9V BATT.

E2

RE

SET

+

- S

14

15PC6

PC7

Cre

con

atore>

>>>

troll

rboa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KNO

B

TW

IU

ART1

0SC

L1

SD

A2

RX1

3TX1

44

AD

C4

46

AD

C6

40

AD

C0

41

AD

C1

42

AD

C2

START

45

AD

C5

>>

USB

DATA

43

AD

C3

3210 4 5

Code explanation

The motor of the channel 0, 1, 2, and 3

are driven with +50% power. Observe the

voltage polarity that supplies to the motor from

color of the status LED on the motor output of

all 4 outputs will have to become green. (If

driving with a negative value, the LED will get

red). In order to define the polarity connecting

of all motor outputs according to the reference.

Apply this pattern of the connection of motor

cables on all experiments of this.

Listing L8-1 : motor_test.pde, the sketch file for checking the motor

connection of the AT-BOT

Page 86: AT-BOT_E120703

86Robo-Creator : AT-BOT activity book

L8.5 Connect the left back motor cable to motor-1 output of the AT-BOT

If polarity is correct, robot’s wheel will rotate to the direction that drive the robot

forward. If not, swap the connection of motor cable

ON

MO

TO

R

BAT

TERY LE

VEL

13

1211

109

8

SERVO

PO

RT

7.2-9V BATT.

E2

RE

SET

+

- S

14

15PC6

PC7

Cre

con

atore>

>>>

troll

rboa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KNO

B

TW

IU

ART1

0SC

L1

SD

A2

RX1

3TX1

44

AD

C4

46

AD

C6

40

AD

C0

41

AD

C1

42

AD

C2

START

45

AD

C5

>>

USB

DATA

43

AD

C3

3210 4 5

L8.6 Connect the right back motor cable to motor-2 output of AT-BOT

If polarity is correct, robot’s wheel will rotate to the direction that drive the robot

forward. If not, swap the connection of motor cable

ON

MO

TO

R

BAT

TERY LE

VEL

13

1211

109

8

SERVO

PO

RT

7.2-9V BATT.

E2

RE

SET

+

- S

14

15PC6

PC7

Cre

con

atore>

>>>

troll

rboa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KNO

B

TW

IU

ART1

0SC

L1

SD

A2

RX1

3TX1

44

AD

C4

46

AD

C6

40

AD

C0

41

AD

C1

42

AD

C2

START

45

AD

C5

>>

USB

DATA

43

AD

C3

3210 4 5

Page 87: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 87

L8.7 Connect the right back motor cable to motor-3 output of AT-BOT

If polarity is correct, robot’s wheel will rotate to the direction that drive the robot

forward. If not, swap the connection of motor cable

ON

MO

TO

R

BAT

TERY LE

VEL

13

1211

109

8

SERVO

PO

RT

7.2-9V BATT.

E2

RE

SET

+

- S

14

15PC6

PC7

Cre

con

atore>

>>>

troll

rboa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KNO

B

TW

IU

ART1

0SC

L1

SD

A2

RX1

3TX1

44

AD

C4

46

AD

C6

40

AD

C0

41

AD

C1

42

AD

C2

START

45

AD

C5

>>

USB

DATA

43

AD

C3

3210 4 5

Since all motor connections are correct and get the correct result.

User must use these motor connection and polarity mainly on

programming for all experiments in this activity book.

Page 88: AT-BOT_E120703

88Robo-Creator : AT-BOT activity book

Experiment 9

Creating the AT-BOT movement function

These experiments will present examples of program development to control the

AT-BOT’s movement by using the movement control function. Includes forward, backwards,

turn left, turn right and stop.

Experiment 9.1 Move forward function

This experiment demonstrates about AT-BOT moving forward after the SW1 switch is

pressed.

#include <atx.h> // Include the main library

void forward() // Function of driving a robot forward

{

motor(0,40); // Drive the motor of the left front wheel forward with 40% power

motor(1,40); // Drive the motor of the left back wheel forward with 40% power

motor(2,40); // Drive the motor of the right back wheel forward with 40% power

motor(3,40); // Drive the motor of the right front wheel forward with 40% power

}

void setup()

{

lcd("SW1 Press!"); // Display the title message

sw1_press(); // Wait until the SW1 is pressed

}

void loop()

{

forward(); // Drive a robot forward

}

Code explanation

When the program starts, there is a title message at the ATX board display and then wait

for the pressing of SW1. After SW1 is pressed, the program will repeat to work in the loop

function to drive motors in order to make the robot move forward constantly.

Addition Program developers can define motor driving power as appropriate. Generally, it is

defined that values of the motor driving power in all wheels should be the same. In case that

each motor operates differently, a developer needs to test by adjusting a value of driving power

of each motor to be not the same in order to find out the best value that makes a robot move

best as possible.

Listing L9-1 : robot_move_forward.pde, the sketch file for controlling

the AT-BOT to move forward

Page 89: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 89

Procedure

L9.1.1 Create the new sketch file. Type the Listing L9-1 and save as robot_move_forward.pdefile.

L9.1.2 Compile and upload the sketch to the AT-BOT.

L9.1.3 Run the program.

AT-BOT display shows the title message ; SW1 Press.

AT-BOT moves forward after the SW1 is pressed.

Experiment 9.2 Move backward function

This experiment demonstrates about AT-BOT moving backward after the SW1 switch

is pressed.

Procedure

L9.2.1 Create the new sketch. Type the Listing L9-2 and save as robot_move_backward.pdefile.

L9.2.2 Compile and upload the sketch to the AT-BOT.

L9.2.3 Run the program.

AT-BOT display shows the title message ; SW1 Press.

AT-BOT moves backward after the SW1 is pressed.

#include <atx.h> // Include the main library

void backward() // Function of driving a robot backward

{

motor(0,-40); // Drive the motor of the left front wheel backward with 40% power

motor(1,-40); // Drive the motor of the left back wheel backward with 40% power

motor(2,-40); // Drive the motor of the right back wheel backward with 40% power

motor(3,-40); // Drive the motor of the right front wheel backward with 40% power

}

void setup()

{

lcd("SW1 Press!"); // Display the title message

sw1_press(); // Wait until the SW1 is pressed

}

void loop()

{

backward(); // Drive a robot backward

}

Listing L9-2 : robot_move_backward.pde, the sketch file for controlling

the AT-BOT to move backward

Page 90: AT-BOT_E120703

90Robo-Creator : AT-BOT activity book

Experiment 9.3 Turn left function

This experiment demonstrates about AT-BOT turning left after the SW1 switch is

pressed.

Procedure

L9.3.1 Create the new sketch file. Type the Listing L9-3 and save as robot_turn_left.pde file.

L9.3.2 Compile and upload the sketch to the AT-BOT.

L9.3.3 Run the program.

AT-BOT display shows the title message ; SW1 Press.

AT-BOT turns left after the SW1 is pressed.

#include <atx.h> // Include the main library

void turn_left() // Function of driving a robot to turn left

{

motor(0,-40); // Drive the motor of the left front wheel backward with 40% power

motor(1,-40); // Drive the motor of the left back wheel backward with 40% power

motor(2,40); // Drive the motor of the right back wheel forward with 40% power

motor(3,40); // Drive the motor of the right front wheel forward with 40% power

}

void setup()

{

lcd("SW1 Press!"); // Display the title message

sw1_press(); // Wait until the SW1 is pressed

}

void loop()

{

turn_left(); // Drive a robot to turn left

}

Listing L9-3 : robot_turn_left.pde, the sketch file for controlling the

AT-BOT to turn left

Page 91: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 91

Experiment 9.4 Turn right function

This experiment demonstrates about AT-BOT turning right after the SW1 switch is

pressed.

Procedure

L9.4.1 Create the new sketch file. Type the Listing L9-4 and save as robot_turn_right.pde file.

L9.4.2 Compile and upload the sketch to the AT-BOT.

L9.4.3 Run the program.

AT-BOT display shows the title message ; SW1 Press.

AT-BOT turns right after the SW1 is pressed.

#include <atx.h> // Include the main library

void turn_right()// Function of driving a robot to turn right

{

motor(0,40); // Drive the motor of the left front wheel forward with 40% power

motor(1,40); // Drive the motor of the left back wheel forward with 40% power

motor(2,-40); // Drive the motor of the right back wheel backward with 40% power

motor(3,-40); // Drive the motor of the right front wheel backward with 40% power

}

void setup()

{

lcd("SW1 Press!"); // Display the title message

sw1_press(); // Wait until the SW1 is pressed

}

void loop()

{

turn_left(); // Drive a robot to turning right

}

Listing L9-4 : robot_turn_right.pde, the sketch file for controlling the

AT-BOT to turn right

Page 92: AT-BOT_E120703

92Robo-Creator : AT-BOT activity book

Experiment 9.5 Timimg control the robot movement

This experiment demonstrates how to control the AT-BOT’s movement by time setting.

Procedure

L9.5.1 Create the new sketch file. Type the Listing L9-5 and save as robo_pause.pde file.

L9.5.2 Compile and upload the sketch to the AT-BOT.

L9.5.3 Run the program.

AT-BOT display shows the title message ; SW1 Press.

AT-BOT moves forward with 2 seconds and stop for 3 seconds alternate continually

after the SW1 is pressed.

#include <atx.h> // Include the main library

void forward() // Function of driving a robot forward

{

motor(0,40); // Drive the motor of the left front wheel forward with 40% power

motor(1,40); // Drive the motor of the left back wheel forward with 40% power

motor(2,40); // Drive the motor of the right back wheel forward with 40% power

motor(3,40); // Drive the motor of the right front wheel forward with 40% power

}

void setup()

{

lcd("SW1 Press!"); // Display the title message

sw1_press(); // Wait until the SW1 is pressed

}

void loop()

{

forward(); // Drive a robot forward

sleep(2000); // Delay 2 seconds

motor_stop(ALL); // Stop all moving

sleep(3000); // Delay 3 seconds

}

Listing L9-5 : robo_pause.pde, the sketch file for controlling the AT-

BOT to move with time setting

Page 93: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 93

Subsequent learning after driving robots is to read values from sensors in order to

define the conditions in the movement and the most basic sensor that functions in here is

switch. In this chapter, it will mention about application of a switch input board cooperated

with AT-BOT so as to detect touching with obstacles and hence a robot will be able to

recognize moving to meet obstacles. After this it will process to define movement to be

capable of passing through the obstacles.

An important material used in this learning is a switch input board. There is a circuit

and a working diagram shown as in the figure 7-1. When there is a pressing of switch, it

means touching or bumping with obstacles. The logic output signal will change from the

logic ‘1’ to ‘0’ until release the switch or there is no bumping and then the output signal

will change back to be ‘1’ again.

With this sensor characteristic, it is used to determine the conditions of the movement

for AT-BOT by attaching the switches at the front of a robot body. Once a robot runs to

touch or crash with any obstruction, the switch will be pressed and the controller will

recognize a change of the output connected with the switch. Therefore, this process will

control the robot to move backward and followed by changing a direction of movement.

Only this, the robot will be able to move passed the obstacless.

When there is no pressing the

switch, the DATA pin has a

logic ‘1’ from connection of

the resistor R2 with the supply

voltage.

When pressing the switch, it will

cause the connection

between the DATA pin and

ground. Therefore, the pin

DATA will have a logic as ‘0’

and there will be electricity

current through the LED 1.

Finally, the LED1 will light up.

DATAR3220

R210k

R1510

LED1

S1

LED status

GND

+V

Output

Switch

Figure 7-1 : Picture, schematic and and circuit explanation of the

Switch input board used in AT-BOT

Chapter 7

Object avoidance by contact

Switch

Output

LEDstatus

Page 94: AT-BOT_E120703

94Robo-Creator : AT-BOT activity book

Experiment 10

Reading the Switch input board

This experiment is a test for function of a switch input board on AT-BOT by which the

ATX board will read the status of pressing the switch from the switch input board connected

with ADC0 port (or the port 40) to show at the LCD module of the ATX board.

Additional hardware connection

Attach a switch input board at the front of the AT-BOT

Connect the switch unput board cable to ADC0 port or Port 40 of the ATX board on the

AT-BOT

The front of AT-BOT

Switch input board

ON

MO

TO

R

BATTERY LE

VEL

13

1211

109

8

SERVO

PO

RT

7.2-9V BATT.

E2

RESET

+

- S

1415

PC6

PC7

Cre

con

atore>

>>>

troll

rboa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KN

OB

TW

IU

ART1

0SCL

1SD

A2

RX1

3TX1

44

AD

C4

46

ADC

640

AD

C0

41

AD

C1

42

ADC

2

START

45

AD

C5

>>

USB

DATA

43

ADC

3

3210 4 5

connect to ADC0

#include <atx.h> // Include the main library

void setup()

{}

void loop()

{

lcd("Switch1: %d ",in(40)); // Read the switch input board status from} // ADC0/40 port to display on the LCD module

// of the AT-BOT

Listing L10-1 : switch1_test.pde, the sketch file for reading status of

the Switch input board that interface with AT-BOT at ADC0/40 port

Page 95: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 95

Procedure

L10.1 Create the new sketch file. Type the Listing L10-1 and save as switch1_test.pde file.

L10.2 Compile and upload the sketch to the AT-BOT.

L10.3 Run the program.

AT-BOT display shows the title message :

Switch: x

therefore x is the reading data of the Swich input board

L10.4 Press and hold the switch and then check the result of the operation.

The status value of the switch (x) will change to 0 and when you release the switch,

the result will return to the value of 1 again.

Page 96: AT-BOT_E120703

96Robo-Creator : AT-BOT activity book

Experiment 11

AT-BOT avoid the obstacle by touching

This experiment demonstrates about programming to controls AT-BOT in order to

detect an obstacle by bumping and then change its direction to avoid the obstacle. A

touch sensor or a switch input board is applied and installed at the front of a robot

according to the experiment 10. There are conditions of the operation as follows:

1. A robot does not find any collision, the robot will move straight forward

constantly.

2. A robot is crashed, there will be a loud sound out once. Then, the robot will

step backward and change the direction to the left and keep moving forward to avoid

the object.

Obstacle Obstacle

ON

MO

TO

R

BA

TTER

Y LE

VEL

13

1211

10

98

SERV

O PO

RT

7.2-9V BATT.

E2

RESET

+

- S

1415

PC6

PC7

Cre

con

atore>>>>

troll

rboa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KN

OB

TW

IU

ART

10

SCL

1SD

A2

RX1

3TX1

44

ADC

446

AD

C6

40

AD

C0

41

AD

C1

42

ADC

2

START

45

AD

C5

>>

USB

DATA

43

AD

C3

3210 4 5

ON

MO

TO

R

BATTERY LE

VEL

13

12

11

10

98

SERVO

PO

RT

7 .2 -9V BATT.

E2

RESE

T

+

- S

14

15PC6

PC7

Cre

con

atore>

>>>

troll

rboa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KN

OB

TW

IU

ART1

0SCL

1SD

A2

RX1

3TX1

44

AD

C4

46

AD

C6

40

ADC

041

AD

C1

42

AD

C2

START

45

ADC

5

>>

USB

DATA

43

ADC

3

3210 4 5

Obstacle

ON

MO

TO

R

BATTERY LE

VEL

1312

11

10

98

SERVO

PO

RT

7.2 -9V BATT.

E2

RESET

+

- S

14

15

PC6

PC7

Cre

con

atore>

>>>

trollr

boa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KN

OB

TW

IU

ART

1

0SCL

1SD

A2

RX1

3TX1

44

AD

C4

46

ADC

640

AD

C0

41

ADC

142

AD

C2

START

45

AD

C5

>>

USB

DATA

43

AD

C3

3210 4 5

ON

MO

TO

R

BATTERY LE

VEL

1312

11

10

98

SERVO

PO

RT

7.2-9V BATT.

E2

RESET

+

- S

14

15

PC6

PC7

Cre

con

atore>

>>>

trollr

boa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KN

OB

TW

IU

ART

10

SCL

1SD

A2

RX1

3TX1

44

AD

C4

46

ADC

640

AD

C0

41

ADC

142

AD

C2

START

45

AD

C5

>>

USB

DATA

43

AD

C3

3210 4 5

ON

MOTO

R

BATTERY LEVEL

13

12

11

10

9

8SERVO PORT

7.2-

9V BATT

.

E2

RESET

+

-

S

14

15

PC6

PC7

Crec

onatore>

>>>

troll

r

boa

>e

rd

R>

48

50

SW

2

49

SW

1

ADC

7

KNOB

TWI

UART

1

0SCL

1SD

A2

RX13

T X1

44AD

C4

46ADC

6

40

ADC0

41

ADC 1

4 2AD

C2

START

45

ADC5

>>

USBDA TA

43ADC

3

3

2

1

0

4

5

ON

MO

TO

R

BATTERY LE

VEL

13

12

11

10

98

SERVO

PO

RT

7.2-9V BATT.

E2

RESE

T

+

- S

14

15PC6

PC7

Cre

con

atore>

>>>

troll

rboa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KN

OB

TW

IU

ART1

0SCL

1SD

A2

RX1

3TX1

44

AD

C4

46

AD

C6

40

ADC

041

AD

C1

42

AD

C2

START

45

ADC

5

>>

USB

DATA

43

ADC

3

3210 4 5

ON

MO

TO

R

BA

TTER

Y LE

VEL

13

1211

10

98

SERV

O PO

RT

7 .2 -9V BATT.

E2

RESET

+

- S

1415

PC6

PC7

Cre

con

atore>>>>

troll

rboa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KN

OB

TW

IU

ART

1

0SC

L1

SDA

2R

X13

TX1

44

ADC

446

AD

C6

40

AD

C0

41

AD

C1

42

ADC

2

START

45

AD

C5

>>

USB

DATA

43

AD

C3

3210 4 5

Obstacle

Procedure

L11.1 Create the new sketch file. Type the Listing L10-1 and save as switch1_test.pde file.

L11.2 Compile and upload the sketch to the AT-BOT. Unplug the USB cable from the robot.

L11.3 Place the robot on the floor. Place some obstacle such as box or can to set the

demonstration field.

L11.4 Run the program.

AT-BOT display shows the title message : Press SW1

L11.5 Press the SW1 at AT-BOT.

AT-BOT move forward. If it will bump a obstacle, it will step back and change a

direction to the left. After that the robot will move forward continuously.

Page 97: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 97

#include <atx.h> // Include the main library#define POW 80 // Set the motor power to 60%char mid; // Declare the switch status variablevoid forward(unsigned int time) // Moving forward function{ motor(0,POW); // Drive the Motor-0 forward motor(1,POW); // Drive the Motor-1 forward motor(2,POW); // Drive the Motor-2 forward motor(3,POW); // Drive the Motor-3 forward sleep(time); // Delay for moving forward}void backward(unsigned int time){ motor(0,-POW); // Drive the Motor-0 backward motor(1,-POW); // Drive the Motor-1 backward motor(2,-POW); // Drive the Motor-2 backward motor(3,-POW); // Drive the Motor-3 backward sleep(time); // Delay for moving backward}void turn_left(unsigned int time){ motor(0,-POW); // Drive the Motor-0 backward motor(1,-POW); // Drive the Motor-1 backward motor(2,POW); // Drive the Motor-2 forward motor(3,POW); // Drive the Motor-3 forward sleep(time); // Delay for turning left}void turn_right(unsigned int time){ motor(0,POW); // Drive the Motor-0 forward motor(1,POW); // Drive the Motor-1 forward motor(2,-POW); // Drive the Motor-2 backward motor(3,-POW); // Drive the Motor-3 backward sleep(time); // Delay for turning right}

void setup(){ lcd("SW1 Press!"); // Display title message for asking to press the SW1 sw1_press(); // Wait until the SW1 is pressed}void loop(){ mid = in(40); // Read the switch status to store to the mid variable if(mid==0) // Check the switch pressing { beep(); // If the sensor is pressed, drive a beep once backward(500); // Move backward 0.5 second turn_left(800); // Turn left 0.8 second for avoiding the obstacle } else { forward(1); // The robot move forward with 0.001 second }}

Listing L11-1 : robo_bumper1.pde, the sketch file for demonstration

the AT-BOT avoid the obstacle by using one touch sensor (continue)

Page 98: AT-BOT_E120703

98Robo-Creator : AT-BOT activity book

Code explanation

The program begins with display the title message for asking the SW1 pressing in order to

wait for pressing the switch of SW1 by the sw1_press function. When the SW1 on the AT-BOT is

pressed, the program will be able to follow a command in the next line, which is to work in the

loop function. It defines to repeat reading a status value of the ADC0/40 port, which is connected

with the switch input board or touch sensor. Store the value at the variable mid. Once the switch

input board is pressed, it will give the result as ‘0’ and then the program will bring the value of

the variable to compare as the following:

The case of if(mind==0) : it is the checking the switch input board is pressed or

not. If it is true, this will influence the robot to drive a beep sound and move backward. After

that the robot will turn left to avoid an object (the value of time delay will be adjusted by a

developer as appropriate).

The case of else : it is inferred that the robot has not found any obstacle. If is true,

it will cause the robot to move forward for a short distance.

Listing L10-1 : switch1_test.pde, the sketch file for reading status of

the Switch input board that interface with AT-BOT at ADC0/40 port

(final)

Page 99: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 99

Experiment 12

Reading two touch sensors

This experiment purpose is reading the status of two of switch input boards. Now it

is called “Touch sensor”. The ATX board will read the status of switch pressing from the

switch input board connected with ADC0/40 and ADC2/41 ports to show at the LCD

module of the ATX board.

Additional hardware connection

Attach two of the switch input board at the front of the AT-BOT

Connect the switch input board #1 cable to ADC0/40 port of the AT-BOT

Connect the switch input board #2 cable to ADC1/41 port of the AT-BOT

ON

MO

TO

R

BATTERY LE

VEL

1312

11

109

8

SERVO

PO

RT

7.2-9V BATT.

E2

RESET

+

- S

1415

PC6

PC7

Cre

con

atore>>>>

troll

rboa

>

erd

R

>

48

50

SW

249

SW

1ADC7

KN

OB

TW

IU

ART1

0SCL

1SD

A2

RX1

3TX1

44

AD

C4

46

AD

C6

40

AD

C0

41

AD

C1

42

AD

C2

START

45

AD

C5

>>

USB

DATA

43

AD

C3

3210 4 5

connect toADC0/40

connect toADC1

Switch inputboard #1

Switch inputboard #2

The front of AT-BOT

Procedure

L12.1 Create the new sketch file. Type the Listing L12-1 and save as switch2_test.pde file.

L12.2 Compile and upload the sketch to the AT-BOT.

Page 100: AT-BOT_E120703

100Robo-Creator : AT-BOT activity book

L12.3 Run the program.

AT-BOT display shows the title message : Press SW1

L12.4 Press the SW1 on the AT-BOT.

AT-BOT display shows the title message :

Switch1: x

Switch2: y

therefore x and y are the reading data of the Swich input board #1 and 2

consequently

L12.5 Press the Switch input board #1 (at port ADC0/40) and see the operation.

The status value of the switch (x) will change to 0 and after release the switch, the

result will return to the value of 1 again.

L12.6 Press the Switch input board #1 (at port ADC1/41) and see the operation.

The status value of the switch (y) will change to 0 and after release the switch, the

result will return to the value of 1 again.

#include <atx.h> // Include the main library

void setup()

{}

void loop()

{

lcd("Switch1: %d #nSwitch2: %d ",in(40),in(41));

// Read the switch input board status from ADC0/40

// and ADC1/41 port to display on the LCD module of the AT-BOT

}

Listing L12-1 : switch2_test.pde, the sketch file for reading status of

two Switch input board that interface with AT-BOT at ADC0/40 and

ADC1/41 port

Page 101: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 101

Experiment 13

AT-BOT avoid the obstacle with two touch sensors

This experiment demonstrates about programming to using two switch input boards

as two of touch sensors. One is installed at the left front side of the AT-BOT. Another one is

on the right front side. The operated condition is as follows :

1. A robot does not find any collision, the robot will move straight forward

constantly.

2. A robot is crashed on the left side, there will be a loud sound out once. Then,

the robot will move backward and change the direction to the right and keep moving

forward to avoid the object.

ObstacleObstacle

ON

MO

TO

R

BATTERY LE

VEL

13

12

11

10

98

SERVO

PORT

7.2-9V BATT.

E2

RESET

+

- S

14

15

PC6

PC7

Cre

con

atore>

>>>

troll

rboa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KN

OB

TW

IU

ART1

0SCL

1SD

A2

RX1

3TX

1

44

AD

C4

46AD

C6

40AD

C041

AD

C1

42

AD

C2

START45

AD

C5

>>

USB

DATA

43

AD

C3

3210 4 5

ON

MO

TO

R

BATTERY

LEVEL

13

12

11

10

98

SER

VO

PORT

7.2-9V BA TT.

E2

RESET

+

- S

14

15

PC6

PC7

Cre

con

atore>

>>>

troll

rboa

>

erd

R

>

48

50SW

249

SW

1AD

C7

KN

OB

TW

IU

ART1

0SCL

1SD

A2

RX

13

TX1

44

ADC4

46

AD

C6

40

AD

C0

41

AD

C1

42

AD

C2

START

45

AD

C5

>>

USB

DATA

43AD

C3

3210 4 5

ON

MO

TO

R

BATT

ERY LE

VE

L13

12

11

10

98

SERVO

PO

RT

7.2-9V BATT.

E2

RES

ET

+

- S

14

15

PC6

PC7

Cre

con

atore>

>>>

troll

rboa

>

erd

R

>

4850

SW

249

SW

1ADC

7KNO

B

TW

IU

ART1

0SCL

1SD

A2

RX1

3TX1

44

AD

C4

46

ADC6

40

AD

C0

41AD

C1

42

ADC2

STA RT

45

AD

C5

>>

USB

DAT

A

43

AD

C3

3210 4 5

ON

MO

TO

R

BATTER

Y LEV

EL

13

12

11

10

98

SERVO

PO

RT

7.2-9V BATT.

E2

RES

ET

+

- S

14

15

PC6

PC

7

Cre

con

atore>>>>

troll

rboa

>

erd

R

>

48

50

SW

249

SW

1ADC7

KNO

B

TW

IU

ART1

0S

CL1

SD

A2

RX1

3TX1

44AD

C4

46

AD

C6

40

AD

C0

41

AD

C1

42AD

C2

STA RT

45

AD

C5

>>

USB

DAT

A

43

AD

C3

3210 4 5

ON

MO

TO

R

BATTERY LE

VEL

13

12

11

10

98

SER

VO

PORT

7.2-9V BATT.

E2

RESET

+

- S

14

15

PC6

PC7

Cre

con

atore>

>>>

trollr

boa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KN

OB

TW

IU

ART1

0SCL

1SD

A2

RX1

3TX

1

44

AD

C446

AD

C6

40

AD

C041

AD

C1

42

AD

C2

START

45

AD

C5

>>

USB

DATA

43

AD

C3

3210 4 5

ON

MOTO

R

BATTERY LEVEL

13

12

11

10

98

SERVO PORT

7. 2-9V B ATT.E2

RESET

+-

S

14

15

PC6

PC7

Cre

con

atore>

>>>

troll

r

boa

>

e

rd

R

>

48

50

SW2

49

SW

1

ADC7

KNOB

TWI

UART1

0SCL

1SDA

2RX1

3TX1

44ADC4

46ADC6

40AD

C0

41ADC1

42ADC2

STAR T

45ADC5

>>

USBDATA

43A

DC3

3

2

1

0

4

5

Obstacle Obstacle

3. A robot is crashed on the right side, there will be a loud sound out once. Then,

the robot will move backward and change the direction to the left and keep moving

forward to avoid the object.

ObstacleObstacle

ON

MO

TO

R

BAT

TERY LE

VEL

13

12

11

10

98 S

ERVO

PO

RT

7.2-9V BATT .

E2

RESE

T

+

- S

14

15

PC6

PC7

Cre

con

atore>

>>>

trollrboa

>

erd

R

>

48

50

SW

249

SW

1ADC

7KN

OB

TW

IUA

RT1

0SC

L1

SDA

2RX1

3TX

1

44

AD

C4

46AD

C6

40

AD

C0

41

AD

C1

42

AD

C2

START

45

AD

C5

>>

US

BD

ATA

43

AD

C3

3210 4 5

ON

MO

TO

R

BAT

TERY LE

VEL

13

12

11

10

98 S

ERVO

PO

RT

7.2-9V BATT.

E2

RES

ET

+

- S

14

15

PC6

PC7

Cre

con

atore>

>>>

trollr

boa

>

erd

R

>

48

50

SW

249

SW

1A

DC

7KN

OB

TW

IUA

RT1

0SC

L1

SDA

2RX1

3TX

1

44

AD

C4

46AD

C6

40AD

C0

41

AD

C1

42

AD

C2

START

45

AD

C5

>>

US

BD

ATA

43

AD

C3

3210 4 5

ON

MO

TO

R

BA

TTERY LE

VE

L13

12

11

10

98

SERVO

PO

RT

7.2-9V BATT.

E2

RESE

T

+- S

14

15

PC

6PC7

Cre

con

atore>>>>

troll

rboa

>

erd

R

>

4850

SW

249

SW

1AD

C7

KNO

B

TW

IU

ART1

0SC

L1

SD

A2

RX

13

TX1

44

AD

C4

46

AD

C6

40

AD

C0

41

AD

C1

42

AD

C2

START

45AD

C5

>>

USB

DAT

A

43

AD

C3

3210 4 5

ON

MO

TO

R

BA

TTERY LE

VE

L13

12

11

10

98

SERVO

PO

RT

7.2-9V BATT.

E2

RESET

+- S

14

15

PC

6PC7

Cre

con

atore>>>>

trollr

boa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KNO

B

TW

IU

ART1

0SC

L1

SD

A2

RX

13

TX1

44

AD

C4

46

AD

C6

40

AD

C0

41

AD

C1

42

AD

C2

START

45

AD

C5

>>

USB

DAT

A

43

AD

C3

3210 4 5

ON

MO

TO

R

BAT

TERY LE

VEL

13

12

11

10

98 S

ERVO

PO

RT

7.2-9V BATT.

E2

RES

ET

+

- S

14

15

PC6

PC7

Cre

con

atore>

>>>

trollrboa

>

erd

R

>

48

50

SW

249

SW

1A

DC7

KN

OB

TW

IUA

RT1

0SC

L1

SDA

2RX1

3TX

1

44AD

C4

46

AD

C6

40

AD

C0

41

AD

C1

42

AD

C2

START

45

AD

C5

>>

US

BD

ATA

43

AD

C3

3210 4 5

ON

MOTO

R

BATTERY LEVEL

13

12

11

10

9

8SERVO PORT

7.2 -9

V BA

TT.

E2

R ESET

+

-

S

14

15

PC6

PC7

Crec

onatore>

>>>

troll

r

boa

>e

rd

R>

48

50

SW2

49

SW1

ADC7

K NOB

TWI

UART 1

0SCL

1SDA

2RX1

3TX1

44ADC4

46AD

C6

40ADC 0

41ADC1

4 2AD

C2

STAR

T

45AD

C5

>>

USBD ATA

43ADC

3

3

2

1

0

4

5

Obstacle Obstacle

Page 102: AT-BOT_E120703

102Robo-Creator : AT-BOT activity book

4. A robot is crashed on the front side. Both touch sensors are attacked. There will

be a loud sound out once. Then, the robot will move backward and change the direction

to the left and keep moving forward to avoid the object.

Obstacle Obstacle

ON

MO

TO

R

BATTERY LE

VEL

13

12

11

10

98

SERV

O PO

RT

7.2-9V B ATT.

E2

RESE

T

+

- S

14

15

PC6

PC7

Cre

con

atore>>>>

troll

rboa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KNO

B

TW

IUART1

0SC

L1

SDA

2RX

13

TX

1

44

AD

C4

46

AD

C6

40

AD

C0

41

AD

C1

42

AD

C2

ST ART

45

AD

C5

>>

US

BD

ATA

43

AD

C3

3210 4 5

ON

MO

TO

R

BA

TTERY LEV

EL

13

12

11

10

98

SERVO

PORT

7.2-9V BATT.

E2

RESE

T

+

- S

14

15

PC6

PC7

Cre

con

atore>>>>

troll

rboa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KN

OB

TW

IUA

RT1

0SC

L1

SDA

2RX1

3TX

1

44

AD

C4

46

AD

C6

40

AD

C0

41

AD

C1

42

AD

C2

START

45AD

C5

>>

US

BD

ATA

43

AD

C3

3210 4 5

ON

MOT

OR

BAT

TERY LE

VEL

13

12

11

10

98

SERV

O P

ORT

7.2-9V BATT .

E2

RESE

T

+

- S

14

15

PC6

PC

7

Cre

con

atore>>>>

trollr

boa

>

erd

R

>

48

50

SW

249

SW

1ADC

7K

NO

B

TW

IU

ART1

0SC

L1

SD

A2

RX

13

TX1

44

AD

C4

46AD

C6

40

AD

C0

41

AD

C1

42

AD

C2

START

45

AD

C5

>>

USB

DAT

A

43

AD

C3

3210 4 5

ON

MO

TO

R

BATTERY LE

VEL

13

12

11

10

98

SERV

O PO

RT

7.2-9V BATT .

E2

RES

ET

+

- S

14

15

PC6

PC7

Cre

con

atore>>>>

troll

rboa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KNO

B

TW

IU

AR

T1

0SCL

1SD

A2

RX1

3TX

1

44

AD

C4

46

AD

C6

40

AD

C0

41

AD

C1

42

AD

C2

START

45

AD

C5

>>

US

BD

ATA

43

AD

C3

3210 4 5

ON

MOT

OR

BAT

TERY LE

VEL

13

12

11

10

98

SER

VO

PO

RT

7.2-9V BATT.

E2

RESE

T

+

- S

14

15

PC6

PC

7

Cre

con

atore>

>>>

trollrboa

>

erd

R

>

48

50

SW

249

SW

1ADC

7KN

OB

TW

IU

ART1

0SC

L1

SD

A2

RX

13

TX1

44

AD

C4

46

AD

C6

40

AD

C0

41

AD

C1

42

AD

C2

START

45

AD

C5

>>

USB

DAT

A

43

AD

C3

3210 4 5

ON

MO TOR

BATTERY LEVEL

13

12

11

109

8SERVO PORT

7.2-

9V B

ATT.

E2

RES ET

+

-

S

14

15

PC6

PC7

Crec

onatore>

>>>

troll

r

boa

>e

rd

R >

48

50

SW2

49

SW1

ADC7

KNOB

TWI

UART1

0SCL

1SDA

2R X1

3TX1

44ADC4

46ADC6

40ADC0

41ADC1

42ADC2

STAR

T

45ADC5

>>

USBDATA

43ADC3

3

2

1

0

4

5

ObstacleObstacle

Additional hardware connection and construction

Attach 2 of Switch inpiut board at the fronmt of AT-BOT by using the Right angle joiners,

Straigh joiners, 3 x 10mm. screws and 3mm. nuts. Attach the sensor at left front and right

front side of the robot by doing at an angle about 45 degrees.

- Attach the switch input board #1 at left front side and connect its cable to ADC0/

40 port of the AT-BOT

- Attach the switch input board #2 at right front side and connect its cable to

ADC1/41 port of the AT-BOT

Procedure

L13.1 Create the new sketch file. Type the Listing L13-1 and save as robo_bumper2.pde.

L13.2 Compile and upload the sketch to the AT-BOT. Unplug the USB cable from the robot.

L13.3 Place the robot on the floor. Place some obstacle such as box or can to set the

demonstration field.

L13.4 Run the program.

AT-BOT display shows the title message : Press SW1

L13.5 Press the SW1 on the AT-BOT. Observe the robot operation.

AT-BOT will go straight constantly. If the robot finds a obstacle and there is collision

according to the defined condition, AT-BOT will be able to change the direction of

movement to avoid the obstacle.

Page 103: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 103

#include <atx.h> // Include the main library#define POW 80 // Set the motor power to 60%char left,right; // Declare the switch status variablevoid forward(unsigned int time) // Moving forward function{ motor(0,POW); // Drive the Motor-0 forward motor(1,POW); // Drive the Motor-1 forward motor(2,POW); // Drive the Motor-2 forward motor(3,POW); // Drive the Motor-3 forward sleep(time); // Delay for moving forward}void backward(unsigned int time){ motor(0,-POW); // Drive the Motor-0 backward motor(1,-POW); // Drive the Motor-1 backward motor(2,-POW); // Drive the Motor-2 backward motor(3,-POW); // Drive the Motor-3 backward sleep(time); // Delay for moving backward}void turn_left(unsigned int time){ motor(0,-POW); // Drive the Motor-0 backward motor(1,-POW); // Drive the Motor-1 backward motor(2,POW); // Drive the Motor-2 forward motor(3,POW); // Drive the Motor-3 forward sleep(time); // Delay for turning left}void turn_right(unsigned int time){ motor(0,POW); // Drive the Motor-0 forward motor(1,POW); // Drive the Motor-1 forward motor(2,-POW); // Drive the Motor-2 backward motor(3,-POW); // Drive the Motor-3 backward sleep(time); // Delay for turning right}void setup(){ lcd("SW1 Press!"); // Display title message for asking to press the SW1 sw1_press(); // Wait until the SW1 is pressed}void loop(){ left = in(40); // Read the left switch status to store to the left variable right = in(41); // Read the right switch status to store to the right variable if(left==1 && right==1) // Check both switch are not pressed (no attack) { forward(1); // Robot move forward short time } else if(left==0 && right==1) // Check only the left switch pressing { beep(); // If the left swith is pressed,

// drive a beep sound once backward(500); // Move backward 0.5 second turn_right(800); // Spin right 0.8 second } //for changing the direction of movement

Listing L13-1 : robo_bumper2.pde, the sketch file for demonstration

the AT-BOT avoid the obstacle by using two touch sensor (continue)

Page 104: AT-BOT_E120703

104Robo-Creator : AT-BOT activity book

else if(left==1 && right==0) // Check only the right switch pressing { beep(); // If the left swith is pressed,

// drive a beep sound once backward(500); // Move backward 0.5 second turn_left(800); // Spin left 0.8 second

// for changing the direction of movement } else if(left==0 && right==0) // Check both switch are pressed together { beep(); // If the both swith is pressed,

// drive a beep sound once backward(500); // Move backward 0.5 second turn_left(1500); // Spin left 1.5 seconds

// for changing the direction of movement }}

Code explanation

At the beginning of the operation, the program will show the text of Press SW1 at the

LCD module to wait for pressing the SW1 switch on the AT-BOT. When the SW1 is pressed, CPU

will operate the command in the loop to repeat reading status values of the both switch input

boards at ACD0/40 and ADC1/41 to store at the variable of left and right respectively. Any

switch board is pressed, this will give the result value as ‘0’ and then the program will take the

values of both variables to compare based on the below mentioned 4 cases:

Case 1: if(left==1 && right==1) verified whether there is no pressing both switches

or not. If it is true (no collision occurred), it will respond a robot to move forward in a short

distance because it has not found any obstacle.

Case 2: else if(left==0 && right==1) checked pressing only the left switch, so if

it is true (bumping at the left), there will be a response by making a beep sound and a robot will

step back to turn right in order to not clash with an obstacle.

Case 3: else if(left==1 && right==0) examined pressing only the right switch, if

it is correct (bumping at the right), it will react by a beep sound. Then, a robot will move

backward to turn left so it can avoid an obstacle.

Case 4: else if(left==0 && right==0) it is a test of pressing both switches. If it is

true (colliding with the front so both switches are pressed simultaneously), there will be 1

rhythm of a sound. Meanwhile, it will cause a robot to reverse and then turn left to avoid an

obstacle.

Program developers can modify the values of time delay used to define rhythmic movement

freely in each situation appropriately.

Listing L13-1 : robo_bumper2.pde, the sketch file for

demonstration the AT-BOT avoid the obstacle by using

two touch sensor (final)

Page 105: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 105

One mission of learning about programming to control a small autonomous robot

is detection and locomotion along lines. Therefore, AT-BOT are able to perform this mission.

Contents in this chapter will start with explanation of function of light reflection sensors

which will be applied as line sensors and followed by preparation of field for test, examples

of sensor calibration to work efficiently. After this, entering to experimentls of programming

with C/C++ language to seek lines, move within the defined areas and move along the

lines which are not considered the junction and considered the junction.

Therefore, learners need efforts to understand and perform trials according to

sequences because the whole contents are related each and you will have to apply

some knowledge from the previous contents to integrate as well.

8.1 Properties of a light reflector : ZX-03RIt is a circuit board used to detect reflection of light. It consist of a super bright red

LED which functions as a light source and highly sensitive phototransistor which can detect

both visible and invisible light (normal light and Infrared light). The phototransistor will get

reflected l ight which is originated from the LED and reflected on the floor. The

phototransistor will give different results based on intensity of reflected ligh it gains. Figure

8-1 shows the working process of this sensor when it is applied on white and black floor.

When the supply voltage is applied, the super-bright red LED will be active. The red

light is emitted all the time. In part of the receptor or the phototransistor will obtain the red

light from the reflection. The phototransistor will get more or less quantity of light depending

on whether any object obstacles or not and the ability to reflect red light on that object,

which is based on the feature of floor and color of the objects. Smooth white objects can

reflect light well, so the light receptor or the phototransistor will obtain a lot of reflected

light. As a result the output voltage of the circuit is high accordingly. Meanwhile, black

objects reflex less light so the light receptor works less and sents out low voltage. With

those features, the circuit boards of the reflected light sensors are installed at the bottom

of a robot’s structure to detect surface and lines.

Chapter 8

AT-BOT Line tracking

Page 106: AT-BOT_E120703

106Robo-Creator : AT-BOT activity book

Super-brightred LED

Photo-transistor

White surface

10k

SFH310

510

+V

GND

OUT

High output voltage

10k510

+V

GND

OUT

curr

ent

LED

SFH310

LED

(A) Operation of sensor with the white surface

Super-brightred LED

Photo-transistor

Black surface

Low output voltagecurr

ent

(B) Operation of sensor with the black surface

Figure 8-1 : Operation of ZX-03R; the reflected light sensors when

used to detect a black line and white surface

Page 107: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 107

8.2 Line tracking activity preparation

8.2.1 Material preparation for making the demonstration field

In the program development to instruct a robot moving along lines by using the

light reflection sensor, first thing to do is analyze lines and surface in order to use the data

to set conditions and then compare with a reference value from the test of reading of the

state of light reflected on lines and field floor which is actually used. The test of reading is

based on the differences of light reflection at each surface with different color. White

surface is able to reflex light well but dark surface can do less because black color has low

ability of light absorption. Learning in this chapter choose the demonstration field which

have white surface and black line. Developers have to make this field first.

The demonstration filed of this handbook will be made from Polypropylene board

or PP board and black tape. Important materials and tools compose of:

1. White Polypropylene or PP board. Size is 90 x 60cm. However the sizing can

change depending on your applications and resoucres.

2. Black electrical tape 1 inches width 2 rolls. It is recommended to buy 3M brand

because this brand has high elastic and it can stick curve well.

3. Scissors or a Cutter

Page 108: AT-BOT_E120703

108Robo-Creator : AT-BOT activity book

8.2.2 Installation the light reflector sensors (ZX-03R) for AT-BOT

The purpose of application of the ZX-03R light reflector sensors in AT-BOT is to detect

surface and lines so the sensors will be installed under a robot body. There are steps

mentioned as follows:

(1) Attach 3 of ZX-03R sensors with 12-hole Strip joiners by 3 x 10mm. screws and

3mm. nuts. There are 3mm. plastic standoffs separated out between the sensor boards

and the Strip joiners.

3 x 40mm. screw

3 x 40mm.screw

3 x 15mm.screw

ZX-03R

3mm. plastic standoff

3mm. plastic standoff

3mm. plastic standoff

ZX-03R

ZX-03R

12-hole Strip joiner

25mm.standoff

3mm. nut

3mm. nut

3mm. nut25mm.standoff

Robot chasis

may be cut if it touch thefront wheel

Right ZX-03R is connectedto ADC2 port

Middle ZX-03R isconnected to ADC1 port

Left ZX-03R is connectedto ADC0 port

25mm. plastic standoff

25mm. plastic standoff

Top view

Page 109: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 109

(2) Turn off power of AT-BOT and then attach the set of the ZX-03R structure from

step (1) at the front of the robot chasis by using 2 sets of 25mm. standoffs to cushion and

fasten them with 3 x 40mm. screws and 3mm. nuts 2 sets. As the result, the ZX-03R boards

are far from the floor about 5mm. Then, plug the cables of the left, middle and right ZX-03R

to ADC0, ADC1 and ADC2 port of AT-BOT respectively.

Finally, the AT-BOT is ready for the mission to detect surface and lines.

The objective of this style of installation sensors is to check surface and lines. The

sensors can detect both white and black lines including different colors such as purple

and yellow, green and red, etc.

8.2.3 Determination of the reference value to separatedifferences between lines and surface

To define the reference value for using in comparison in order to let a robot know

that a sensor has found lines or surface, generally it is based on a programming of reading

a value from each light reflection sensor to display. Normally, both values have to be

quite different.

Programming C/C++ with Wiring to read values from a ZX-03R light reflection sensor

of the AT-BOT will use the analog function. It shows result values 0 to 1023.

Page 110: AT-BOT_E120703

110Robo-Creator : AT-BOT activity book

Super-brightred LED

Photo-transistor

White surface

Photo-transistor

Black line

Super-brightred LED

(A) The white surface reading of the ZX-03R sensor

(B) The black line reading of the ZX-03R sensor

Value fromanalog function

400 to 900

Value fromanalog function

10 to 300

If the ZX-03R is at the area of black lines, a value will be low and tends towards the

value 0.

If the sensor is at the area of a white surface, a value will be high and tends towards

the value 1023.

The calculation of the reference value used to identify white surface and black

lines will be as follows

Reference value = (value from the white surface + value from black lines)/2

For example, if the white surface values is 950 and black lines is 250, so the reference

value will be (950+250)/2 = 600

However, in a practical application user may configure to be wider than these but

should be in the range of 250 to 950 but should not choose values to be close to 250 and

950 too much.

Page 111: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 111

Experiment 14

Determine the reference value for line detection

This experiment is a test to find reference values for detecting lines of AT-BOT with 3

of ZX-03R sensors. They are installed in the front and under the structure base of the AT-BOT.

The left sensor is connected with ADC0, the middle one connected with ADC1 and the

right one connected with ADC2. The result values will be processed and shown at the LCD

module of the AT-BOT robot.

The experiment will present and make the understanding about how to find the

reference value for distinguish between lines and surface. It is important knowledge for

making the line tracking robot.

Make the simple field

The test field in this experiment has a white surface and constructed by sticking of

black tapes as a quadrangle with curve angles according to the figure below:

The size of this field can be modified as appropriate. In this book, choose a white

PP board with the size of 90x60cm. stuck with black tapes to create the curve border in

the size of 70x40cm.

Page 112: AT-BOT_E120703

112Robo-Creator : AT-BOT activity book

Hardware connection

Connect ADC0/40 port of AT-BOT with the left front ZX-03R sensor

Connect ADC1/41 port of AT-BOT with the middle front ZX-03R sensor

Connect ADC2/42 port of AT-BOT with the right front ZX-03R sensor

ON

MO

TO

R

BA

TTERY LE

VEL

13

12

11

10

98

SERV

O P

ORT

7.2-9V BATT.

E2

RESET

+

- S

14

15

PC6

PC7

Cre

con

atore>

>>>

trollr

boa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KN

OB

TW

IU

ART1

0SCL

1SD

A2

RX1

3TX

1

44

AD

C4

46

ADC

640

AD

C0

41

AD

C1

42

AD

C2

START

45

AD

C5

>>

USB

DAT

A

43

AD

C3

3210 4 5

ZX-03R sensors areattached at bottomof the robot chasis

#include <atxt.h> // Include the main library

void setup()

{}

void loop()

{

lcd("L %d M%d #nR %d ",analog(0),analog(1),analog(2));

// Read all sensor value from ADC0, ADC1 and ADC2 port

// to display on the AT-BOT display

}

Listing L14-1 : reflect_test.pde, the sketch file for reading sensor from

ADC0, ADC1 and ADC2 input to display at the AT-BOT

Page 113: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 113

Procedure

L14.1 Create the new sketch file. Type the Listing L10-1 and save as reflect_test.pde file.

L14.2 Compile and upload the sketch to the AT-BOT. Unplug the USB cable.

L14.3 Run the program.

AT-BOT display shows the title message :

L xxx M yyy

R zzz

therefore xxx ,yyy and zzz are the digital data from reading the sensor at left, middle

and right position of AT-BOT

L14.4 Place the robot on the white surface. Read and record the reading values which

are displayed on the AT-BOT .

If all 3 sets of the sensing boards read similar values, you will average all values. The

value is about 900 in this step from the test.

ON

MO

TO

R

BA

TTERY LE

VEL

13

12

11

10

98

SER

VO

PO

RT

7.2-9V BATT.

E2

RES

ET

+

- S

14

15

PC6

PC7

Cre

con

atore>

>>>

trollr

boa

>

erd

R

>

48

50

SW

249

SW

1AD

C7

KN

OB

TW

IU

ART1

0SCL

1SD

A2

RX

13

TX1

44

AD

C4

46

AD

C6

40

AD

C0

41

AD

C1

42

AD

C2

START

45

AD

C5

>>

USB

DAT

A

43

AD

C3

3210 4 5

left(ADC0)

mid(ADC1)

right(ADC2)

ZX-03R result data

Page 114: AT-BOT_E120703

114Robo-Creator : AT-BOT activity book

L14.5 Place the robot above the black line and then read and record a measured value

which is shown on the LCD module.

All 3 sets of the sensing boards should read similar values. At this step in this test,

the average is about 100.

ON

MO

TO

R

BATTERY LE

VEL

1312

11

10

98

SERVO

PO

RT

7.2-9V BATT.

E2

RESET

+

- S

14

15PC6

PC7

Cre

con

atore>>>>

troll

rboa

>

erd

R

>

48

50SW

249

SW

1A

DC7

KNO

B

TW

IU

ART1

0SCL

1SD

A2

RX1

3TX1

44

AD

C4

46

AD

C6

40

AD

C0

41

AD

C1

42ADC2

START

45AD

C5

>>

USB

DA

TA

43

AD

C3

3210 4 5

ZX-03R result data

left(ADC0)

mid(ADC1)

right(ADC2)

L14.6 Calculate a reference value as follows:

Reference value = ( Value from the white surface + Value from the black line)/2

= (900+100) / 2 = 500

However, in actual application, possible to define wider values than these but

they should be in the range of 100 to 900 and they should not to be close to 100 and 900

too much. From now on, examples will be based on the reference value 500.

Page 115: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 115

Experiment 15

Frame detection robot

This experiment demonstrates about controlling AT-BOT robots to move within a

rectangular frame with curve corners surrounded with black lines. This experiment use

only 2 of ZX-03R sensors that is left and right front position.

Use only 2 of ZX-03R at left andright position to detect the blackline for controlling the robotmove within the frame

Procedure

L15.1 Create the new sketch file. Type the Listing L15-1 and save as robo_inner.pde file.

L15.2 Compile and upload the sketch to the AT-BOT. Unplug the USB cable.

L15.3 Place the AT-BOT within the frame of the test field that making in Expeirment 14.

L15.4 Run the program.

AT-BOT display shows the title message :

Press SW1

L15.5 Press the SW1 button on the AT-BOT. Observe the robot operation.

AT-BOT will move within the black frame. If the sensor detect the black line, robot

will move backward and change direction similar the object avoider robot activity in

chapter 7 but changing from inspection of bumping to inspection of black lines and white

surface instead.

Page 116: AT-BOT_E120703

116Robo-Creator : AT-BOT activity book

#include <atx.h> // Include the main library#define POW 80 // Set the motor power to 60%#define REF 500 // Set the reference value as 500unsigned int left,right; // Declare the ZX-03R sensor variablevoid forward(unsigned int time) // Moving forward function{ motor(0,POW); // Drive the Motor-0 forward motor(1,POW); // Drive the Motor-1 forward motor(2,POW); // Drive the Motor-2 forward motor(3,POW); // Drive the Motor-3 forward sleep(time); // Delay for moving forward}void backward(unsigned int time){ motor(0,-POW); // Drive the Motor-0 backward motor(1,-POW); // Drive the Motor-1 backward motor(2,-POW); // Drive the Motor-2 backward motor(3,-POW); // Drive the Motor-3 backward sleep(time); // Delay for moving backward}void turn_left(unsigned int time){ motor(0,-POW); // Drive the Motor-0 backward motor(1,-POW); // Drive the Motor-1 backward motor(2,POW); // Drive the Motor-2 forward motor(3,POW); // Drive the Motor-3 forward sleep(time); // Delay for turning left}void turn_right(unsigned int time){ motor(0,POW); // Drive the Motor-0 forward motor(1,POW); // Drive the Motor-1 forward motor(2,-POW); // Drive the Motor-2 backward motor(3,-POW); // Drive the Motor-3 backward sleep(time); // Delay for turning right}void setup(){ lcd("SW1 Press!"); // Display title message for asking to press the SW1 sw1_press(); // Wait until the SW1 is pressed}void loop(){

left = analog(0); // Read the left sensor data// to store to the variable left

right = analog(2); // Read the left sensor data// to store to the variable right

if(left>REF && right>REF) // Verify both sensor still not// detect the black line

{forward(1); // No line detect,

// move forward for short time}else if(left<REF && right>REF) // Only the left sensor detect line ?{

Listing L15-1 : robo_inner.pde, the sketch file for demonstration the

AT-BOT move within the black frame by using two line sensors (cont.)

Page 117: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 117

backward(500); // If correct, move backward 0.5 secondturn_right(800); // Spin right 0.8 second to change direction

}else if(left>REF && right<REF) // Only the right sensor detect line ?{

backward(500); // If correct, move backward 0.5 secondturn_left(800); // Spin left 0.8 second to change direction

}else if(left<REF && right<REF) // Both sensors detect the line ?{

backward(500); // If detect, move backward 0.5 secondturn_left(1500); // Spin left 1.5 second to change direction

}}

Code explanation

When the program runs, at the LCD module there is message Press SW1 to wait for

pressing the SW1 button. After SW1 is pressed, CPU operate within the loop function to repeat

reading values from the ZX-03R sensors at ADC0 (left) and ADC2 (right) input of AT-BOT to store

at the variable left and right respectively. The values will be compared with the reference value

later. Therefore, the robot would have found the white surface or black lines.

If any ZX-03R sensor has the result value greater than REF (reference value equal 500),

the robot will decide that it has found the white surface. On the other hand, if the result value is

less than REF, the robot will consider that it has detected black lines already.

Then, the program will compare values of both variables based on 4 cases as follows:

1. if(left>REF && right>REF) : it is verification that the sensors have not detected

any line, true or not. If it is true (not found any strip), this will react to the robot to move

forward.

2. else if(left<REF && right>REF) : it is checking that the left sensor has found

only one line, true or not. If it is true (found a line or a black frame), this will respond to the

robot to back and then turn right in order to change the direction of motion.

3. else if(left>REF && right<REF) : it is examination that the right sensor has

detected only one line, true or not. If it is true (detected a line or black frame), it will affect the

robot to move backwards and then turn left to alter the direction of movement.

4. else if(left<REF && right>REF) : it is checkup that both side sensors have

found lines, true or not. It is true (discovered a line or black frame), will take action to the robot

moving back and turn left in order to change the direction of locomotion.

Note: REF value for comparing lines and surface in this experiment is 500, which comes from

the experiment 14. Developers may have different reference values depending on other external

factors, such as the amount of illumination from the sun around a test field, incandescent light

bulbs, or other light sources, including distance from sensors to the field floor.

Listing L15-1 : robo_inner.pde, the sketch file for demonstration the

AT-BOT move within the black frame by using two line sensors (final)

Page 118: AT-BOT_E120703

118Robo-Creator : AT-BOT activity book

Experiment 16

AT-BOT with the Zigzag movement

This is an additional example of searching and detecting lines of the AT-BOT by

specifying that there are 2 parallel black lines. Then, the robot will be released to move

along the direction that is an angle about 45 degrees with a black line and the robot will

move straight constantly until it has detected a line and then turns. AT-BOT continues moving

forward till it will found a line again, so it will turn to another direction again. Normally, it

works like this so on. Therefore, the robot has a route of zigzag motion between both black

lines as the following figure.

at

lea

st

30

cm

.

During the zigzag movement in this experiment, use only 2 of ZX-03R sensors in the

left and right front like the experimet 15.

Make the demonstration field

Using a PP board with the size of 90x60 cm. from the experiment 14 (or possible

other sizes as required but should be large enough to let a robot be able to move

conveniently) to be a field of this experiment and stick 2 parallel lines of black tapes with

the distance at least 30 centimeters in order to make the robot have enough area in

locomotion.

Procedure

L16.1 Create the new sketch file. Type the Listing L15-1 and save as robo_pingponf.pde

L16.2 Compile and upload the sketch to the AT-BOT. Unplug the USB cable.

L16.3 Place the robot at the start point at an angle about 45 degrees with a black line.

Page 119: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 119

#include <atx.h> // Include the main library#define POW 80 // Set the motor power to 60%#define REF 500 // Set the reference value as 500unsigned int left,right; // Declare the ZX-03R sensor variablevoid forward(unsigned int time) // Moving forward function{ motor(0,POW); // Drive the Motor-0 forward motor(1,POW); // Drive the Motor-1 forward motor(2,POW); // Drive the Motor-2 forward motor(3,POW); // Drive the Motor-3 forward sleep(time); // Delay for moving forward}void backward(unsigned int time){ motor(0,-POW); // Drive the Motor-0 backward motor(1,-POW); // Drive the Motor-1 backward motor(2,-POW); // Drive the Motor-2 backward motor(3,-POW); // Drive the Motor-3 backward sleep(time); // Delay for moving backward}void turn_left(unsigned int time){ motor(0,-POW); // Drive the Motor-0 backward motor(1,-POW); // Drive the Motor-1 backward motor(2,POW); // Drive the Motor-2 forward motor(3,POW); // Drive the Motor-3 forward sleep(time); // Delay for turning left}void turn_right(unsigned int time){ motor(0,POW); // Drive the Motor-0 forward motor(1,POW); // Drive the Motor-1 forward motor(2,-POW); // Drive the Motor-2 backward motor(3,-POW); // Drive the Motor-3 backward sleep(time); // Delay for turning right}void setup(){ lcd("SW1 Press!"); // Display title message for asking to press the SW1 sw1_press(); // Wait until the SW1 is pressed}void loop(){

left = analog(0); // Read the left sensor data// to store to the variable left

right = analog(2); // Read the left sensor data// to store to the variable right

if(left>REF && right>REF) // Verify both sensor still not// detect the black line

{forward(1); // No line detect,

// move forward for short time}

Listing L16-1 : robo_pingpong.pde, the sketch file for demonstration

the AT-BOT move zigzag or similar pingpong movement by using two

line sensors (continue)

Page 120: AT-BOT_E120703

120Robo-Creator : AT-BOT activity book

else if(left<REF && right>REF) // Only the left sensor detect line ?{

turn_right(400); // If correct, spin right 0.4 second// to change the direction

}else if(left>REF && right<REF) // Only the left sensor detect line ?{

turn_left(400); // If correct, spin left 0.4 second// to change the direction

}else if(left<REF && right<REF) // Both sensors detect the line ?{

backward(1); // If detect, move backward for short time}

}Code explanation

Starting the program with show the message to ask pressing the SW1 button at the LCD

of AT-BOT. After SW1 is pressed, CPU will start working in the loop function to repeat reading

values of the ZX-03R sensors from ADC0 (left) and ADC1 (right) to store at the variable left and

right respectively. Verify and check which sensor can detect black lines similar the previois

experiment. Then, compare both variable values as follows:

1. if(left>REF && right>REF) : it is verification that both side sensors have not

found any line, true or not. If it is true (not found any strip), this will react to the robot to move

forward continuously.

2. else if(left<REF && right>REF) : it is checking that the left sensor has found

only one line, true or not. In this case, it is interpreted that the robot has found the upper border

line. The program will define the robot to turn right to avoid the line. Program developers can

modify a delay time value in turning to have a turning angle as required.

3. else if(left>REF && right<REF) : it is examination that the right sensor has

detected only one line, true or not. If it is true, in this case will interpret that the robot has found

the lower border line. Then, the program will control the robot to turn left to be out of the line.

4. else if(left<REF && right>REF) : it is checkup that the line sensors in both

sides have found lines, true or not. If it is true (discovered a line), will react to the robot moving

back for a short time to be away from the line.

Moreover, developers may add commands to instruct robots turning around and turn with

other different angles in order to create the type of complete zigzag movement, may involving

adjusting the position and the distance of the line sensors from the floor and mechanical

adjustments as necessary to make the motion of the robot follow the requirement.

Listing L16-1 : robo_pingpong.pde, the sketch file for demonstration

the AT-BOT move zigzag or similar pingpong movement by using two

line sensors (final)

Page 121: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 121

L16.4 Run the program.

AT-BOT display shows the title message :

Press SW1

L16.5 Press the SW1 button on the AT-BOT. Observe the robot operation.

The AT-BOT will move straight constantly until it has found a line, it will turn with an

angle about 90 degrees. Then, it will go ahead again till it finds a line of another side, so it

will turn and walk forward to turn back to another side in a zigzag motion. Normally, the

robot will move between black lines of both sides and program developers have to observe

whether the movement of the robot and the angle of turning to change a direction are

suitable or not. If it is not complete enough, program developers can adjust appropriate

delay time value for turning in the program until the function of the robot is satisfied.

Page 122: AT-BOT_E120703

122Robo-Creator : AT-BOT activity book

Experiment 17

Line tracking robot using 2 sensors

After learning and testing the line detection from the experiment 15 and 16, In this

experiment presents the line tracking robot activity by using 2 of ZX-03R sensors. The

demonstration field of this experiment is shown in the figure L17-1. The conditions to act the

mission as follows:

1. Robot moves along the black line.

2. When detect the junction, robot must stop for 3 seconds .

Figure L17-1 : Illustration of the demonstration field of the experiment

17. It is made from the PP board and black tapes are stuck on it as

the pattern from above

Page 123: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 123

Basic principle of the line tracking robot using 2 of sensors

The main purpose of a line tracking robot is to control the robot to move along the

line while 2 sensors are located astride the line. Therefore, there will be 4 cases of events,

which are used to specify the conditions of functioning as follows:

Case 1 : The robot is in the field lines or bestride the lines

Using 2 of ZX-03R sensorsat the left and right front side to control

the robot to move along the line.

Left sensor position : located on the white surface

(sensor’s value > reference value)

Right sensor position : located on the white surface

(sensor’s value > reference value)

Operation : Robot moves straight and delay the time of the

motion for a short time.

Page 124: AT-BOT_E120703

124Robo-Creator : AT-BOT activity book

Case 2 : The robot skips a line to the right

ZX-03R result data

left(ADC0)

right(ADC2)

Left sensor position : located on the black line

(sensor’s value < reference value)

Right sensor position : located on the white surface

(sensor’s value > reference value)

Operation : Robot turn left with a short time to bestride the

black line again .

Case 3 : The robot tilts from the route to the left

ZX-03R result data

left(ADC0)

right(ADC2)

Left sensor position : located on the white surface

(sensor’s value > reference value)

Right sensor position : located on the black line

(sensor’s value < reference value)

Operation : Robot turn right with a short time to bestride the

black line again .

Page 125: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 125

Case 4 : The robot has found the crossline or may be junction

ZX-03R result data

left(ADC0)

right(ADC2)

Left sensor position : located on the black line

(sensor’s value < reference value)

Right sensor position : located on the black line

(sensor’s value < reference value)

Operation : choose the robot to move forward, turn left,

turn right, stop or backward as needed.

It is based on the goal of the mission.

Make the demonstration field

Using a PP board with the size of 90x60 cm. from the experiment 14 (or possible

other sizes as required but should be large enough to let a robot be able to move

conveniently) to be a field of this experiment and stick the black tapes following the

figure L17-1

Procedure

L17.1 Create the new sketch file. Type the Listing L15-1 and save as robo_line1.pde

L17.2 Compile and upload the sketch to the AT-BOT. Unplug the USB cable.

L17.3 Place the robot bestride the black line of the field.

L17.4 Run the program.

AT-BOT display shows the title message :

Press SW1

Page 126: AT-BOT_E120703

126Robo-Creator : AT-BOT activity book

#include <atx.h> // Include the main library#define POW 80 // Set the motor power to 60%#define REF 500 // Set the reference value as 500unsigned int left,right; // Declare the ZX-03R sensor variablevoid forward(unsigned int time) // Moving forward function{ motor(0,POW); // Drive the Motor-0 forward motor(1,POW); // Drive the Motor-1 forward motor(2,POW); // Drive the Motor-2 forward motor(3,POW); // Drive the Motor-3 forward sleep(time); // Delay for moving forward}void backward(unsigned int time){ motor(0,-POW); // Drive the Motor-0 backward motor(1,-POW); // Drive the Motor-1 backward motor(2,-POW); // Drive the Motor-2 backward motor(3,-POW); // Drive the Motor-3 backward sleep(time); // Delay for moving backward}void turn_left(unsigned int time){ motor(0,-POW); // Drive the Motor-0 backward motor(1,-POW); // Drive the Motor-1 backward motor(2,POW); // Drive the Motor-2 forward motor(3,POW); // Drive the Motor-3 forward sleep(time); // Delay for turning left}void turn_right(unsigned int time){ motor(0,POW); // Drive the Motor-0 forward motor(1,POW); // Drive the Motor-1 forward motor(2,-POW); // Drive the Motor-2 backward motor(3,-POW); // Drive the Motor-3 backward sleep(time); // Delay for turning right}void setup(){ lcd("SW1 Press!"); // Display title message

// for asking to press the SW1 sw1_press(); // Wait until the SW1 is pressed}void loop(){

left = analog(0); // Read the left sensor data// to store to the variable left

right = analog(2); // Read the left sensor data// to store to the variable right

if(left>REF && right>REF) // Verify both sensor still not// detect the black line

{forward(1); // If no detect the line, robot moves forward

}

Listing L17-1 : robo_line1.pde, the sketch file for demonstration the

AT-BOT moves along the black line by using 2 line sensors (continue)

Page 127: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 127

else if(left<REF && right>REF) // Only the left sensor detect line ?{

turn_left(10); // Spin left 0.01 second// to move to bestride the line again

}else if(left>REF && right<REF) // Only the right sensor detect line ?{

turn_right(10); // Spin right 0.01 second// to move to bestride the line again

}else if(left<REF && right<REF) // Both sensors detect the line ?{

pause(); // If detect, robot found the crosslinesleep(3000); // Stop movement 3 secondsforward(300); // Move forward 0.3 second

// to over the crossline}

}

L16.5 Press the SW1 button on the AT-BOT. Observe the robot operation.

Robot moves along the black line and stop for 3 seconds when detect the junction.

After that moves along line continually.

Listing L17-1 : robo_line1.pde, the sketch file for demonstration the

AT-BOT moves along the black line by using 2 line sensors (final)

Page 128: AT-BOT_E120703

128Robo-Creator : AT-BOT activity book

Experiment 18

Line tracking robot using 3 sensors

This experiment is continuing of the experiment 17 by adding a ZX-03 sensor to apply

as the third line sensor. It will be installed at the middle position between the left and the

right sensor (In the experiment 17, if you did not take out the middle sensor at the bottom of

robot chasis, you will use it in this study). This is to help the AT-BOT able to detect the crossline

or junction better. For the test field, still use the same layout as the experiment 17 as well as

the hardware connection. The additional sensor is to connect with ADC1 port of AT-BOT.

The functioning conditions of AT-BOT robots in this study include:

1. A robot must move along the black line.

2. When a robot has discovered the crossline, it needs to stop and wait at the

junction for 3 seconds. And then, it will move on.

Principles of the line tracking robot by using 3 line sensors

There are 6 situations will probably happen in using 3 line sensors. User can apply

them on the determination of the functioning conditions as follows:

Case 1 : A robot is in the lines or bestride a line

ZX-03R result data

left(ADC0)

right(ADC2)

mid(ADC1)

Left sensor position : located on the white surface (value > 500)

Middle sensor position : located on the black line (value < 300)

Right sensor position : located on the white surface (value > 500)

Operation : Robot moves straight and delay the time of the

motion for a short time.

Page 129: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 129

Case 2 : A robot goes out of a line to the right

ZX-03R result data

right(ADC2)

mid(ADC1)

left(ADC0)

Left sensor position : located on the black line (value < 300)

Middle sensor position : located on the white surface (value > 500)

Right sensor position : located on the white surface (value > 500)

Operation : Robot turn left with a short time to bestride the

black line again .

Case 3 : A robot tilts from a route to the left

ZX-03R result data

left(ADC0)

mid(ADC1)

right(ADC2)

Left sensor position : located on tthe white surface (value > 500)

Middle sensor position : located on the white surface (value > 500)

Right sensor position : located on the black line (value < 300)

Operation : Robot turn right with a short time to bestride the

black line again .

Page 130: AT-BOT_E120703

130Robo-Creator : AT-BOT activity book

Case 4 : A robot has found junction on the left

ZX-03R result data

right(ADC2)

mid(ADC1)

left(ADC0)

Left sensor position : located on the black line (value < 300)

Middle sensor position : located on the black line (value < 300)

Right sensor position : located on the white surface (value > 500)

Operation : choose the robot to move forward, turn left,

turn right, stop or backward as needed.

It is based on the goal of the mission.

Case 5 : A robot has found junction on the right

ZX-03R result data

left(ADC0)

mid(ADC1)

right(ADC2)

Left sensor position : located on the white surface (value > 500)

Middle sensor position : located on the black line (value < 300)

Right sensor position : located on the black line (value < 300)

Operation : choose the robot to move forward, turn left,

turn right, stop or backward as needed.

It is based on the goal of the mission.

Page 131: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 131

Case 6 : A robot has found a junction, which is a crossline

ZX-03R result data

mid(ADC1)

right(ADC2)

left(ADC0)

Left sensor position : located on the black line (value < 300)

Middle sensor position : located on the black line (value < 300)

Right sensor position : located on the black line (value < 300)

Operation : choose the robot to move forward, turn left,

turn right, stop or backward as needed.

It is based on the goal of the mission.

Procedure

L18.1 Create the new sketch file. Type the Listing L18-1 and save as robo_line2.pde

L18.2 Compile and upload the sketch to the AT-BOT. Unplug the USB cable.

L18.3 Place the robot bestride the black line of the field.

L18.4 Run the program.

AT-BOT display shows the title message :

Press SW1

L18.5 Press the SW1 button on the AT-BOT. Observe the robot operation.

Robot moves along the black line and stop for 3 seconds when detect the junction.

After that moves along line continually.

Page 132: AT-BOT_E120703

132Robo-Creator : AT-BOT activity book

#include <atx.h> // Include the main library#define POW 80 // Set the motor power to 60%#define REF 500 // Set the reference value as 500unsigned int left,middle,right; // Declare the ZX-03R sensor variablevoid forward(unsigned int time) // Moving forward function{ motor(0,POW); // Drive the Motor-0 forward motor(1,POW); // Drive the Motor-1 forward motor(2,POW); // Drive the Motor-2 forward motor(3,POW); // Drive the Motor-3 forward sleep(time); // Delay for moving forward}void backward(unsigned int time){ motor(0,-POW); // Drive the Motor-0 backward motor(1,-POW); // Drive the Motor-1 backward motor(2,-POW); // Drive the Motor-2 backward motor(3,-POW); // Drive the Motor-3 backward sleep(time); // Delay for moving backward}void turn_left(unsigned int time){ motor(0,-POW); // Drive the Motor-0 backward motor(1,-POW); // Drive the Motor-1 backward motor(2,POW); // Drive the Motor-2 forward motor(3,POW); // Drive the Motor-3 forward sleep(time); // Delay for turning left}void turn_right(unsigned int time){ motor(0,POW); // Drive the Motor-0 forward motor(1,POW); // Drive the Motor-1 forward motor(2,-POW); // Drive the Motor-2 backward motor(3,-POW); // Drive the Motor-3 backward sleep(time); // Delay for turning right}void setup(){ lcd("SW1 Press!"); // Display title message

// for asking to press the SW1 sw1_press(); // Wait until the SW1 is pressed}void loop(){

left = analog(0); // Read the left sensor data// to store to the variable left

mid = analog(1); // Read the middle sensor data// to store to the variable mid

right = analog(2); // Read the left sensor data// to store to the variable right

if(left>REF && mid<REF && right>REF)// Check the robot bestride the line or not ?

{forward(1); // If correct, robot moves forward with a short time

}

Listing L18-1 : robo_line2.pde, the sketch file for demonstration the

AT-BOT moves along the black line by using 3 line sensors (continue)

Page 133: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 133

else if(left<REF && mid>REF && right>REF)// Check the robot to move rightward away of the line

{turn_left(10);

// Spin left 0.01 second for trying to bestride the line again}else if(left>REF && mid>REF && right<REF)

// Check the robot to move leftward away of the line {

turn_right(10);// Spin right 0.01 second for trying to bestride the line again

}else if(left<REF && mid<REF && right<REF)

// Check the robot to found the crossline{

pause(); // If found, stop movementsleep(3000); // Delay 3 secondsforward(300); // Move forward 3 seconds to over the crossline

}else // For another conditions{

forward(1); // Move forward with a short time}

}

Code explanation

There are 4 conditions of the sensor operation to control the AT-BOT movement.

1. if(left>REF && mid<REF && right>REF) : it is checking about the robot still

bestride the line or not. If true, robot will move forward with a short time

2. else if(left<REF && mid>REF && right>REF) : it is checking about the robot

moves rightward away of the line or not. If true, robot is controlled to spin left with a short time

to back to bestride the line again.

3. else if(left>REF && mid>REF && right<REF) : it is checking about the robot

moves leftward away of the line or not. If true, robot is controlled to spin right with a short time

to back to bestride the line again.

4. else if(left<REF && mid<REF && right<REF) : it is checking about the robot

found the crossline or not. If true, it will stop at the junction for 3 seconds after that moves

forward 0.3 second to over the crossline.

Listing L18-1 : robo_line2.pde, the sketch file for demonstration the

AT-BOT moves along the black line by using 3 line sensors (final)

Page 134: AT-BOT_E120703

134Robo-Creator : AT-BOT activity book

Additional conclusion

From the experiment 17 and 18, the robot moves along the line with same concept

but their difference is the number of line sensors because the test field is not much

complexity so the application of only 2 line sensors can accommodate. However, in case

that a test field is more complex such as a left junction and a right junction available.

Using 3 line sensors will be more efficient than only 2 sensors. Consideration of comparable

cases will be shown as follows:

(1) The left junction case

(1.1) Using 2 of line sensors

ZX-03R result data

left(ADC0)

right(ADC2)

ZX-03R result data

left(ADC0)

right(ADC2)

The result is only the left sensor detects the black line. It is inconclusive. Now,

the robot is moving out of the line to the right or straight to the left junction.

(1.2) Using 3 of line sensors

ZX-03R result data

right(ADC2)

mid(ADC1)

left(ADC0)

With this techniques, the left and middle sensors will detect lines.

The conclusion is clear that now the robot is moving straight to the left junction.

Page 135: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 135

(2) The right junction case

(2.1) Using 2 of line sensors

ZX-03R result data

left(ADC0)

right(ADC2)

ZX-03R result data

left(ADC0)

right(ADC2)

The result is only the right sensor detects the black line. It is inconclusive.

Now, the robot is moving out of the line to the left or straight to the right junction.

(1.2) Using 3 of line sensors

With this techniques, the right and middle sensors will detect lines.

The conclusion is clear that now the robot is moving straight to the right junction.

Page 136: AT-BOT_E120703

136Robo-Creator : AT-BOT activity book

Experiment 19

Advance line tracking robot

This experiment expands on the experiment 18 by adding the complexity of the

test field and further conditions of movement along lines as shown in the figure L19-1. AT-

BOT robots have to use 3 line sensors in the operation of this mission. The conditions of the

functioning compose of:

1. The robot has to be released from the start point.

2. The robot has to move to pass the left and the right crossroads of the way to

the triple junction.

3. When the robot has arrived to the junction, it must turn left and move along

the line in clockwise direction.

Creating the test field

Using a PP board with the size of 120 x 60cm. or it is possible to use other sizes to build

the field but they should be large enough to support the convenient movement of a

robot and stick it with black tapes according to the figure L19-1.

About the robot

Use the AT-BOT that is installed 3 of line sensors from the experiment 17 and 18

Procedure

L19.1 Create the new sketch file. Type the Listing L18-1 and save as robo_line3.pde

L19.2 Compile and upload the sketch to the AT-BOT. Unplug the USB cable.

Figure L19-1 : The test field of the advance line tracking robot in the

experiment 19

Junction withcrossline

ST

AR

T

Page 137: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 137

#include <atx.h> // Include the main library#define POW 80 // Set the motor power to 60%#define REF 500 // Set the reference value as 500unsigned int left,middle,right; // Declare the ZX-03R sensor variablevoid forward(unsigned int time) // Moving forward function{ motor(0,POW); // Drive the Motor-0 forward motor(1,POW); // Drive the Motor-1 forward motor(2,POW); // Drive the Motor-2 forward motor(3,POW); // Drive the Motor-3 forward sleep(time); // Delay for moving forward}void backward(unsigned int time){ motor(0,-POW); // Drive the Motor-0 backward motor(1,-POW); // Drive the Motor-1 backward motor(2,-POW); // Drive the Motor-2 backward motor(3,-POW); // Drive the Motor-3 backward sleep(time); // Delay for moving backward}void turn_left(unsigned int time){ motor(0,-POW); // Drive the Motor-0 backward motor(1,-POW); // Drive the Motor-1 backward motor(2,POW); // Drive the Motor-2 forward motor(3,POW); // Drive the Motor-3 forward sleep(time); // Delay for turning left}void turn_right(unsigned int time){ motor(0,POW); // Drive the Motor-0 forward motor(1,POW); // Drive the Motor-1 forward motor(2,-POW); // Drive the Motor-2 backward motor(3,-POW); // Drive the Motor-3 backward sleep(time); // Delay for turning right}void setup(){ lcd("SW1 Press!"); // Display title message

// for asking to press the SW1 sw1_press(); // Wait until the SW1 is pressed}void loop(){

left = analog(0); // Read the left sensor data// to store to the variable left

mid = analog(1); // Read the middle sensor data// to store to the variable mid

right = analog(2); // Read the left sensor data// to store to the variable right

if(left>REF && mid<REF && right>REF)// Check the robot bestride the line or not ?

{forward(1); // If correct, robot moves forward with a short time

}else if(left<REF && mid>REF && right>REF)

// Check the robot to move rightward away of the line

Listing L19-1 : robo_line3.pde, the sketch file for demonstration the

AT-BOT moves along the black line by using 3 line sensors on the

complex route (continue)

Page 138: AT-BOT_E120703

138Robo-Creator : AT-BOT activity book

{turn_left(10); // Spin left 0.01 second for trying to bestride the line

}else if(left>REF && mid>REF && right<REF)

// Check the robot to move leftward away of the line{

turn_right(10); // Spin right 0.01 second for trying to bestride the line}

else if(left<REF && mid<REF && right>REF)// Check the robot to detect the left junction

{forward(10); // Move forward 0.01 second to pass the left junction

}else if(left>REF && mid<REF && right<REF)

// Check the robot to detect the right junction{

forward(10); // Move forward 0.01 second to pass the right junction}else if(left<REF && mid<REF && right<REF)

// Check the robot to detect the crossline{

forward(100); // Move forward 0.1 second to pass the crosslineturn_left(200); // Spin left 0.2 second for trying to bestride the line

// at the crossline again}

}

Code explanation

There are 6 conditions of the sensor operation to control the AT-BOT movement.

1. if(left>REF && mid<REF && right>REF) : it is checking about the robot still

bestride the line or not. If true, robot will move forward with a short time

2. else if(left<REF && mid>REF && right>REF) : it is checking about the robot

moves rightward away of the line or not. If true, robot is controlled to spin left with a short time

to back to bestride the line again.

3. else if(left>REF && mid>REF && right<REF) : it is checking about the robot

moves leftward away of the line or not. If true, robot is controlled to spin right with a short time

to back to bestride the line again.

4. else if(left<REF && mid<REF && right>REF) : it is checking about the robot

detect the left junction or not. If true, robot will move forward to pass this junction.

5. else if(left>REF && mid<REF && right<REF) it is checking about the robot

detect the right junction or not. If true, robot will move forward to pass this junction.

6. else if(left<REF && mid<REF && right<REF) : it is checking about the robot

found the crossline or not. If true, robot will move forward 0.1 second and turn left 0.3 second

after that back to check all line tracking conditions continually

Listing L19-1 : robo_line3.pde, the sketch file for demonstration the

AT-BOT moves along the black line by using 3 line sensors on the

complex route (final)

Page 139: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 139

L19.3 Place the robot bestride the black line of the field.

L19.4 Run the program.

AT-BOT display shows the title message :

Press SW1

L18.5 Press the SW1 button on the AT-BOT. Observe the robot operation.

The robot AT-BOT will move along the black line until the robot has found the

triple junction, it will turn left and move along the curve in the clockwise direction constantly.

Junction withcrossline

Start

More information

From the previous experiments, the test fields have black strips and white surface.

If you will change to black surface and white lines, programming to control functioning

will be able to use the principles of line and surface analysis of the experiment 14 and the

principles of driving robots from the experiment 15 to 18 as guidelines of program

development.

For example, if you would like to test that a robot has found any crossline or not.

In the case that the field surface is white and the strips are black, apply this

condition to check.

else if(left<REF && mid<REF && right<REF)

But if the field surface is black and the strips are white, use this condition to verify.

else if(left>REF && mid>REF && right>REF)

Because strips have become white, if read a value from a line sensor of each

position and it shows that the value is greater than the reference value. Hence, the line

sensor of that position has found a white line.

Page 140: AT-BOT_E120703

140Robo-Creator : AT-BOT activity book

Experiment 20

Line tracking robot with the white line mission

This experiment represents a guideline of the control program development for AT-

BOT to move along the white lines on a black surface. The appearance of the test field in

this experiment is opposite to that of the experiment 15 to 19, by which the test field in this

trial has the same feature with which of the experiment 18 but colors are changed that

black lines converts to white lines and the white surface change to black surface as

demonstrated in the figure L20-1.

The conditions of functioning include:

1. The robot must move along the white line.

2. When the robot detects the crossline, it will have to stop and wait for 3 seconds

approximately and then it will move on.

Making the test field

Using a black PP board in the size of 90x60 cm. (possible other sizes as you wish but

the size should be large enough that the robot can move around comfortably) as the

field surface and stick with white tape to create a curve frame and 2 points of the

intersections in accordance with the example in the figure L20-1.

Figure L20-1 : The test field for Line tracking robot in the experiment 20

Page 141: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 141

Principles of the line tracking robot with white line by using3 line sensors

There are 6 situations will probably happen in using 3 line sensors. User can apply

them on the determination of the functioning conditions as follows:

Case 1 : A robot is in the lines or bestride a line

ZX-03R result data

mid(ADC1)

left(ADC0)

right(ADC2)

left(ADC0)

Left sensor position : located on the black line (value < 300)

Middle sensor position : located on the white surface (value > 500)

Right sensor position : located on the black line (value < 300)

Operation : Robot moves straight and delay the time of the

motion for a short time.

Case 2 : A robot goes out of a line to the right

ZX-03R result data

left(ADC0)

right(ADC2)

mid(ADC1)

Left sensor position : located on the white surface (value > 500)

Middle sensor position : located on the black line (value < 300)

Right sensor position : located on the black line (value < 300)

Operation : Robot turn left with a short time to bestride the

black line again .

Page 142: AT-BOT_E120703

142Robo-Creator : AT-BOT activity book

Case 3 : A robot tilts from a route to the left

ZX-03R result data

right(ADC2)

mid(ADC1)

left(ADC0)

Left sensor position : located on tthe black line (value < 300)

Middle sensor position : located on the black line (value < 300)

Right sensor position : located on the white surface (value > 500)

Operation : Robot turn right with a short time to bestride the

black line again .

Case 4 : A robot has found junction on the left

ZX-03R result data

left(ADC0)

mid(ADC1)

right(ADC2)

Left sensor position : located on the white surface (value > 500)

Middle sensor position : located on the white surface (value > 500)

Right sensor position : located on the black line (value < 300)

Operation : choose the robot to move forward, turn left,

turn right, stop or backward as needed.

It is based on the goal of the mission.

Page 143: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 143

Case 5 : A robot has found junction on the right

ZX-03R result data

right(ADC2)

mid(ADC1)

left(ADC0)

Left sensor position : located on the black line (value < 300)

Middle sensor position : located on the white surface (value > 500)

Right sensor position : located on the white surface (value > 500)

Operation : choose the robot to move forward, turn left,

turn right, stop or backward as needed.

It is based on the goal of the mission.

Case 6 : A robot has found a crossline

ZX-03R result data

right(ADC2)

mid(ADC1)

left(ADC0)

Left sensor position : located on the black line (value < 300)

Middle sensor position : located on the black line (value < 300)

Right sensor position : located on the black line (value < 300)

Operation : choose the robot to move forward, turn left,

turn right, stop or backward as needed.

It is based on the goal of the mission.

Page 144: AT-BOT_E120703

144Robo-Creator : AT-BOT activity book

#include <atx.h> // Include the main library#define POW 80 // Set the motor power to 60%#define REF 500 // Set the reference value as 500unsigned int left,middle,right; // Declare the ZX-03R sensor variablevoid forward(unsigned int time) // Moving forward function{ motor(0,POW); // Drive the Motor-0 forward motor(1,POW); // Drive the Motor-1 forward motor(2,POW); // Drive the Motor-2 forward motor(3,POW); // Drive the Motor-3 forward sleep(time); // Delay for moving forward}void backward(unsigned int time){ motor(0,-POW); // Drive the Motor-0 backward motor(1,-POW); // Drive the Motor-1 backward motor(2,-POW); // Drive the Motor-2 backward motor(3,-POW); // Drive the Motor-3 backward sleep(time); // Delay for moving backward}void turn_left(unsigned int time){ motor(0,-POW); // Drive the Motor-0 backward motor(1,-POW); // Drive the Motor-1 backward motor(2,POW); // Drive the Motor-2 forward motor(3,POW); // Drive the Motor-3 forward sleep(time); // Delay for turning left}void turn_right(unsigned int time){ motor(0,POW); // Drive the Motor-0 forward motor(1,POW); // Drive the Motor-1 forward motor(2,-POW); // Drive the Motor-2 backward

Procedure

L20.1 Create the new sketch file. Type the Listing L20-1 and save as robo_line4.pde

L20.2 Compile and upload the sketch to the AT-BOT. Unplug the USB cable.

L20.3 Place the robot bestride the white line of the field.

L20.4 Run the program.

AT-BOT display shows the title message :

Press SW1

L20.5 Press the SW1 button on the AT-BOT. Observe the robot operation.

Robot moves along the white line and stop for 3 seconds when detect the junction.

After that moves along line continually.

Listing L20-1 : robo_line4.pde, the sketch file for demonstration the

AT-BOT moves along the white line by using 3 line sensors (continue)

Page 145: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 145

motor(3,-POW); // Drive the Motor-3 backward sleep(time); // Delay for turning right}void setup(){ lcd("SW1 Press!"); // Display title message

// for asking to press the SW1 sw1_press(); // Wait until the SW1 is pressed}void loop(){

left = analog(0); // Read the left sensor data// to store to the variable left

mid = analog(1); // Read the middle sensor data// to store to the variable mid

right = analog(2); // Read the left sensor data// to store to the variable right

if(left<REF && mid>REF && right<REF)// Check the robot bestride the line or not ?

{forward(1); // If correct, robot moves forward with a short time

}else if(left>REF && mid<REF && right<REF)

// Check the robot to move rightward away of the line{

turn_left(10); // Spin left 0.01 second for trying to bestride the line again}else if(left<REF && mid<REF && right>REF)

// Check the robot to move leftward away of the line{

turn_right(10); // Spin right 0.01 second for trying to bestride the line again}else if(left>REF && mid>REF && right>REF)

// Check the robot to found the crossline{

pause(); // If found, stop movementsleep(3000); // Delay 3 secondsforward(300); // Move forward 3 seconds to over the crossline

}}

Listing L20-1 : robo_line4.pde, the sketch file for demonstration the

AT-BOT moves along the white line by using 3 line sensors (final)

Page 146: AT-BOT_E120703

146Robo-Creator : AT-BOT activity book

Page 147: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 147

In this chapter, it will present the application of a sensing module and a distance

measurement with the infrared distance sensor or Infrared ranger GP2D120 with AT-BOT. .

As the result, the AT-BOTs have ability to detect objects without contact with objects.

Therefore, the robots are able to go straight or avoid more wisely.

9.1. The characteristics of the module GP2D120The GP2D120 is the infrared distance sensing module, which has three used pins

composed of the power supply pin (Vcc), the ground pin (GND), and the output voltage

pin (Vout). Reading voltage values from GP2D120 will have to wait until the preparation

phase is finished by which it takes about between 32.7 and 52.9 milliseconds (1 millisecond

equals 0.001 seconds). Hence, the reading of the voltage should wait until after the first

period.

The output voltage of GP2D120 at the distance 30cm. with the power supply +5V is

in the range from 0.25 to 0.55V and the median is 0.4V. Therefore, the transition period of

the output voltage at the distance 4cm. is 2.25V ± 0.3V.

Figure 9-1 : Body, Pin assignment and Characteristic graph of the

Infrared ranger GP2D120

Chapter 9

AT-BOT with touchless object avoiding

Infrared LED Infrared Receiver

GNDVout Vcc

GP2D120

4 8 12 16 20 24 28 3200

0.4

0.8

1.2

1.6

2.0

2.4

2.8

Output voltage (V)

Distance (cm)

1st measure 2nd measure

Not stable 1st output 2nd output n output

nmeasure

38.3±9.6 ms

5 ms

Measurement

Vout

Supply

Page 148: AT-BOT_E120703

148Robo-Creator : AT-BOT activity book

9.2 How to install the GP2D120 with AT-BOTFrom the chapter 7, AT-BOT can keep away from the obstacles by touching or

bumping. As this chapter, the contents will focus on the ability development of AT-BOT to

another level by using the GP2D120 module to help with the detection of the distance

from obstacles in order to let the robots capable of getting away from obstruction without

touching.

The installation of the GP2D120 can be done as follows:

(1) Attach a right-angle joiner with the front of the robot at the middle with screws 3

x 10mm. screws and 3mm. nuts.

(2) Fasten a 5-hole strip joiner on the right-angle joiner from the first step with 3 x

10mm. screws and 3mm. nuts at the 3rd hole (the middle) of the strip joiner.

(3) Join the GP2D120 with the strip joiner by 2 sets of 3 x 10mm. screws and 3mm.

nuts by which the screws will be strung through the holes of the wings used to fasten the

module and the notches at the end of the strip joiner.

ON

MO

TO

R

BATTERY L

EVEL

13

12

11

10

98

SERVO

PO

RT

7.2-9V BATT.

E2

RESET

+

- S

14

15

PC

6PC7

Cre

con

atore>>>>

trollr

boa

>

erd

R

>

48

50SW

249

SW

1AD

C7

KNO

B

TW

IU

ART1

0SCL

1SD

A2

RX1

3TX1

44

AD

C4

46

AD

C6

40

ADC

041

AD

C1

42

ADC2

START

45

ADC5

>>

USB

DAT

A

43

AD

C3

3210 4 5

GP2D120

ON

MO

TO

R

BATTERY L

EVEL

13

12

11

10

98

SERVO

PO

RT

7.2-9V BATT.

E2

RESET

+

- S

14

15

PC

6PC7

Cre

con

atore>>>>

trollr

boa

>

erd

R

>

48

50SW

249

SW

1AD

C7

KNO

B

TW

IU

ART1

0SCL

1SD

A2

RX1

3TX1

44

AD

C4

46

AD

C6

40

ADC

041

AD

C1

42

ADC2

START

45

ADC5

>>

USB

DAT

A

43

AD

C3

3210 4 5

GP2D120 Front of theAT-BOT

GP2D120 module

connect to ADC3

Page 149: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 149

(4) Plug the signal cable of the GP2D120 to ADC3 port of the AT-BOT robot that can

detect the obstacles without any contact and be ready for the programming later on.

9.3 gp2d120_lib.h - the library for GP2D120The gp2d120_lib.h file contains an instruction set or a C/C++ language program

function to perform the GP2D120 module. Before running a function in this library,

developers have to append the library file at the beginning of the program with the

command below.

#include <gp2d120_lib.h>

9.3.1. Hardware connection

Because the GP2D120 is a sensor that shows the result as DC voltage related with a

measured distance. Starting the use of the module should connect with any analog input

of the AT-BOT. Includes ADC0 to ADC6.

9.3.2 getdist function

In the gp2d120_lib.h library , there is a function to read a measured distance from

the GP2D120 in the unit of centimeter.

Syntax

unsigned int getdist(char adc_ch)

Parameter

adc_ch : used to define analog input connecting with the GP2D120.

Return value

The distance in the centimeter unit.

Page 150: AT-BOT_E120703

150Robo-Creator : AT-BOT activity book

Experiment 21

Distance meaturement by GP2D120

This experiment is a programming to read distance values from the module GP2D120

by which the readable values are shown at the LCD module of AT-BOT.

Additional hardware connection

AT-BOT robot already installed with the GP2D120 module and connect to ADC3 port.

Procedure

L21.1 Create the new sketch file. Type the Listing L21-1 and save as gp2d120_test.pde

L20.2 Compile and upload the sketch to the AT-BOT. Unplug the USB cable.

#include <atx.h> // Include the main library

#include <gp2d120_lib.h> // Include the GP2D120 library

unsigned int dist; // Declare the distance measurment variable

void setup()

{}

void loop()

{

dist = getdist(3); // Read value from GP2D120 at ADC3 input

if(dist>=4 && dist<=32) // Compare with the reference value

{

lcd(“Distance: %d cm “,dist); // Show the distance at AT-BOT’s display

}

else

{

lcd(“Out of Range! “); // Show the warning message

// when the measured data is out of range

}

sleep(100); // Delay 0.1 second

}

Code explanation

The distance value that read from GP2D120 with getdist function is stored in the variable

dist. Then, the value will be analyzed whether it is in the range of 4 to 32cm. or not before

displayed at the LCD module. If the value is less than 4 and greater than 32, it is considered that

the data is not reliable. After this, the message as Out of Range! will be displayed at the LCD

module.

Listing L21-1 : gp2d120_test.pde, the sketch file for reading the

distance measurement from GP2D120

Page 151: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 151

L21.3 Run the program.

AT-BOT display shows the title message :

Distance: xxx cm

therefore xxx is the distance value in centimetre unit.

and displays the message

Out of Range!

when the distance value is out of the range 4 to 32cm.

L21.4 Put an object or your hand to block in the front of the GP2D120 in the operating

distance, which is 4 to 32 cm. Then, move in and away the object from the GP2D120.

Finally, monitor the readable distance value at the LCD module.

If the object is in the operating distance of the GP2D120, at the LCD module, there

will be a display of the distance value in centimetre. But if the object is not in the operating

distance, at the LCD module will be displayed the message Out or Range!, instead.ON

MOTOR

BATTERY LEVEL13 12 11 10 9 8

SERVO PORT

7.2

-9V B

ATT.

E2

RESET

+

-S

1415 PC6PC7

Cre

con

atore>>>>

trollrboa

>

erd R

>

48 50 SW2 49 SW1 ADC7 KNOB

TWIUART1

0SCL1SDA2RX13TX1

44ADC4 46ADC6 40ADC0 41ADC1 42ADC2START

45ADC5

>>

USBDATA

43ADC3

32

10

45

GP

2D

12

0

connect to ADC3

4cm.

32cm.

move object

Page 152: AT-BOT_E120703

152Robo-Creator : AT-BOT activity book

Experiment 22

Touchless object avoiding robot

This is the ability development of AT-BOT robos. Previously, AT-BOT used to avoid

obstacles by bumping. However, this experiment is different from the mentioned experiment

that this experiment will use the GPD120 sensor to help for detecting and avoiding the

obstacles without contact. There are conditions of functioning as follows:

1. In case that a robot does not detect any obstruction in the distance of 14 cm.

The robot will move forward.

2. In case that a robot finds obstruction in the distance less than 14 cm. The

robot will move backwards and then turn left to change the movement direction.

Procedure

L22.1 Create the new sketch file. Type the Listing L22-1 and save as robo_ranger.pde

L22.2 Compile and upload the sketch to the AT-BOT. Unplug the USB cable.

L22.3 Place the robot on the floor. Place some obstacle such as box or can to set the

demonstration field.

L22.4 Run the program.

AT-BOT display shows the title message : Press SW1

L18.5 Press the SW1 button on the AT-BOT. Observe the robot operation.

The AT-BOT will go straight constantly. When the robot will be able to detect

obstruction, it will reverse and turn left. After that, the AT-BOT will move forward in order to

avoid the obstruction. If the robot turns around and finds an object, it will go backward

again and turn left. The pattern will be repeated until the robot will be able to pass objects

and move straight later on.

Obstacle Obstacle Obstacle Obstacle

Page 153: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 153

#include <atx.h> // Include the main library#include <gp2d120_lob.h> // Include the GP2D120 library#define POW 80 // Set the motor power to 80%unsignd int dist; // Declare the distance variablevoid forward(unsigned int time) // Moving forward function{ motor(0,POW); // Drive the Motor-0 forward motor(1,POW); // Drive the Motor-1 forward motor(2,POW); // Drive the Motor-2 forward motor(3,POW); // Drive the Motor-3 forward sleep(time); // Delay for moving forward}void backward(unsigned int time){ motor(0,-POW); // Drive the Motor-0 backward motor(1,-POW); // Drive the Motor-1 backward motor(2,-POW); // Drive the Motor-2 backward motor(3,-POW); // Drive the Motor-3 backward sleep(time); // Delay for moving backward}void turn_left(unsigned int time){ motor(0,-POW); // Drive the Motor-0 backward motor(1,-POW); // Drive the Motor-1 backward motor(2,POW); // Drive the Motor-2 forward motor(3,POW); // Drive the Motor-3 forward sleep(time); // Delay for turning left}void turn_right(unsigned int time){ motor(0,POW); // Drive the Motor-0 forward motor(1,POW); // Drive the Motor-1 forward motor(2,-POW); // Drive the Motor-2 backward motor(3,-POW); // Drive the Motor-3 backward sleep(time); // Delay for turning right}void setup(){ lcd("SW1 Press!"); // Display title message for asking to press the SW1 sw1_press(); // Wait until the SW1 is pressed}void loop(){ dist = getdist(3); // Read the distance from GP2D120 at ADC3 input

if(dist>=4 && dist<15) // Found the object within 4 to 14cm. or not ?{

backward(500); // If found, move backward 0.5 secondturn_left(800); // Spin left 0.8 second

} // to change the movement directionelse{

forward(1); // Move forward with a short time}

}

Listing L22-1 : robo_ranger.pde, the sketch file for demonstration the

AT-BOT avoids the object without contact by using GP2D120 sensor

(continue)

Page 154: AT-BOT_E120703

154Robo-Creator : AT-BOT activity book

Code explanation

At the beginning of the program, there will be a display of the text Press SW1 at the LCD

module. When pressing the SW1 is done, the CPU will operate in the loop function to repeat

reading values from the GP2D120, which is connected to the ADC3 input.

Those values will be collected at the variable dist. Then, the program will take distance

values to compare based on these cases:

1. if(dist>=4 && dist<15) : it is verification that if the robot finds an object in the

range of 4 to 14 cm. or not. If it is true (a barrier present), the program will respond the robot

by moving backward and turning left to avoid the obstruction.

2. else : if the condition of the case 1 is not true, that is meant that there is no barrier

in the interesting distance, so the AT-BOT is designed to move forward.

More information

The development of the control program for AT-BOTs cooperated with the GP2D120 will

run the getdist function from the gp2d120_lib.h library to read distance values from barriers

in the centimetre.

Listing L22-1 : robo_ranger.pde, the sketch file for demonstration the

AT-BOT avoids the object without contact by using GP2D120 sensor

(final)

Page 155: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 155

In this chapter, it will introduce the control of servo motors of the AT-BOT. It can

drive 6 of 4.8 to 6V R/C servo motors through the ports 8 to 13.

10.1 Servo motor introductionFigure 10-1 shows a drawing of a Standard Servo. The plug is used to connect the

servo motor to a power source (Vdd and Vss) and a signal source (a microcontroller I/O

pin). The cable conducts Vdd, Vss and the signal line from the plug into the servo motor.

The horn is the part of the servo that looks like a four-pointed star. When the servo is running,

the horn is the moving part that the microcontroller controls. The case contains the servo’s

control circuits, a DC motor, and gears. These parts work together to take high/low signals

from the microcontroller and convert them into positions held by the servo horn.

Figure 10-2 shows the servo motor cable assignment. It has 3 wires with difference

color; Black for GND or Vss or Negative pole, Red for Vdd or Servo motor supply voltage

and White (sometime is yellow or brown) wire for signal.

The servo motor plug standard has 2 types; S-type and J-type are shown in the

figure 10-3.

Chapter 10

AT-BOT with Servo motor

Figure 10-1 : Standard servo motor physical

STANDARDSERVO MOTOR

Case

Horn

CablePlug

Page 156: AT-BOT_E120703

156Robo-Creator : AT-BOT activity book

Controlling of the servo motors is used to pulse controlling. The control pulse is positive

going pulse with length of 1 to 2 ms which is repeated about 50 to 60 times a second. You

can check the details in the figure 10-4. Start by generating a pulse a period 20 millisecond

and adjust the positive pulse width 1 millsecond. The servo motor will move the horn to last

left position. The pulse width 1.5 millisecond move the servo horn to center and pulse

width 2 millsecond causes the servo horn to last right position.

The important specification of servo motor are 2 points, Speed or Servo turn rate or

transit time and Torque. The servo turn rate, or transit time, is used for determining servo

rotational velocity. This is the amount of time it takes for the servo to move a set amount,

usually 60 degrees. For example, suppose you have a servo with a transit time of 0.17sec/

60 degrees at no load. This means it would take nearly half a second to rotate an entire

180 degrees. More if the servo were under a load. This information is very important if high

servo response speed is a requirement of your robot application. It is also useful for

determining the maximum forward velocity of your robot if your servo is modified for full

rotation. Remember, the worst case turning time is when the servo is at the minimum

rotation angle and is then commanded to go to maximum rotation angle, all while under

load. This can take several seconds on a very high torque servo.

Torque is the tendency of a force to rotate an object about an axis. The torque

unit is ounce-inches (oz-in) or kilogram-centimetre (kg-cm). It tell you know about this servo

motor can drive a load weight in 1 oz. to move 1 inche or 1kg. weight to moved 1

centimeter (1oz. = 0.028kg. or 1kg. = 25.274oz.). Normally the RC servo motor has 3.40 kg-

cm/47oz-in torque.

(b) J-type plug(a) S-type plug

Figure 10-2 : Standard Servo motor

cable assignment

Figure 10-3 : Standard Servo motor

plug type

Page 157: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 157

1 to 2 millsecond pulse

20ms period

(a) Servo motor control pulse

1 millisecond pulse

(b) 1 millisecond pulse causesservo horn moves anti-clockwisedirection to last right position (0o)

1.5 millisecond pulse

(c) 1.5 millisecond pulse causesservo horn moves to center position

2 millisecond pulse

(d) 2 millisecond pulse causesservo horn moves clockwisedirection to last left position (180o)

STANDARDSERVO MOTOR

STANDARDSERVO MOTOR

STANDARDSERVO MOTOR

Figure 10-4 : Timing diagram of the servo motor control pulse

Page 158: AT-BOT_E120703

158Robo-Creator : AT-BOT activity book

10.3 Servo motor control management of AT-BOTrobot

In an AT-BOT robot, using the port pins 8 to 13 (sorted by the requirements of Wiring

I/O and required to use these pin names in the programming in Wiring IDE) to generate

pulse signal in order to drive servo motors.

The power supply voltage of servo motors comes from a battery connected with

the circuit board of ATX through the control circuit of the constant power supply. At +6V

can supply the electric current 1500mA, so it can be applied to all models of small servo

motors, which use the power supply in the range of 4.8 to 6V.

Figure 10-5 : Demonstration of servo motor outputs of the AT-BOT

ON

MOTOR

BATTERY LEVEL1312111098

SERVO PORT

7.2

-9V B

ATT.

E2

RESET

+

-S

14 15PC6 PC7

C r e

c o n

a t o r e > > > >

t r o l l r b o a

>

e r dR

>

4850SW249SW1ADC7KNOB

TWI UART1

0 SCL 1 SDA 2 RX1 3 TX1

44 ADC446 ADC640 ADC041 ADC142 ADC2 START

45 ADC5

> >

USB DATA

43 ADC3

32

10

45

6 of Servo motor output[8 to 13]

Page 159: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 159

10.4 Servo motor library One important thing of controlling servo motors is to create pulse signal, which

comes from programming to command the microcontroller to produce desirable pulse

signal. To help the programming controlled by C language in AT-BOT robots more

convenient, the library file servo.h has to be attached with the program.

This library file supports all functions for controlling 6 servo motor outputs of the ATX

controller board. This library must be included at the top of the program with the command

#include as follows :

#include <servo.h> or #inclue <atx.h>

There is one function. It is servo.

Syntax

void servo(unsigned char _ch, int _pos)

Parameter

_ch - Servo motor output (8 to 13)

Define as 8 for servo motor output #1 (8)

Define as 9 for servo motor output #2 (9)

Define as 10 for servo motor output #3 (10)

Define as 11 for servo motor output #4 (11)

Define as 12 for servo motor output #5 (12)

Define as 13 for servo motor output #6 (13)

_pos - Set the sevo motor shaft poistion (0 to 180 and -1)

If set to -1, disable selected servo motor output

Page 160: AT-BOT_E120703

160Robo-Creator : AT-BOT activity book

Experiment 23

AT-BOT controlled the servo motor

In this experiment presents about how to test the positional control of the servo

motorat ch. 10 of an AT-BOT by pressing the SW1 (adding a positional value) and SW2

(reducing a position value) swithes as well as monitoring a value of the reference position

of the servo motor. The reference position value will be shown at the LCD module on the

AT-BOT robot.

Additional hardware connection

Connect a servo motor to port 10 of servo motor output of the AT-BOT

Procedure

L23.1 Create the new sketch file. Type the Listing L23-1 and save as servo_test.pde

L23.2 Compile and upload the sketch to the AT-BOT. Unplug the USB cable.

L23.3 Run the program.

AT-BOT display shows the title message :

Position: xx

therefore xx is the servo motor shaft’s position number

ON

MOTOR

BATTERY LEVEL1312111098

SERVO PORT

7.2

-9V B

ATT.

E2

RESET

+

-S

14 15PC6 PC7

Pos

con

ition: 20>

troll r boa

>

e rdR

>

4850SW249SW1ADC7KNOB

TWI UART1

0 SCL 1 SDA 2 RX1 3 TX1

44 ADC446 ADC640 ADC041 ADC142 ADC2 START

45 ADC5

>>

USB DATA

43 ADC3

32

10

45

STANDARDSERVO MOTOR

Servo motor

Increaseposition

Decreaseposition

Page 161: AT-BOT_E120703

Robo-Creator : AT-BOT activity book 161

#include <atx.h> // Include the main library

unsigned int pos=0; // Declare the servo motor position variable

void setup()

{}

void loop()

{

lcd(“Position: %d “,pos); // Show the position value

servo(10,pos); // Set the postion to servo motor output #10

if(sw1()==0) // Check the SW1 pressing

{

pos++; // Increase the position value

sleep(100); // Debouncing delay

}

if(sw2()==0) // Check the SW2 pressing

{

pos - - ; // Decrese the position value

sleep(100); // Debouncing delay

}

}

Code explanation

The program will operate in the loop function to repeat showing the result of the positional

values of the servo motor at port 10 of AT-BOT and define the values constantly. At the same

time, the function will verify addition and deduction of the positional values of servo motor shaft

form pressing the switches SW1 and SW2 respectively. The program will work like this repeatedly

and continuously.

Listing L23-1 : servo_test.pde, the sketch file for controlling

the servo motor shaft position of the AT-BOT

L23.4 Try to press the SW1 switch on the AT-BOT to increase the postion value.

The servo motor shaft is driven to upper position. Also at the display of AT-BOT shows

the position value. However if the value is greater than 90 greatly, tthe servo motor may

not stable to maintain position.

L23 .5 Press the SW2 switch on the AT-BOT to decrease the postion value.

The servo motor shaft is driven to lower position. At the display of AT-BOT shows the

position value. However if the value is less than 20 greatly, the servo motor may not stable

to maintain position also.

Note: About the positional range of servo motors, each manufacturer may have

different positional range or direction of position. Therefore, developers have to create

programming about testing positional values of servo motors in order to know how many

a positional value is in the control program when you need to control a servo motor to be

located at a desired position.

Page 162: AT-BOT_E120703

Recommended