~vibhavp/ubuntu/saucy/urg/merge-from-debian

« back to all changes in this revision

Viewing changes to samples/c/convert_2d.c

  • Committer: Bazaar Package Importer
  • Author(s): Albert Huang
  • Date: 2011-05-20 11:33:03 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20110520113303-u8niofzwzcea0osk
Tags: 0.8.12-1
* New upstream release (closes: #624987)
* Add debian/watch file
* Bump standards-version to 3.9.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*!
 
2
  \example convert_2d.c
 
3
 
 
4
  \brief Sample of 2D representation of URG data
 
5
 
 
6
  \author Satofumi KAMIMURA
 
7
 
 
8
  $Id: convert_2d.c 1733 2010-03-06 01:19:49Z satofumi $
 
9
*/
 
10
 
 
11
#include <math.h>
 
12
#include "urg_ctrl.h"
 
13
#include <stdio.h>
 
14
#include <stdlib.h>
 
15
 
 
16
 
 
17
static void urg_exit(urg_t *urg, const char *message)
 
18
{
 
19
  printf("%s: %s\n", message, urg_error(urg));
 
20
  urg_disconnect(urg);
 
21
 
 
22
#ifdef MSC
 
23
  getchar();
 
24
#endif
 
25
  exit(1);
 
26
}
 
27
 
 
28
 
 
29
/*! main */
 
30
int main(int argc, char *argv[])
 
31
{
 
32
#ifdef WINDOWS_OS
 
33
  const char device[] = "COM3"; /* For Windows */
 
34
#else
 
35
  const char device[] = "/dev/ttyACM0"; /* For Linux */
 
36
#endif
 
37
 
 
38
  long *data = NULL;
 
39
  int data_max;
 
40
  int min_length = 0;
 
41
  int max_length = 0;
 
42
  int ret;
 
43
  int n;
 
44
  int i;
 
45
 
 
46
  /* Connection */
 
47
  urg_t urg;
 
48
  urg_initialize(&urg);
 
49
  ret = urg_connect(&urg, device, 115200);
 
50
  if (ret < 0) {
 
51
    urg_exit(&urg, "urg_connect()");
 
52
  }
 
53
 
 
54
  /* Reserve for Reception data */
 
55
  data_max = urg_dataMax(&urg);
 
56
  data = (long*)malloc(sizeof(long) * data_max);
 
57
  if (data == NULL) {
 
58
    perror("data buffer");
 
59
    exit(1);
 
60
  }
 
61
 
 
62
  /* Request for GD data */
 
63
  ret = urg_requestData(&urg, URG_GD, URG_FIRST, URG_LAST);
 
64
  if (ret < 0) {
 
65
    urg_exit(&urg, "urg_requestData()");
 
66
  }
 
67
 
 
68
  /* Reception */
 
69
  n = urg_receiveData(&urg, data, data_max);
 
70
  if (n < 0) {
 
71
    urg_exit(&urg, "urg_receiveData()");
 
72
  }
 
73
 
 
74
  /* Output as 2 dimensional data */
 
75
  /* Consider front of URG as positive direction of X axis */
 
76
  min_length = urg_minDistance(&urg);
 
77
  max_length = urg_maxDistance(&urg);
 
78
  for (i = 0; i < n; ++i) {
 
79
    int x, y;
 
80
    long length = data[i];
 
81
 
 
82
    /* Neglect the out of range values */
 
83
    if ((length <= min_length) || (length >= max_length)) {
 
84
      continue;
 
85
    }
 
86
 
 
87
    x = (int)(length * cos(urg_index2rad(&urg, i)));
 
88
    y = (int)(length * sin(urg_index2rad(&urg, i)));
 
89
 
 
90
    printf("%d\t%d\t# %d, %ld\n", x, y, i, length);
 
91
  }
 
92
 
 
93
  urg_disconnect(&urg);
 
94
  free(data);
 
95
 
 
96
#ifdef MSC
 
97
  getchar();
 
98
#endif
 
99
 
 
100
  return 0;
 
101
}