2014-10-15 11:29:41 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Hamburg University of Applied Sciences
|
2015-09-04 18:47:13 +02:00
|
|
|
* 2015 Freie Universität Berlin
|
2014-10-15 11:29:41 +02:00
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2015-09-04 18:47:13 +02:00
|
|
|
* @ingroup tests
|
2014-10-15 11:29:41 +02:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief Test application for the SRF02 ultrasonic range sensor
|
|
|
|
*
|
|
|
|
* @author Peter Kietzmann <peter.kietzmann@haw-hamburg.de>
|
2015-04-13 13:07:56 +02:00
|
|
|
* @author Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
|
2015-09-04 18:47:13 +02:00
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
2018-06-27 09:24:39 +02:00
|
|
|
* @author Kevin Weiss <kevin.weiss@haw-hamburg.de>
|
2014-10-15 11:29:41 +02:00
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
2015-09-04 18:47:13 +02:00
|
|
|
|
2016-02-10 18:27:07 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "shell.h"
|
|
|
|
#include "xtimer.h"
|
2016-07-07 16:15:33 +02:00
|
|
|
#include "timex.h"
|
2016-02-10 18:27:07 +01:00
|
|
|
#include "srf02.h"
|
|
|
|
|
2014-10-15 11:29:41 +02:00
|
|
|
#ifndef TEST_SRF02_I2C
|
|
|
|
#error "TEST_SRF02_I2C not defined"
|
|
|
|
#endif
|
|
|
|
#ifndef TEST_MODE
|
|
|
|
#error "TEST_MODE not defined"
|
|
|
|
#endif
|
|
|
|
|
2017-01-17 16:44:05 +01:00
|
|
|
#define SAMPLE_PERIOD (100LU * US_PER_MS) /* 100 ms */
|
2014-10-15 11:29:41 +02:00
|
|
|
|
2016-02-10 18:27:07 +01:00
|
|
|
static srf02_t dev;
|
2014-10-15 11:29:41 +02:00
|
|
|
|
2016-03-21 15:39:10 +01:00
|
|
|
static void sample(void)
|
2016-02-10 18:27:07 +01:00
|
|
|
{
|
2016-03-21 15:39:10 +01:00
|
|
|
uint16_t distance = srf02_get_distance(&dev, TEST_MODE);
|
2018-06-27 09:24:39 +02:00
|
|
|
if (distance != 0xFFFF) {
|
|
|
|
printf("distance = %3i cm\n", distance);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("Sample failed!\n");
|
|
|
|
}
|
2016-02-10 18:27:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int cmd_init(int argc, char **argv)
|
2014-10-15 11:29:41 +02:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
2016-02-10 18:27:07 +01:00
|
|
|
if (argc < 2) {
|
|
|
|
printf("usage: %s <addr (decimal)>\n", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
2015-09-04 18:47:13 +02:00
|
|
|
|
2017-05-13 12:39:48 +02:00
|
|
|
uint8_t addr = atoi(argv[1]);
|
2016-02-10 18:27:07 +01:00
|
|
|
|
2016-03-21 15:39:10 +01:00
|
|
|
printf("Initializing SRF02 sensor at I2C_DEV(%i), address is %i\n... ",
|
2016-02-10 18:27:07 +01:00
|
|
|
TEST_SRF02_I2C, (int)addr);
|
|
|
|
res = srf02_init(&dev, TEST_SRF02_I2C, addr);
|
2014-10-15 11:29:41 +02:00
|
|
|
if (res < 0) {
|
2015-09-04 18:47:13 +02:00
|
|
|
puts("[Failed]");
|
2014-10-15 11:29:41 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
puts("[Ok]\n");
|
2015-09-04 18:47:13 +02:00
|
|
|
}
|
2016-02-10 18:27:07 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-10-15 11:29:41 +02:00
|
|
|
|
2016-02-10 18:27:07 +01:00
|
|
|
static int cmd_sample(int argc, char **argv)
|
|
|
|
{
|
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
2016-07-05 21:32:44 +02:00
|
|
|
xtimer_ticks32_t wakeup = xtimer_now();
|
2016-03-21 15:39:10 +01:00
|
|
|
|
|
|
|
while(1) {
|
|
|
|
sample();
|
2016-07-07 16:15:33 +02:00
|
|
|
xtimer_periodic_wakeup(&wakeup, SAMPLE_PERIOD);
|
2016-03-21 15:39:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cmd_shoot(int argc, char **argv)
|
|
|
|
{
|
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
2016-02-10 18:27:07 +01:00
|
|
|
|
2016-03-21 15:39:10 +01:00
|
|
|
sample();
|
2016-02-10 18:27:07 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cmd_set_addr(int argc, char **argv)
|
|
|
|
{
|
|
|
|
uint8_t new_addr;
|
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
printf("usage: %s <new_addr (decimal)>\n", argv[0]);
|
|
|
|
return 1;
|
2014-10-15 11:29:41 +02:00
|
|
|
}
|
2016-02-10 18:27:07 +01:00
|
|
|
|
2017-05-13 12:39:48 +02:00
|
|
|
new_addr = atoi(argv[1]);
|
2018-06-27 09:24:39 +02:00
|
|
|
if (srf02_set_addr(&dev, new_addr) == 0) {
|
|
|
|
printf("Set address to %i\n", (int)new_addr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Set address failed\n");
|
|
|
|
return 1;
|
2016-02-10 18:27:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const shell_command_t shell_commands[] = {
|
|
|
|
{ "init", "initialize a device", cmd_init },
|
|
|
|
{ "sample", "start sampling", cmd_sample },
|
2016-03-21 15:39:10 +01:00
|
|
|
{ "shoot", "get a single sample", cmd_shoot },
|
2016-02-10 18:27:07 +01:00
|
|
|
{ "addr", "reprogram the devices address", cmd_set_addr },
|
|
|
|
{ NULL, NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
puts("\nSRF02 Ultrasonic Range Sensor Test\n");
|
2016-03-21 15:39:10 +01:00
|
|
|
puts("Use the following flow to test your device/setup. First you need to\n"
|
|
|
|
"initialize your device (e.g. 'init 224'). Next you can sample your \n"
|
|
|
|
"device continuously ('sample'), or get one value ('shoot').\n\n"
|
|
|
|
"This application let's you also reprogram your device's I2C"
|
|
|
|
"address:\n"
|
|
|
|
" 1. initialize your device -> e.g. 'init 224'\n"
|
|
|
|
" 2. specify the new address -> e.g.'addr 228'\n"
|
|
|
|
"The device will be programmed with the new address and it is\n"
|
|
|
|
"re-initialized with the new address, so you can use it right away\n");
|
2016-02-10 18:27:07 +01:00
|
|
|
|
|
|
|
char line_buf[SHELL_DEFAULT_BUFSIZE];
|
|
|
|
shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);
|
2014-10-15 11:29:41 +02:00
|
|
|
}
|