S14: Need For Speed

From Embedded Systems Learning Academy
Revision as of 09:51, 22 May 2014 by Proj user17 (talk | contribs) (Abstract)

Jump to: navigation, search

Abstract

File:CMPE 244 F14 PU17
Android meets Embedded System

This page contains design and implementation of our Embedded Software project, Need For Speed. It's about maneuvering an RC vehicle using the Acceleration Sensor in a smart phone. An Android Application is developed to transmit the Acceleration Sensor Signals to the vehicle via Bluetooth. The car is mounted with a Bluetooth module to receive data from the phone. And the acceleration and turning of the vehicle will be based on this data. The vehicle is also mounted with two Infrared Sensor modules for collision detection, one in the front and other in the rear.Detection of any obstacle in the vehicles path will bring the vehicle to a halt state immediately.

Introduction

In this project we are using JY-MCU Bluetooth module to be the wireless link between the smartphone and the battery powered RC vehicle. The Android application is developed to transmit a particular byte for every threshold value of the Acceleration Sensor i.e. when the phone is one byte for front tilt and another for back. This received byte is compared by the SJOne board with few predefined values and accordingly PWM signals are given to the motors.

The input of the IR sensors is connected to the hardware interrupts on the SJOne board, the interrupt subroutine will be called every time there is an obstacle in the path of the vehicle, this subroutine will in turn stop the vehicle.


Objectives

As this project is both software and hardware based, our objectives were as follows;

  • Hardware Objectives
    • To achieve a constant 6V DC supply for the motors and 5V DC for the board from the battery.
    • To interface the motors to the SJOne board via L298 motor driver.
    • To calibrate the IR sensors to sense obstacle in 6" proximity.
  • Software Objectives
    • To develop an Android application to send the data from Acceleration Sensor via Bluetooth.
    • To develop a FreeRTOS code to receive these signals and process them.
    • To develop a FreeRTOS code to send PWM sigal to the motor for variable speed.
    • To develop a FreeRTOS code to accept interrupt from the IR sensors and take actions accordingly.

Technology Used

  • A motion sensor
  • A distance sensor

Team Members & Responsibilities

  • Amey Patil
    • Hardware Design
    • Free RTOS (Part 1: To interface motors and LPC1758 using PWM)
    • Android Application(Part1: To send data from Android Phone to LPC1758)
  • Siddhata Patil
    • Free RTOS (Part 2: To form pairing of LPC1758 and Bluetooth module)
    • Free RTOS (Part 3: To send and receive data between Bluetooth module and LPC1758)
    • Android Application(Part2: To send results of accelerometer sensor from Android Phone to LPC1758)

Schedule

Week# Planned Date Completed Date Tasks Status
1 3/6 3/13 Planning Hardware and placing order for the required components Completed
2 3/20 3/20 Designing and Assembling the Hardware(Motors+Motor Driver+LPC1758) Completed
3 3/20 3/20 Reviewing Datasheet, Pin Selection and Setting up the Software Completed
4 3/27 3/27 Designing and Assembling the Hardware(Bluetooth Module+LPC1758) Completed
5 3/27 3/27 Free RTOS pairing between LPC1758 and Bluetooth Module Completed
6 3/28 3/28 Testing the pairing of Bluetooth and LPC1758 Completed
7 4/3 4/3 Free RTOS code for PWM Completed
8 4/11 4/18 Free RTOS code for sending and receiving data from Bluetooth Completed
9 4/18 4/24 Android App Designing and learning basics of Android SDK Completed
10 4/24 4/24 Android App Designing Completed
11 5/1 Android App java code to transfer data from phone to LPC1758
12 5/1 Android App java code to send values of accelerometer sensor to LPC1758
13 5/1 5/ Final Project Report
14 5/8 5/8 Final Demo


Parts List & Cost

Category Item Quantity Unit Cost Total Cost
Electronics SJOne Board 1 80.00 80.00
9.6V 1600mAh Rechargeable Ni-Mh Battery 1 14.42 14.42
6V DC Gear Motor with Tyres 4 3.25 13.00
JY-MCU Bluetooth Module 1 7.68 7.68
IR Transceiver Module 2 1.00 2.00
L298 Motor Driver 1 7.50 7.50
LM 7806 Voltage Regulator 1 0.45 0.45
LM 7805 Voltage Regulator 1 0.32 0.32
10uF Electrolytic Capacitor 2 0.13 0.26
1uF Ceramic Capacitor 2 0.10 0.20
Chassis Smart Car Chassis 1 10.00 10.00
Connections Nuts, Bolts and Spacers 3.00
Phone Android Smart Phone 1
TOTAL $138.83


Design and Implementation

Hardware Design

[[]] The next block diagram displays the buses and interfaces used in the hardware design: (click to enlarge).

Hardware Implementation

Frame and Inner Placement

[[]]For convenience, a New Bright toy RC car was purchased and disassembled. The motors and wheels were already pre-mounted onto the chassis. Motor controllers could easily control the motors to drive and steer the vehicle. On the chassis, slots above the wheel wells were large enough to house motor controllers while the center of the chassis provided enough room to include a small breadboard and the microcontroller. All wires were shortened to the shortest length possible to reduce clutter.

Sensor and Alarm Placement

The sensors, which guided car control, were strategically mounted in front and above the car. This increased visibility of the sensors. Three distance sensors were used to detect obstacles: a left-, a center-, and a right-side sensor was used to determine steering of the vehicle. A front-mounted sensor sought motion as the car was stopped. Once motion was detected, the car promptly sounded the piezo buzzer alarm after determining that the motion was legitimate.

Power Supply

The battery box beneath the car provided additional convenience. It housed five 1.5-volt AA batteries that supplied up to 7.5 volts in series. This power supply provided enough power to run all of the components of this project. Whenever existing batteries began to run low on power, new batteries could be swapped into place easily. The following list describes the power requirements of each part used:

PartVoltage Range Source
Distance Sensors (Parallel) +5V Microcontroller Pin
LPC2148 Microcontroller +7V to +20V Battery Pack
Motion Sensors +5V to +12V Battery Pack
Motor Controllers +7V to +30V Battery Pack
Piezo Buzzer +3V to +6V Microcontroller Pin

Motion Sensor

[[]] The passive infrared sensor measures changes in the environment, using heat as an indicator. It signals the pin low when it detects a change in heat. Three pins, with associated wire colorings, are used: red for voltage source, brown for ground, and black for alarm signal. The alarm signal can be connected to any pin on the microcontroller that can handle GPIO. Also, a pull up resistor is needed on the pin to GPIO. The pull up resistor is required because the sensor alarm pin is an open collector. Without this resistor, the value of the alarm will merely fluctuate between low and high constantly. Shown below are the connections between the microcontroller and the alarm:

MicrocontrollerMotion Sensor
VCC (7.5V) VCC (7.5V)
GND GND
P0.10 Alarm (AL)


Distance Sensor

[[]] The SRF02 ultrasonic range finder (distance sensor) can operate through serial mode or I2C mode. This device carries five pins: +5V VCC, SDA, SCL, Mode, and Ground. Since the I2C mode is used, the mode pin doesn’t need to connect to anything. The SCL and SDA lines are connected to the pins that support SCL0 and SDA0 on the microcontroller. These pins also need a pair of resistors because I2C is an open collector.

MicrocontrollerDistance Sensor
Regulator (+5V) VCC (+5V)
P0.2 SDA
P0.3 SCL
GND GND

5A Motor Controllers

5A motor controller.

The motor controllers were used to adjust speed on the motors. The controllers maintain a continuous 5A current while applying voltage. Interfacing the motor controllers requires three pins: PWM, direction, and ground. Pins were initiated prior to using the motor controllers. The rear motor used PWM while the front motor used GPIO in place of PWM.

MicrocontrollerFront Motor Controller
VCC (7.5v) VCC (7.5v)
P0.9 PWM
P0.6 DIR
GND GND
MicrocontrollerRear Motor Controller
VCC (7.5v) VCC (7.5v)
P0.7 PWM
P0.8 DIR
GND GND

Switch

[[]] A single-pole double-throw switch was added to the board to quickly connect and disconnect power to the system.

Software Design

Using FreeRTOS

FreeRTOS, a real-time operating system, was used to program the hardware used on the vehicle. FreeRTOS mainly uses C while allowing for some expansion into C++. Its support for tasks and semaphores made it a convenient platform for writing and implementing software for this project. Furthermore, FreeRTOS requires a small amount of memory while running tasks quickly and efficiently.

Several labs built using FreeRTOS were completed prior to starting this project, which contributed to a good understanding of the FreeRTOS system. The basic coding structure used for the labs also served as the basic coding framework for the Evil Watchdog.

Part-by-part Expansion

Once all the necessary parts were collected, each part received its own dedicated piece of code and its own testing before becoming integrated with the other parts used for the car. Intuitively, the motor controllers were selected as the first parts for writing code, because the vehicle's movement depended entirely on the motors and wheels. Each motor controller can regulate the speed at which the attached wheels spin, through the use of pulse-width modulation (PWM). Looking at the car, only the rear wheels need speed control. It was not necessary to implement PWM on the front wheels because those wheels are used for steering and simply require steering extreme left, extreme right, or straight, thanks to the car's small size. The rear wheels were controlled through PWM, and the front wheels through GPIO.

Pulse-width Modulation

Pulse-width modulation, or PWM, allows a motor controller to govern the speed at which a motor turns. Two key features of a PWM driver are the frequency, which determines how often a cycle repeats, and the duty cycle, which controls the speed of the motor spinning. The PWM driver allows the microcontroller to send out pulses based on the frequency and duty cycle settings, similar to a person flapping a rope up and down with the other end of the rope attached to a wall. By changing the PWM's frequency, the vehicle's rear motors could be slowed down or sped up. The board pins were set up accordingly:

  1. Reset P0.7
  2. Assign P0.7 for PWM2
  3. Assign P0.8 for direction (GPIO)
  4. Set P0.8 direction as output

The PWM driver was initialized, too:

Initializing the PWM driver.

The PWM registers had to be set accordingly. Looking at the LPC2148 datasheet, the registers shown above were configured:

  • PWMTCR: Timer control. Reset first with bit 1.
  • PWMPR: Prescale. Increments every PCLK cycle. Set to zero.
  • PWMMCR: Match control. Control if interrupt is generated. Will reset on MR0.
  • PWMPCR: Control. Enables PWM in single-edge mode.
  • PWMMR0: PWM period. Set to 48000 Hz to match CPU.
  • PWMMR2: Duty cycle/PWM width. Compared to PWMMR0. Output duty is inversely related to PWMMR0.
  • PWMLER: Latch enable. Latch in all PWMMRx.
  • PWMTCR: Timer control (again). Enable counter with bits 0 and 3.

With the PWM registers initialized, the duty cycle could later be modified to change motor speed. PWMMR2 was used in this case. Therefore, if a change in motor speed was necessary, PWMMR2 could be set inclusively between 0 and 48000 to change the duty cycle.

Steering Control

At first, it seemed a good idea that PWM be used on the front motor controller. After attempting to establish PWM on the steering side of the car, Preet recommended that GPIO could handle the steering job just as well. The pins had to be initialized first:

  1. Assign P0.9 as GPIO for controlling full or zero power
  2. Set P0.9 as output
  3. Assign P0.6 as GPIO for direction
  4. Set P0.6 as output

The most difficult part of using GPIO for steering was mapping out which voltages corresponded to which steering direction. The motor controller could turn extreme left, extreme right, or stayed straight. To help determine the directions, a handy table was created:

Controller GPIO Low (0V)GPIO High (+3.3V)
PWM Pin Power No Power
DIR Pin Left Right

Changing the motor between go/stop and left/right was done by either setting the P0.6 or P0.9 pins, respectively, to 1 or 0 using IOSET0 and IOCLR0. Using the help of LEDs, the correct directions and power distribution was mapped for the motor controller. It was interesting to note that to get back to center, a change of direction had to take place. For example, if the car was heading left, it had to be steered back to the right to "release" the left steering before returning to center position.

Distance Sensors: I2C

The distance sensors were designed for use on the I2C or serial buses. Based on pin availability and software convenience, I2C was used to gather distance information from the sensors. The next question was: How would the microcontroller distinguish between the three sensors? A glance at the technical specifications provided the solution. Each sensor was equipped with a handy LED that flashed its address in a recognizable sequence. Each sequence was one long flash, accompanied by a number of short flashes. The short flashes represented the difference in the current address from 0xE0 in increments of 0x02, up to 15 short flashes. Legal addresses ranged from 0xE0 to 0xFE.

By comparing the sequence emitted, a programmer could determine the sensor's set address. Also, its address could be altered to allow the I2C to accommodate additional sensors, though each sensor must be programmed individually, one at a time. This sequence was used to set each sensor:

  1. Get sensor address
  2. Send 0xA0 to address
  3. Send 0xAA to address
  4. Send 0xA5 to address
  5. Send new address to address

For convenience, the following addresses were selected for the sensors: 0xE2 for the left sensor, 0xE4 for the center, and 0xE6 for the right. [[]] An I2C call is made when the microcontroller wants to retrieve information from the distance sensors. The I2C bus is shared, and the program allocates enough time to release slave devices when required. The "unit_command" variable was set to 81 to force the sensor to measure distances in centimeters. If the user wants to opt for measurements in inches, the variable can simply be changed to 80.

Locations 2 and 3 hold the newest reading in a 16-bit unsigned integer format. Through high and low bytes, the sensor readings could be retrieved. The datasheet warns that it is best to give a minimum of 65 milliseconds for the pings to fade away properly. In the code, a 75 ms vTaskDelay was added to accommodate for the wait.

Motion Sensor

As with the front motor, the pins for the motion sensor were initialized:

  1. Assign P0.10 as GPIO
  2. Set P0.10 as input

A GPIO input pin was designated on the microcontroller to interact with the motion sensor. The motion sensor dropped its voltage to zero whenever motion was detected, which registered sufficient data for the pin on the board.

To check for motion, the code looked for a zero on the P0.10 pin.

The system surveyed for motion eight times in 500 millisecond intervals. When motion was detected in five out of those eight intervals, the alarm was sounded. This prevented false alarms from occurring, in the event that stray, non-legitimate motion came across the sensor's environment. This diagram shows the motion surveying method: [[]]


Loading the Software

[[]]Once the program was built and compiled, a .hex file was generated, which contained the program that will be read by the microcontroller. The .hex file was transferred from computer to microcontroller through a USB cable. HyperLoad 1.1 was run on the programming computer to interact with the microcontroller via USB. Setting up HyperLoad 1.1 included changing the COM port speed to 500000 bps and target CPU frequency to 48000000. The .hex file was chosen, the proper port was opened, and the file was transferred.

Software Implementation

Task Flowchart

The program was designed to run by employing tasks that handled sensor readings and appropriate outputs. A "drive" task was assigned to handle vehicular driving, and a "SensorValue" task was assigned to continuously capture data from the distance and motion sensors. When combined, the tasks worked to drive the vehicle safely through a path. The below illustrates how the general function of the program used to control the Evil Watchdog: [[]] A timer, in conjunction with the vTaskDelay command, allowed the microcontroller to switch between driving mode and motion sensing mode. It is important to note that scanning for motion while the vehicle is moving is pointless. Unless there was a way to filter out motion caused by the vehicle, it was better to stop the vehicle briefly to look for motion before continuing driving.

Testing

Motor Controllers

The motor controllers were the first components tested once the PWM driver was written. At first, the assumption was that setting PWMMR2 to zero meant a full stop. That became untrue as the dog tried to run away at full speed. Once it was realized that a higher duty cycle corresponded to slower motion, the rear controller was successfully configured. The front motor controller was tested too. The main difficulty was in mapping out the voltage levels and voltage directions to see whether they corresponded to left, right, or center steering. Using LEDs helped figure out the mappings. An interesting point to note is that to return to center steering via GPIO, a change in steering direction must take place.

Distance Sensors

Prior to implementation, the distance sensors were placed on a breadboard to verify the accuracy of their readings. The sensors featured measurements at the centimeter level, allowing for more detailed integer readings compared to readings in inches. During testing, a tape measure was used to confirm sensor measurements. The error found was no more than 3-5% at distances up to 250 cm, which showed that the sensors were highly reliable for providing the vehicle's sight.

Motion Sensor

The motion sensor was tested independently with a voltage generator and a digital multimeter. During everything, the multimeter showed that the sensor was very sensitive to nearby movement. Another observation was that when higher voltage was applied, the motion sensor captured motion from further distances. Lastly, it was noted that there was a delay between returning from "motion detected" to "no motion detected." It turns out that the alarm signal stayed low when detecting motion for about 3 seconds before returning to high. An open collector circuit caused the delays observed.


Technical Challenges

Battery Life

Distance Sensor Placement

Motion Sensor Polling

Vehicle Weight

Conclusion

Finished Product

References

Thanks To...

  • Preet Kang: Lecturer

Datasheets

Appendix

  • [Project source code is available at SourceForge]
  • [Project Demonstration Video]