Thursday, February 18, 2016

Line Tracking Sensor Driver for Raspberry Pi


KY-033 is a line tracking sensor. This project is to develop a dynamically loadable device driver for sensor KY-033 to enabled in RaspberryPi -1.
KY-033 is a single pin line tracking sensor. 

Raspberry Pi -1, a very popular SBC running with Debian linux (code name Wheezy for raspberry pi) with default kernel version 3.18. 


As it required to develop a kernel module, same version of running kernel header should be installed and configured for the system.


Description of Hardware:

The traget sensor KY-033 is a Line Tracking Sensor consist of one Infrared light and one LED. It also consist of a Si photosensor. The basic principle of Line Tracking Sensor is to detect if any white light component reflect from opposite surface.



















According characteristics of light, it reflect from a white surface, whether in black surface all light component is absorbed. So, the photosensor from KY-033, if it detect any reflection, the sensor mark it as positive otherwise voltage level stays zero.

Device Driver

Wiring -
G – Ground [ rpi – 6]
V+ –  power [ rpi – 2]
S – control signal [rpi – 3 / GPIO - 2]

Device driver codes and their explanation –

File operations for device driver. As explained above – open, read and release are defined here followed by there function name.

static struct file_operations fops =
{
.open = open_init,
.read = read_init,
.release = release_init,
};

Registering a character device named “TracK” with random major number.

majorNumber = register_chrdev (0, "TracK", &fops);

Creating a device class, which used to tie the sensor with system. Next function [device_create()] is creating a device under the declared class using the majorNumber used while registering the character device and 0 as minor number. Minor number is used to identify, the individual physical or logical device file under same class.

class_create (THIS_MODULE, CLASS_NAME))
device_create (cl, NULL, MKDEV(majorNumber, 0), NULL, DEVICE_NAME))

To successfully retrive sensor data with error handling, it require 4 different gpio function as described below.

gpio_is_valid (gpioTrck)
gpio_request (gpioTrck, "sysfs")
gpio_direction_input (gpioTrck)
gpio_export (gpioTrck, false)

gpio_is_valid() is return type boolien function, holds a single integer type value – GPIO pin number and determine whether the given gpio pin number is valid or not.
Sysfs is usually point to /sys/class/gpio, in gpio_request(gpioTrcK,”sysfs”), the sysfs enables the control interface to control the GPIO from user_space.

For input, current GPIO status (low / high) can be read using gpio_get_value(unsigned int gpio_no). With copy_to_user (buffer as to, sen_status as from, sizeof(sen_status) as n) copies sensor status (low as black / high as white) to buffer, which is a pointer to user space program.

inf_Status = gpio_get_value (gpioTrck)

sprintf (sen_status, "%d", inf_Status)

cp_cnt = copy_to_user (buffer, sen_status, sizeof(sen_status))

The function ky033_exit() is the exit or cleanup function. It cleaning and unregistering all resources for the sensor opend at initialiation function.

static void __exit ky033_exit(void){

device_destroy (cl, MKDEV(majorNumber, 0));
class_unregister(cl);
class_destroy (cl);
unregister_chrdev(majorNumber, DEVICE_NAME);

A full source code will be found here.

No comments:

Post a Comment