Skip to content
Permalink
Browse files

samples: sample modified according to the changes in Zephyr macros

main.c file changes
1. Renamed <board.h> to <device.h>
2. <misc/printk.h> now is inserted into <sys/printk.h>
3. <i2c.h> and <gpio.h> now are inserted into
<drivers/i2c.h> and <drivers/gpio.h>
4. Deleted <errno.h> because it is not using
5. Deleted <pwm.h> because it is not using
6. Deleted <display/mb_display.h> because it is not using
in that project (legacy from old project).
7. Now we use the following commands to rename any
SW._GPIO_{CONTROLLER,PIN,FLAGS} to
DT_ALIAS_SW._GPIOS_{CONTROLLER,PIN,FLAGS} ,
so renamed SW0_GPIO_CONTROLLER to DT_ALIAS_SW0_GPIOS_CONTROLLER

prj.conf file changes
1. Deleted CONFIG_SYS_CLOCK_TICKS_PER_SEC=250
because apps shouldn't set tick rate.

Made a line follower robot sample using DFRobot Maqueen chassis
for micro:bit board. Deleted unused files.

Signed-off-by: Maksim Masalski <maxxliferobot@gmail.com>
  • Loading branch information...
maxxlife authored and carlescufi committed May 30, 2019
1 parent d4f1a5a commit 9901d8cf4bca2fa61f0d248cafae31e185b7c29d
@@ -0,0 +1,9 @@
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.13.1)

include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
project(robot)

FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})
@@ -0,0 +1,40 @@
.. _microbit_line_follower_robot:

BBC micro:bit line-follower robot
#################################

Overview
********
This sample show makers and robotics engineers how to use a Zephyr
OS application to control a stand-alone line-following DFRobot Maqueen
robot chassis containing a BBC micro:bit board.

Building and running
********************

To build and run this sample you'll need a `DFRobot Maqueen robot
chassis (ROB0148) <https://www.dfrobot.com/product-1783.html>`_
with a BBC micro:bit board. Use black tape to create a line track
for the robot to follow. Build and flash the program to the BBC
micro:bit board (described below), turn on the robot,
and put it on the black line track.

Build this sample project using these commands:

.. zephyr-app-commands::
:zephyr-app: samples/boards/microbit/line_follower_robot
:board: bbc_microbit
:goals: build
:compact:

Then flash the program to the micro:bit board.

Sample Output
*************

The sample program controls the robot to follow a line track and does
not write to the console. You can watch this `robot video`_
to see it in action.

.. _robot video:
https://youtu.be/tIvoHQjo8a4
@@ -0,0 +1,3 @@
CONFIG_GPIO=y
CONFIG_I2C=y
CONFIG_PRINTK=y
@@ -0,0 +1,6 @@
sample:
name: BBC micro:bit Line follower robot
tests:
sample.board.bbc_microbit.line_follower_robot:
platform_whitelist: bbc_microbit
tags: microbit line-follower robot car
@@ -0,0 +1,137 @@
/*
* Copyright (c) 2019 Maksim Masalski maxxliferobot@gmail.com
* A Line follower robot program using DFRobot Maqueen Robot and microbit
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr.h>
#include <sys/printk.h>
#include <drivers/gpio.h>
#include <drivers/i2c.h>
#include <device.h>

#define I2C_SLV_ADDR 0x10
#define I2C_DEV "I2C_0"
#define EXT_P13_GPIO_PIN 23 /* P13, SPI1 SCK */
#define EXT_P14_GPIO_PIN 22 /* P14, SPI1 MISO */

static struct device *gpio;
struct device *i2c_dev;
unsigned int left_line[1];
unsigned int right_line[1];
unsigned char buf[3];
unsigned char speed_hex[1];

/* Setup gpio of the microbit board */
static void line_detection(struct device *dev, struct gpio_callback *cb,
u32_t pins)
{
gpio_pin_read(gpio, EXT_P13_GPIO_PIN, left_line);
gpio_pin_read(gpio, EXT_P14_GPIO_PIN, right_line);
/* printk("%d %d\n", left_line[0], right_line[0]); */
}
/* Function to convert decimal speed value to hex speed value */
/* It makes possible to transfer that value using I2C bus */
int decimal_to_hex(int speed_decimal)
{
speed_hex[0] = (speed_decimal & 0x000000FF);
return speed_hex[0];
}

/* Function to control motors of the DFRobot Maqueen Robot */
/* Send value > 0 motor rotates forward */
/* Send 0 motor stop */
/* Send value < 0 motor rotates backward */
void motor_left_control(int left_speed)
{
if (left_speed < 0) {
left_speed = left_speed * (-1);
/* Command bits to control I2C motordriver of the robot */
buf[0] = 0x00;
buf[1] = 0x01;
buf[2] = decimal_to_hex(left_speed);
} else {
buf[0] = 0x00;
buf[1] = 0x00;
buf[2] = decimal_to_hex(left_speed);
}
/* Left motor write data*/
/* Address of the I2C motordriver on the robot is 0x10 */
i2c_write(i2c_dev, buf, 3, 0x10);
}

void motor_right_control(int right_speed)
{
if (right_speed < 0) {
right_speed = right_speed * (-1);
buf[0] = 0x02;
buf[1] = 0x01;
buf[2] = decimal_to_hex(right_speed);
} else {
buf[0] = 0x02;
buf[1] = 0x00;
buf[2] = decimal_to_hex(right_speed);
}

/* Right motor write data*/
i2c_write(i2c_dev, buf, 3, 0x10);
}

/* Line follower algorithm for the robot */
void line_follow(void)
{
if ((left_line[0] == 0) && (right_line[0] == 0)) {
motor_left_control(200);
motor_right_control(200);
} else {
if ((left_line[0] == 0) && (right_line[0] == 1)) {
motor_left_control(0);
motor_right_control(200);
if ((left_line[0] == 1) && (right_line[0] == 1)) {
motor_left_control(0);
motor_right_control(200);
}
} else {
if ((left_line[0] == 1) && (right_line[0] == 0)) {
motor_left_control(200);
motor_right_control(0);
if ((left_line[0] == 1) &&
(right_line[0] == 1)) {
motor_left_control(200);
motor_right_control(0);
}
if ((left_line[0] == 1) &&
(right_line[0] == 0)) {
motor_left_control(200);
} else {
motor_right_control(0);
}
}
}
}
}

void main(void)
{
static struct gpio_callback line_sensors;

gpio = device_get_binding(DT_ALIAS_SW0_GPIOS_CONTROLLER);
i2c_dev = device_get_binding(I2C_DEV);
/* Setup gpio to read data from digital line sensors of the robot */
gpio_pin_configure(gpio, EXT_P13_GPIO_PIN, (GPIO_DIR_IN |
GPIO_INT | GPIO_INT_EDGE | GPIO_INT_DOUBLE_EDGE));
gpio_pin_configure(gpio, EXT_P14_GPIO_PIN, (GPIO_DIR_IN |
GPIO_INT | GPIO_INT_EDGE | GPIO_INT_DOUBLE_EDGE));

gpio_init_callback(&line_sensors, line_detection,
BIT(EXT_P13_GPIO_PIN) | BIT(EXT_P14_GPIO_PIN));

gpio_add_callback(gpio, &line_sensors);

gpio_pin_enable_callback(gpio, EXT_P13_GPIO_PIN);
gpio_pin_enable_callback(gpio, EXT_P14_GPIO_PIN);

while (1) {
line_follow();
}
}

0 comments on commit 9901d8c

Please sign in to comment.
You can’t perform that action at this time.