Blame view

RIOT/drivers/include/srf08.h 5.56 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
  /*
   * Copyright (C) 2013 Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
   *
   * 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.
   */
  
  /**
   * @defgroup    drivers_srf08 SRF08 ultrasonic range sensor
   * @ingroup     drivers_sensors
   * @brief       Driver for the SRF08 ultrasonic range sensor
   *
   * The connection between the MCU and the SRF08 is based on the i2c-interface.
   *
   * @{
   *
   * @file
   * @brief       Driver definitions for the SRF02 ultrasonic ranger.
   *
   * The connection between the SRF08 and the MCU is based on the i2c interface.
   *
   * @author      Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
   * @author      Peter Kietzmann <peter.kietzmann@haw-hamburg.de>
   */
  
  #ifndef SRF08_H
  #define SRF08_H
  
  #include <stdint.h>
  #include "periph/i2c.h"
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  /** @brief   The sensors default I2C address */
  #define SRF08_DEFAULT_ADDR              112
  
  /** @brief   The sensors command register (write) */
  #define SRF08_COMMAND_REG               0x0
  
  /** @brief   Max. gain register (write) */
  #define SRF08_GAIN_REG                  0x1
  
  /** @brief   The sensors range register (write) */
  #define SRF08_RANGE_REG                 0x2
  
  /** @brief   The upper measurement byte (read) */
  #define SRF08_RANGE_HIGH_BYTE           0x2
  
  /** @brief   The lower measurement byte (read)*/
  #define SRF08_RANGE_LOW_BYTE            0x3
  
  /** @brief   Number of highest register */
  #define SRF08_MAX_REGISTER_NUMBER       35
  
  /** @brief   Maximum number of echos from more distant objects */
  #define SRF08_MAX_ECHO_NUMBER           17
  
  /** @brief   Maximum range the sensor can measure (6m) */
  #define SRF08_MAX_RANGE_6M              0x8C
  
  /** @brief   Maximum gain of the sensor (1025)*/
  #define SRF08_MAX_GAIN                  0x25
  
  /**
   * @brief   Device descriptor for SRF08 sensors
   */
  typedef struct {
      i2c_t i2c;               /**< I2C device the sensor is connected to */
      uint8_t addr;            /**< I2C bus address of the sensor */
  } srf08_t;
  
  /**
   * @brief   Possible measurement modes for the SRF08 sensor
   */
  typedef enum {
      SRF08_MODE_INCH =          0x50,      /**< result in inches */
      SRF08_MODE_CM =            0x51,      /**< result in centimeters */
      SRF08_MODE_MICRO_SEC =     0x52,      /**< result in centimeters */
      SRF08_ANN_MODE_INCH =      0x53,      /**< synchronous measurement in inches */
      SRF08_ANN_MODE_CM =        0x54,      /**< synchronous measurement in centimeters */
      SRF08_ANN_MODE_MICRO_SEC = 0x55       /**< synchronous measurement in microseconds */
  }srf08_mode_t;
  
  /**
   * @brief   Initialize the SRF08 ultrasonic sensor
   *
   * @param[in] dev           device descriptor of an SRF08 sensor
   * @param[in] i2c           I2C device the sensor is connected to
   * @param[in] addr          I2C address of the sensor
   * @param[in] speed         I2C speed mode
   *
   * @return                  0 on successful initialization
   * @return                  -1 on undefined device given
   * @return                  -2 on unsupported speed value
   * @return                  -3 on max. range error
   * @return                  -4 on max. gain error
   *
   */
  int srf08_init(srf08_t *dev, i2c_t i2c, uint8_t addr, i2c_speed_t speed);
  
  /**
   * @brief   Set the maximum range of the SRF08.
   *
   * @param[in] dev           device descriptor of an SRF08 sensor
   * @param[in] max_range     the adjusted maximal range is:
   *                          max_range = (max_range x 43mm) + 43mm.
   *                          The hardware limit is 6m.
   *
   * @return                  1 on success
   * @return                  -1 on undefined device given
   *
   */
  int srf08_set_max_range(const srf08_t *dev, uint8_t max_range);
  
  /**
   * @brief   Set the maximum of the analog stages.
   *
   * @ note
   * This value is just a limitation of the maximum amplification and not the actual.
   * While measuing, this value starts at its minimum and increases approx. each 75 us
   * until the maximum value is reached.
   *
   * @param[in] dev           device descriptor of an SRF08 sensor
   * @param[in] max_gain  the maximal gain value.
   *
   * @return                  1 on success
   * @return                  -1 on undefined device given
   *
   */
  int srf08_set_max_gain(const srf08_t *dev, uint8_t max_gain);
  
  /**
   * @brief   Get all distances measured from the SRF08 ultrasonic sensor.
   *          The results of a ranging can be returned in inches, centimeters
   *          or microseconds. The SRF08 can detect up to 17 targets.
   *
   *
   * @param[in] dev           device descriptor of an SRF08 sensor
   * @param[in] range_array   a pointer to a buffer holding the ranging results.
   * @param[in] num_echos     number of stored measurements (echos)
   * @param[in] ranging_mode  there are three real ranging modes, which return
   *                          the result in inches, centimeters or microseconds.
   *                          Another set of three fake ranging modes do the same
   *                          but without transmitting the burst.
   *
   * @return                  number of measured echos on success
   * @return                  -1 on ranging mode errors
   * @return                  -2 if num_echos exceeds the max. number of
                                 possible measurements
   * @return                  -3 if i2c read high byte failed
   * @return                  -4 if i2c read low byte failed
   *
   */
  int srf08_get_distances(const srf08_t *dev,
                          uint16_t *range_array,
                          int num_echos,
                          srf08_mode_t ranging_mode);
  
  #ifdef __cplusplus
  }
  #endif
  
  /** @} */
  #endif /* SRF08_H */