set_calibration.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * * Software License Agreement (BSD License)
3  * *
4  * * Copyright (c) 2009-2010, Willow Garage, Inc.
5  * * All rights reserved.
6  * *
7  * * Redistribution and use in source and binary forms, with or without
8  * * modification, are permitted provided that the following conditions
9  * * are met:
10  * *
11  * * * Redistributions of source code must retain the above copyright
12  * * notice, this list of conditions and the following disclaimer.
13  * * * Redistributions in binary form must reproduce the above
14  * * copyright notice, this list of conditions and the following
15  * * disclaimer in the documentation and/or other materials provided
16  * * with the distribution.
17  * * * Neither the name of the Willow Garage nor the names of its
18  * * contributors may be used to endorse or promote products derived
19  * * from this software without specific prior written permission.
20  * *
21  * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * * POSSIBILITY OF SUCH DAMAGE.
33  * *********************************************************************/
34 
35 // Author: Blaise Gassend
36 
37 #include <assert.h>
38 #include <iostream>
39 #include <fstream>
40 
41 #include <string.h> // for memset(3)
42 #include <stdlib.h> // for atoi(3)
43 
44 #include <ros/console.h>
45 #include <ros/time.h>
46 
50 
51 uint16_t checksum(uint16_t *data)
52 {
53  uint16_t sum = 0;
54  for (int i = 0; i < FLASH_PAGE_SIZE; i++)
55  sum += htons(data[i]);
56  return htons(0xFFFF - sum);
57 }
58 
60 {
61  uint8_t calbuff[2 * FLASH_PAGE_SIZE];
62 
63  fprintf(stderr, "Reading old calibration...\n");
64  if(wge100ReliableFlashRead(camera, FLASH_CALIBRATION_PAGENO, (uint8_t *) calbuff, NULL) != 0 ||
65  wge100ReliableFlashRead(camera, FLASH_CALIBRATION_PAGENO+1, (uint8_t *) calbuff+FLASH_PAGE_SIZE, NULL) != 0)
66  {
67  fprintf(stderr, "Flash read error. Aborting.\n");
68  return -2;
69  }
70 
71  uint16_t chk = checksum((uint16_t *) calbuff);
72  if (chk)
73  {
74  fprintf(stderr, "Previous camera calibration had bad checksum.\n");
75  }
76  else
77  {
78  calbuff[sizeof(calbuff) - 2] = 0; // Make sure it is zero-terminated.
79  printf("%s\n", calbuff);
80  }
81 
82  return 0;
83 }
84 
85 int write_calibration(IpCamList *camera, char *filename)
86 {
87  uint8_t calbuff[FLASH_PAGE_SIZE * 2];
88  bzero(calbuff, sizeof(calbuff));
89 
90  fprintf(stderr, "\nWriting new calibration...\n");
91 
92  FILE *f;
93  if (strcmp(filename, "-"))
94  f = fopen(filename, "r");
95  else
96  {
97  fprintf(stderr, "Enter new calibration information on standard input.\n");
98  f = stdin;
99  }
100  if (!f)
101  {
102  fprintf(stderr, "Unable to open file %s.\n", filename);
103  return -1;
104  }
105  int maxsize = sizeof(calbuff) - sizeof(uint16_t) - 1;
106  int bytesread = fread(calbuff, 1, maxsize + 1, f);
107  fclose(f);
108  if (bytesread > maxsize)
109  {
110  fprintf(stderr, "File %s is too long. At most %i bytes can be stored.\n", filename, maxsize);
111  return -1;
112  }
113  calbuff[bytesread] = 0; // Make sure we zero-terminate the data.
114 
115  ((uint16_t *) calbuff)[FLASH_PAGE_SIZE - 1] = 0;
116  ((uint16_t *) calbuff)[FLASH_PAGE_SIZE - 1] = checksum((uint16_t *) calbuff);
117 
118  if (wge100ReliableFlashWrite(camera, FLASH_CALIBRATION_PAGENO, (uint8_t *) calbuff, NULL) != 0 ||
119  wge100ReliableFlashWrite(camera, FLASH_CALIBRATION_PAGENO+1, (uint8_t *) calbuff+FLASH_PAGE_SIZE, NULL) != 0)
120  {
121  fprintf(stderr, "Flash write error. The camera calibration is an undetermined state.\n");
122  return -2;
123  }
124 
125  fprintf(stderr, "Success!\n");
126  return 0;
127 }
128 
129 int clear_calibration(IpCamList *camera, char *filename)
130 {
131  uint8_t calbuff[FLASH_PAGE_SIZE * 2];
132  bzero(calbuff, sizeof(calbuff));
133 
134  fprintf(stderr, "\nClearing calibration...\n");
135 
136  if (wge100ReliableFlashWrite(camera, FLASH_CALIBRATION_PAGENO, (uint8_t *) calbuff, NULL) != 0 ||
137  wge100ReliableFlashWrite(camera, FLASH_CALIBRATION_PAGENO+1, (uint8_t *) calbuff+FLASH_PAGE_SIZE, NULL) != 0)
138  {
139  fprintf(stderr, "Flash write error. The camera calibration is an undetermined state.\n");
140  return -2;
141  }
142 
143  fprintf(stderr, "Success!\n");
144  return 0;
145 }
146 
147 int main(int argc, char **argv)
148 {
149  if ((argc != 3 && argc != 2) || !strcmp(argv[1], "--help")) {
150  fprintf(stderr, "Usage: %s <camera_url> <calibration_file> # Sets the camera calibration information\n", argv[0]);
151  fprintf(stderr, " %s <camera_url> - # Sets the camera calibration information from stdin\n", argv[0]);
152  fprintf(stderr, " %s <camera_url> --invalidate # Invalidates the camera calibration information\n", argv[0]);
153  fprintf(stderr, " %s <camera_url> # Reads the camera calibration information\n", argv[0]);
154  fprintf(stderr, "\nReads or writes the camera calibration information stored on the camera's flash.\n");
155  return -1;
156  }
157 
158  char *camera_url = argv[1];
159 
160  // Find the camera matching the URL
161  IpCamList camera;
162  const char *errmsg;
163  int outval = wge100FindByUrl(camera_url, &camera, SEC_TO_USEC(0.1), &errmsg);
164  if (outval)
165  {
166  fprintf(stderr, "Matching URL %s : %s\n", camera_url, errmsg);
167  return -1;
168  }
169 
170  // Configure the camera with its IP address, wait up to 500ms for completion
171  outval = wge100Configure(&camera, camera.ip_str, SEC_TO_USEC(0.5));
172  if (outval != 0) {
173  if (outval == ERR_CONFIG_ARPFAIL) {
174  fprintf(stderr, "Unable to create ARP entry (are you root?), continuing anyway\n");
175  } else {
176  fprintf(stderr, "IP address configuration failed\n");
177  return -1;
178  }
179  }
180 
181  outval = read_calibration(&camera);
182  if (outval)
183  return outval;
184 
185  if (argc != 3)
186  return 0;
187 
188  char *filename = argv[2];
189 
190  if (strcmp(filename, "--invalidate"))
191  return write_calibration(&camera, filename);
192  else
193  return clear_calibration(&camera, filename);
194 
195 }
#define FLASH_PAGE_SIZE
Definition: ipcam_packet.h:60
f
int wge100ReliableFlashRead(const IpCamList *camInfo, uint32_t address, uint8_t *pageDataOut, int *retries)
Definition: wge100lib.c:906
int wge100ReliableFlashWrite(const IpCamList *camInfo, uint32_t address, const uint8_t *pageDataIn, int *retries)
Definition: wge100lib.c:1014
int wge100Configure(IpCamList *camInfo, const char *ipAddress, unsigned wait_us)
Definition: wge100lib.c:532
int read_calibration(IpCamList *camera)
#define ERR_CONFIG_ARPFAIL
Definition: wge100lib.h:116
uint16_t checksum(uint16_t *data)
int main(int argc, char **argv)
#define SEC_TO_USEC(sec)
Definition: host_netutil.h:65
int wge100FindByUrl(const char *url, IpCamList *camera, unsigned wait_us, const char **errmsg)
Definition: wge100lib.c:94
char ip_str[16]
Definition: list.h:296
int clear_calibration(IpCamList *camera, char *filename)
int write_calibration(IpCamList *camera, char *filename)
#define FLASH_CALIBRATION_PAGENO
Definition: ipcam_packet.h:63


wge100_camera
Author(s): Blaise Gassend, Patrick Mihelich, Eric MacIntosh, David Palchak
autogenerated on Mon Jun 10 2019 15:44:15