summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/urm37/urm37.cxx
blob: 85305d31a70db2a1ed8defae61ef5cffc9cad972 (plain)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
 * Author: Jon Trulson <jtrulson@ics.com>
 * Copyright (c) 2015 Intel Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <iostream>

#include "urm37.h"

using namespace upm;
using namespace std;

static const int waitTimeout = 1000;
static const int maxRetries = 10;

URM37::URM37(int aPin, int resetPin, int triggerPin, float aref) :
  m_uart(0), m_aio(new mraa::Aio(aPin)), m_gpioReset(resetPin), 
  m_gpioTrigger(new mraa::Gpio(triggerPin))
{
  m_analogMode = true;

  m_aRes = (1 << m_aio->getBit());
  m_aref = aref;

  m_gpioTrigger->dir(mraa::DIR_OUT);

  // setup trigger for mmapped access, not a big deal if this fails
  m_gpioTrigger->useMmap(true);

  // trigger high
  m_gpioTrigger->write(1);

  init();
}

URM37::URM37(int uart, int resetPin) :
  m_uart(new mraa::Uart(uart)), m_aio(0), m_gpioReset(resetPin), 
  m_gpioTrigger(0)
{
  m_analogMode = false;

  m_aRes = 0;
  m_aref = 0;

  // 9600 baud is the only support baud rate...
  if (m_uart->setBaudRate(9600))
    {
      throw std::runtime_error(string(__FUNCTION__) +
                               ": setBaudRate(9600) failed");
      return;
    }

  init();
}

URM37::~URM37()
{
  if (m_uart)
    delete m_uart;
  if (m_aio)
    delete m_aio;
  if(m_gpioTrigger)
    delete m_gpioTrigger;
}

void URM37::init()
{
  m_gpioReset.dir(mraa::DIR_OUT);

  // reset the device
  reset();
}

void URM37::reset()
{
  // toggle reset
  m_gpioReset.write(0);
  usleep(100);
  m_gpioReset.write(1);
  // wait for reset to complete
  sleep(3);
}

bool URM37::dataAvailable(unsigned int millis)
{
  return m_uart->dataAvailable(millis);
}

std::string URM37::readDataStr(int len)
{
  return m_uart->readStr(len);
}

int URM37::writeDataStr(std::string data)
{
  m_uart->flush();
  return m_uart->writeStr(data);
}

float URM37::getDistance(int degrees)
{
  // analog mode
  if (m_analogMode)
    {
      m_gpioTrigger->write(0);
      int val = m_aio->read();
      m_gpioTrigger->write(1);
      
      float mVolts = (float(val) * (m_aref / m_aRes)) * 1000.0;
      
      // 6.8mV per CM
      return (mVolts / 6.8);
    }

  // UART mode
  // query distance cmd sequence
  uint8_t deg = (uint8_t)(degrees / 6);
  if (deg > 46)
    throw std::out_of_range(string(__FUNCTION__) +
                            ": degrees out of range, must be 0-270");

  string cmd;
  uint8_t cksum = 0x22 + deg + 0x00;
  cmd.push_back(0x22);
  cmd.push_back(deg);
  cmd.push_back(0x00);
  cmd.push_back(cksum);
  
  string resp = sendCommand(cmd);

  if (resp.empty())
    {
      throw std::runtime_error(string(__FUNCTION__) +
                               ": sendCommand() failed");
      return 0.0;
    }

  uint8_t h = (uint8_t)resp[1];
  uint8_t l = (uint8_t)resp[2];

  float distance = float((h << 8) | l);

  return (distance);
}

float URM37::getTemperature()
{
  if (m_analogMode)
    {
      throw std::runtime_error(string(__FUNCTION__) +
                 ": Temperature measurement not available in analog mode");

      return 0.0;
    }

  // query temperature cmd sequence
  string cmd;
  cmd.push_back(0x11);
  cmd.push_back(0x00);
  cmd.push_back(0x00);
  cmd.push_back(0x11);
  
  string resp = sendCommand(cmd);

  if (resp.empty())
    {
      throw std::runtime_error(string(__FUNCTION__) +
                               ": sendCommand() failed");
      return 0.0;
    }

  uint8_t h = (uint8_t)resp[1];
  uint8_t l = (uint8_t)resp[2];

  float temp;
  temp = float((h & 0x0f) * 256 + l) / 10.0;
  if (h & 0xf0)
    temp *= -1;

  return (temp);
}

uint8_t URM37::readEEPROM(uint8_t addr)
{
  if (m_analogMode)
    {
      throw std::runtime_error(string(__FUNCTION__) +
                 ": readEEPROM() is not possible in analog mode");

      return 0;
    }

  if (addr > 0x04)
    throw std::out_of_range(string(__FUNCTION__) +
                            ": addr must be between 0x00-0x04");

  string cmd;
  uint8_t cksum = 0x33 + addr + 0x00;
  cmd.push_back(0x33);
  cmd.push_back(addr);
  cmd.push_back(0x00);
  cmd.push_back(cksum);

  string resp = sendCommand(cmd);

  if (resp.empty())
    {
      throw std::runtime_error(string(__FUNCTION__) +
                               ": sendCommand() failed");
      return 0;
    }

  return resp[2];
}

void URM37::writeEEPROM(uint8_t addr, uint8_t value)
{
  if (m_analogMode)
    {
      throw std::runtime_error(string(__FUNCTION__) +
                 ": writeEEPROM() is not possible in analog mode");

      return;
    }

  if (addr > 0x04)
    throw std::out_of_range(string(__FUNCTION__) +
                            ": addr must be between 0x00-0x04");

  string cmd;
  uint8_t cksum = 0x44 + addr + value;
  cmd.push_back(0x44);
  cmd.push_back(addr);
  cmd.push_back(value);
  cmd.push_back(cksum);

  string resp = sendCommand(cmd);

  if (resp.empty())
    {
      throw std::runtime_error(string(__FUNCTION__) +
                               ": sendCommand() failed");
      return;
    }

  return;
}

string URM37::sendCommand(string cmd)
{
  if (m_analogMode)
    {
      throw std::runtime_error(string(__FUNCTION__) +
                               ": can only be executed in UART mode");

      return "";
    }

  int tries = 0;
  string resp;

  while (tries++ < maxRetries)
    {
      writeDataStr(cmd);
      if (!dataAvailable(waitTimeout))
        {
          cerr << __FUNCTION__ << ": Timed out waiting for response" << endl;
          continue;
        }
      
      resp = readDataStr(8);
      
      // verify size
      if (resp.size() != 4)
        {
          cerr << __FUNCTION__ << ": Invalid returned packet size" << endl;
          continue;
        }
      else
        {
          // we have data, verify cksum, return the response if it's
          // good, retry otherwise
          uint8_t cksum = (uint8_t)(resp[0] + resp[1] + resp[2]);

          if ((uint8_t)resp[3] != cksum)
            {
              cerr << __FUNCTION__ << ": cksum failure" << endl;
              continue;
            }

          // else, we are good to go
          return resp;
        }
    }

  // :(
  return "";
}