Constructing a 16-Channel Bluetooth Wireless Servo Motor Controller

Ranchi,
INDIA
Useful Links in this Article:
ATMega8515/8515L Processors
8-Kbyte self-programming Flash Program Memory, 544 Byte internal + up to
64 Kbyte external SRAM, 512 Byte EEPROM. Up to 16 MIPS throughput at 16
Mhz. 2.7 - 5.5 Volt operation
Atmel ATMega Product Page:
(For Datasheets, Whitepapers and Application Notes)
http://www.atmel.com/dyn/products/product_card.asp?family_id=607&fami-
ly_name=AVR+8%2DBit+RISC+&part_id=2006
Embedded Developer ATmega Product landing page:
(For datasheets, chip samples, RFQ,s and tools purchases)
http://www.embeddeddeveloper.com/group/776/Atmel/ATmega8515_8515L.htm
ATMega Hardware and Software Tools Support:
(Select and purchase tools on-line)
http://www.embeddeddeveloper.com/atmeltools/?p=atmeltools
Bluetooth and Power Management:
http://www.atmel.com/dyn/general/advanced_search_results.asp?device=1&too
ls=1&faqs=1&datasheets=1&appNotes=1&userGuides=1&software=1&press=
1&articles=1&flyers=1&checkAll=1&checkAllReference=1&target=Bluetooth
SYNOPSIS
Most of us have been enjoyed creating hobby electronics circuits
in our late childhood. Some of us developed our skill from
amateur circuit builders to good circuit designers, while a
majority of us left it because of increased complexity of electronics
knowledge required for building circuits of our own requirement.
With the advent of microcontrollers and ICs, we can develop
circuits of our demand in no time. It is much simpler making
circuits with microcontrollers as compared to the discrete
electronics components.
It is fun working with ATMEL
®
microcontrollers as it is nicely
supported by cross compilers like AVR Studio, IAR Embedded
Workbench or GNU C Compiler and wide range of its features.
These compilers have very good blend of library functions which
give almost full freedom to program as we do in standard pro-
gramming languages like C/C++. Without going into details of
these we would like to mention one of the very good titles which
came in this Journal Novices Guide to AVR Development by
Arild Rodland. Starting with that, we came up to ATmega8515 for
fulfilling one of our requirements for the development of a servo
controller board for our biped robot. It requires about 12 servos
to be precisely controlled remotely using a PC. There are many
readymade servo controllers available in the market like
Parallax, Lynxmotion etc. to name a few, but we found it cheap-
er to design ourselves using AVR.
A
standard hobby servo is a DC Motor with a mechanical
feedback and a reduction gear to reduce its speed. It has a
potentiometer position encoder and a mechanical feedback
arrangement built in to its enclosure. It is driven by a square
wave pulse of 5V (TTL) in amplitude, width of which ranges from
1.0 mS to 2.0 mS with refresh rate of 50Hz i.e. 20 mS. The
duration of the pulse i.e. its width, is used to judge the angular
position of the motor. The angular position 0 to 180o, is linearly
dependent on the pulse width. Such a control is known as Pulse
Width Modulation (PWM) control. Several brands like Futaba,
JR, Airtronics (Sanwa), GWS, Hitec, BMS, Multiplex, Parallax
(Futaba) etc. manufacture compatible servos of similar
specifications. Most of these are available at any hobby shop.
The circuit uses an ATMEL
®
AVR 8 bit Microcontroller Tmega8515.
It is a 40 pin IC with four 8 bit ports and one 4 bit port. Although
it has only 2 dedicated PWM pins, all I/O pins can be internally
timed through program to get a PWM pulse. So we decided to
switch ON a microcontroller pin connected to the signal pin of
the servo for the required duration based on the angle which is
required to be moved. HE EMBEDDED CODE
By default a new AVR microcontroller is driven by its internal clock. It
is required to change the security and configuration bits i.e. fuse bits
to use external crystal oscillator. It is always advisable to refer
datasheet before handling these bits. The circuit uses a 11.0592 MHz
crystal which makes serial communication error of 0 % and PWM
error of 0.125%. The program is designed to accept or send serial data
at 230.4 kbps which may be modified to any other value based on
UBRR setting as per the datasheet. An optional Transmit function is
introduced in the code for debugging purpose.
The microcontroller program listed should be compiled using IAR
Embedded Workbench V4.2. Following Options must be selected
before building the Intel .hex file for burning it to the microcon-
troller. Sequence of options that must be selected from the main
menu of IAR Embedded Workbench is as follows:
Project > Create New Project > Expand C >Select AVR
®
Studio 4
Compatible output > Save As (project name) > Type the given
program listing under main.c program space.
Project > Options > General Options > Target [tab] > Processor
configuration > Select [--cpu=m8515, ATmega8515] from the
drop down combo box.
Project > Options > General Options > System [tab] > Check
[ ] Enable bit definitions in I/O - include files
Project > Options > Linker > Output [tab] > Click Other
[Radio Button] > Output format > Select intel-standard [from
Drop down combo box]
Project > Options > Linker > Extra Output [tab] > Check
Generate extra output file > Output format > Select ubrof 8
(forced) [from Drop down combo box]
For building HEX file for burning to the microcontroller through
any suitable burner tool like AVRISP. A HEX file (Projectname.hex)
is generated in the folder Debug\Exe by default.
Project > Rebuild All
The servo motor used for testing is GWS Micro 2BBMG, and it is
found that end to end pulse width required for motor to rotate 0 to
180o is 800_S to 2560_S. Any motor selected, it is better to go
through its datasheet once to obtain the pulse width required for
end positions and centre position. A linear interpolation is used to
convert angles to corresponding pulse width. A serial port can send
data of one byte at a time that ranges from 0 255. So angles
ranging from 0 to 180o is converted to 0 to 255 Scale and sent via
serial port to the controller. So a minimum angle which the con-
troller can turn is 180/255 = 0.705o.
i.e. pulse_duration = 800 + (2560 800)/255 _ relative position
in (0 255 Scale)
This converts the angles to corresponding pulse duration. A delay
routine is developed that is fed with delay time as input argument.
The delay time is readjusted for accounting the function call dura-
tion, for loop duration and error caused due to taking integral
time duration in __delay_cycles() function. So a correcting func-
tion is used as below.
corrected_duration = (pulse_duration - 3.617)/1.537
This is based on duration calculated by AVR
®
Studio results. A
direct calling of __delay_cycles() function cannot be used as the
duration required for this functions cannot be a variable. The AVR
®
Studio can open the Object file in Ubrof 8 format (*.dbg) generat-
ed along with the .hex file. This is used to simulate the program
before actual downloading.
A T M E L A P P L I C A T I O N S O N - L I N E J O U R N A L
Page 2
/*********************************************************************
16 Channel Bluetooth Servo Motor Control Program for ATmega8515
**********************************************************************/
#includ
e <ioavr.h>
#includ
e <intrinsics.h>
#define
CHECKBIT(ADDRESS, BIT) (ADDRESS & (1 << BIT))
#define
CLEARBIT(ADDRESS, BIT) (ADDRESS &= ~(1 << BIT))
#define
SETBIT(ADDRESS, BIT) (ADDRESS |= (1 << BIT))
#define
F_OSC
11059200
//Oscillator-frequency in Hz
#define
USART_BAUD_RATE
230400
// Baud Rate for Serial Communication
#define
USART_BAUD_CALC(USART_BAUD_RATE,F_OSC)
((F_OSC)/((USART_BAUD_RATE)*16)-1)
void
USART_Init (unsigned int</b>baud);
unsigned char
USART_Receive (void);
void
USART_Transmit (unsigned char</b>data);
void
Move_motor(unsigned char</b>Motor_number, unsigned int</b>corrected_duration);
void
delay_us(unsigned int</b>duration_in_us);
__C_task void</b>main (void)
{
DDRA= DDRC =
0xFF
;
//Make PORTAand PORTC as output for PWM
while (!CHECKBIT(PIND, PIND2));
//Wait for connection to be established
//PIND2 is trigerred by Status pin of eb505
USART_Init (USART_BAUD_CALC(USART_BAUD_RATE, F_OSC));
unsigned char
Motor_number, Motor_position;
while
(
1
)
{Motor_number = USART_Receive();
USART_Transmit
('M
');
Motor_position = USART_Receive();
USART_Transmit
('P
');
//Rescaling to 800uS to 2560uS pulse duration
unsigned int
pulse_duration
= 800
+ 6.901960784
* Motor_position;
//Correcting duration and compensating function call duration
//For Crystal of 11.0592 MHz
unsigned int corrected_duration = (pulse_duration -
3.617
)
/
1.537
;
Move_motor(Motor_number, corrected_duration);
}
}
//Initialize USART
void
USART_Init (unsigned int</b>baud)
{ HARDWARE DESCRIPTION
The robot controller board is based on ATMEL 8 bit ATmega8515
microcontroller. An 11.0592 MHz external crystal is used with USART
setting fixed to use a high baud rate of 230.4Kbps with 8 data bits, 1
stop bit and no parity (8, 1, N) format. A USB based IOGEAR
®
Bluetooth dongle is used at the PC end for signal transmission and
receiving from the controller board. The microcontroller may issue a
command for connecting to the PCs Bluetooth device. In the current
case host PC establishes the connection and serial data i.e. the motor
number and the angles (in 0 255 scale) at the required rate, is sent
via Bluetooth serial port. The board expects