Browse Source

tests/driver_srf02: mutliple improvements

- added single shot conversion command
- enhanced README
- added more verbose output
- now all outputted addresses are in decimal format
pr/gpio
Hauke Petersen 7 years ago
parent
commit
1a91600c24
  1. 47
      tests/driver_srf02/README.md
  2. 43
      tests/driver_srf02/main.c

47
tests/driver_srf02/README.md

@ -2,9 +2,50 @@
This is a manual test application for the SRF02 ultrasonic ranger driver.
# Usage
First you always need to initialize the sensor using the `init` shell command
and the devices address in right-aligned, decimal format (as specified in the
reference manual), e.g.: `init 224`.
If successful (the shell will tell you...), you can sample distance data from
the device, either once (`shoot`) or continuously every second (`sample`).
After initialization, the sensor value is read periodically and printed to the STDOUT.
This test application also allows to re-program the I2C address of SRF02
devices. For this initialize the device with its current address and then change
to the new address using the `addr` shell command (e.g. `addr 228`). After
calling the `addr` command, this application will automatically re-initialize
the device with the new address, so it is usable right away. Refer to the
datasheet for more information on usable addresses.
To verify the seen value you can focus the sensor against any reflecting object and vary the distance to
see the value changing.
The following sequence shows how to re-program a device. The initial address is
`224`, the new address after the sequence is `228`:
```
2016-03-21 15:53:34,049 - INFO # > init 224
2016-03-21 15:53:34,054 - INFO # Initializing SRF02 sensor at I2C_DEV(0), address is 224
2016-03-21 15:53:34,079 - INFO # ... [Ok]
2016-03-21 15:53:34,079 - INFO #
shoot
2016-03-21 15:53:37,339 - INFO # > shoot
2016-03-21 15:53:37,411 - INFO # distance = 94 cm
addr 228
2016-03-21 15:53:41,468 - INFO # > addr 228
2016-03-21 15:53:41,472 - INFO # Set address to 228
shoot
2016-03-21 15:53:42,454 - INFO # > shoot
2016-03-21 15:53:42,527 - INFO # distance = 94 cm
init 224
2016-03-21 15:53:48,933 - INFO # > init 224
2016-03-21 15:53:48,938 - INFO # Initializing SRF02 sensor at I2C_DEV(0), address is 224
2016-03-21 15:53:48,962 - INFO # ... [Failed]
shoot
2016-03-21 15:53:50,568 - INFO # > shoot
2016-03-21 15:53:50,641 - INFO # distance = 29703 cm
init 228
2016-03-21 15:53:53,084 - INFO # > init 228
2016-03-21 15:53:53,089 - INFO # Initializing SRF02 sensor at I2C_DEV(0), address is 228
2016-03-21 15:53:53,113 - INFO # ... [Ok]
2016-03-21 15:53:53,113 - INFO #
shoot
2016-03-21 15:53:54,201 - INFO # > shoot
2016-03-21 15:53:54,274 - INFO # distance = 94 cm
```

43
tests/driver_srf02/main.c

@ -40,15 +40,10 @@
static srf02_t dev;
static void sample_loop(void)
static void sample(void)
{
uint32_t wakeup = xtimer_now();
while(1) {
uint16_t distance = srf02_get_distance(&dev, TEST_MODE);
printf("distance = %3i cm\n", distance);
xtimer_usleep_until(&wakeup, SAMPLE_PERIOD);
}
uint16_t distance = srf02_get_distance(&dev, TEST_MODE);
printf("distance = %3i cm\n", distance);
}
static int cmd_init(int argc, char **argv)
@ -62,7 +57,7 @@ static int cmd_init(int argc, char **argv)
uint8_t addr = (uint8_t)atoi(argv[1]);
printf("Initializing SRF02 sensor at I2C_DEV(%i), address is 0x%02x\n... ",
printf("Initializing SRF02 sensor at I2C_DEV(%i), address is %i\n... ",
TEST_SRF02_I2C, (int)addr);
res = srf02_init(&dev, TEST_SRF02_I2C, addr);
if (res < 0) {
@ -79,8 +74,22 @@ static int cmd_sample(int argc, char **argv)
{
(void)argc;
(void)argv;
sample_loop();
uint32_t wakeup = xtimer_now();
while(1) {
sample();
xtimer_usleep_until(&wakeup, SAMPLE_PERIOD);
}
return 0;
}
static int cmd_shoot(int argc, char **argv)
{
(void)argc;
(void)argv;
sample();
return 0;
}
@ -95,13 +104,14 @@ static int cmd_set_addr(int argc, char **argv)
new_addr = (uint8_t)atoi(argv[1]);
srf02_set_addr(&dev, new_addr);
printf("Set address to %i (0x%02x)\n", (int)new_addr, (int)new_addr);
printf("Set address to %i\n", (int)new_addr);
return 0;
}
static const shell_command_t shell_commands[] = {
{ "init", "initialize a device", cmd_init },
{ "sample", "start sampling", cmd_sample },
{ "shoot", "get a single sample", cmd_shoot },
{ "addr", "reprogram the devices address", cmd_set_addr },
{ NULL, NULL, NULL }
};
@ -109,8 +119,15 @@ static const shell_command_t shell_commands[] = {
int main(void)
{
puts("\nSRF02 Ultrasonic Range Sensor Test\n");
puts("This test will sample the sensor once per second and display the\n"
"result\n");
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");
char line_buf[SHELL_DEFAULT_BUFSIZE];
shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);

Loading…
Cancel
Save