37 #include <sys/socket.h> 38 #include <netinet/in.h> 39 #include <sys/types.h> 40 #include <arpa/inet.h> 67 ROS_ERROR(
"Error creating/binding socket: %s", strerror(errno));
121 imager_registers_[i] = 0;
124 uint8_t supported_registers[] = { 0x7F, 0x06, 0x05, 0x04, 0x03, 0 };
125 uint16_t init_value[] = { 0 , 0x2D, 0x5E, 0x2F0, 0x1E0, 0x1311 };
126 for (
unsigned int i = 0; i <
sizeof(supported_registers) /
sizeof(*supported_registers); i++)
129 imager_registers_[supported_registers[i]] = init_value[i];
132 uint8_t tolerated_registers[] = { 0x18, 0x15, 0x20, 0x21, 0x01, 0x02, 0x0D, 0x70, 0x71, 0x1C, 0xAF, 0x0B, 0x47, 0x48, 0x4C, 0xA5, 0x35, 0xBD };
134 for (
unsigned int i = 0; i <
sizeof(tolerated_registers) /
sizeof(*tolerated_registers); i++)
135 imager_register_flags_[tolerated_registers[i]] &= ~IMAGER_REGISTER_UNSUPPORTED;
140 if ((x + 1) / 2 + y < 500)
155 if (width_ != imager_registers_[0x04])
157 ROS_ERROR(
"Width mismatch %i reg: %i", width_, imager_registers_[0x04]);
162 if (height_ != imager_registers_[0x03])
164 ROS_ERROR(
"Height mismatch %i reg: %i", height_, imager_registers_[0x03]);
170 ros::Duration period =
ros::Duration(((imager_registers_[0x03] + imager_registers_[0x06]) * (imager_registers_[0x04] + imager_registers_[0x05]) + 4) / 16e6);
172 if (sleeptime < -period)
175 ROS_WARN(
"Simulator lost lock on frame #%i", frame_);
180 next_frame_time_ += period;
189 for (
int y = 0; y <
height_; y++)
191 if ((imager_registers_[0x7F] & 0x3C00) == 0x3800)
194 for (
int x = 0; x <
width_; x++)
197 for (
int x = 0; x <
width_; x++)
202 for (
int x = 0; x <
width_; x++)
203 pvl.
data[x] = x + y + frame_;
207 sendto(socket_, &pvl,
sizeof(pvl.
header) + width_, 0,
208 (
struct sockaddr *) &data_addr_,
sizeof(data_addr_));
212 ROS_INFO(
"Sending frame #%i", frame_);
220 sendto(socket_, &peof,
sizeof(peof), 0,
221 (
struct sockaddr *) &data_addr_,
sizeof(data_addr_));
223 frame_ = (frame_ + 1) & 0xFFFF;
228 unsigned char buff[100];
232 ssize_t len = recv(socket_, buff, 100, MSG_DONTWAIT);
238 if (len == 1 && buff[0] == 0xFF)
244 ROS_INFO(
"Got a packet with a bad magic number or type."); \
248 #define CAST_AND_CHK(type) \ 249 if (len != sizeof(type)) \ 251 ROS_INFO("Got a "#type" with incorrect length"); \ 254 if (false) ROS_INFO("Got a "#type""); \ 255 type *pkt = (type *) buff; 258 switch (ntohl(hdr->
type))
271 pkt->ser_no != htonl(serial_no_))
281 unsigned int new_mode = htons(pkt->trig_state);
282 if (new_mode == 0 && !running_)
286 ROS_ERROR(
"Tried to set to trigger mode %i.", new_mode);
295 uint32_t mode = htonl(pkt->mode);
296 if (mode >= 10 || running_)
307 imager_registers_[0x03] =
height_;
308 imager_registers_[0x04] =
width_;
318 unsigned int new_width = htons(pkt->horizontal);
319 unsigned int new_height = htons(pkt->vertical);
320 if (new_width > 752 || new_height > 640)
322 ROS_ERROR(
"Tried to set resolution to out of range %ix%i", new_width, new_height);
328 height_ = new_height;
341 bzero(&data_addr_,
sizeof(data_addr_));
342 data_addr_.sin_family = AF_INET;
343 data_addr_.sin_addr.s_addr = pkt->receiver.addr;
344 data_addr_.sin_port = pkt->receiver.port;
370 if (imager_register_flags_[pkt->address] & IMAGER_REGISTER_UNDEFINED)
371 ROS_WARN(
"Reading uninitialized imager register 0x%02X.", pkt->address);
372 if (imager_register_flags_[pkt->address] & IMAGER_REGISTER_UNSUPPORTED)
373 ROS_WARN(
"Reading unsupported imager register 0x%02X.", pkt->address);
374 sendSensorData(&pkt->hdr, pkt->address, imager_registers_[pkt->address]);
381 if (imager_register_flags_[pkt->address] & IMAGER_REGISTER_UNSUPPORTED)
382 ROS_ERROR(
"Writing unsupported imager register 0x%02X.", pkt->address);
383 imager_register_flags_[pkt->address] &= ~IMAGER_REGISTER_UNDEFINED;
384 imager_registers_[pkt->address] = ntohs(pkt->data);
397 ROS_ERROR(
"Got an unknown message type: %i", ntohl(hdr->
type));
404 #define FILL_HDR(pkttype, pktid) \ 405 if (false) ROS_INFO("Sending a "#pkttype" packet."); \ 406 Packet##pkttype rsp; \ 407 rsp.hdr.magic_no = htonl(WG_MAGIC_NO); \ 408 strncpy(rsp.hdr.hrt, #pkttype, sizeof(rsp.hdr.hrt)); \ 409 rsp.hdr.type = htonl(pktid); 413 sockaddr_in rsp_addr; \ 414 bzero(&rsp_addr, sizeof(rsp_addr)); \ 415 rsp_addr.sin_family = AF_INET; \ 416 rsp_addr.sin_addr.s_addr = hdr->reply_to.addr; \ 417 rsp_addr.sin_port = hdr->reply_to.port; \ 418 sendto(socket_, &rsp, sizeof(rsp), 0, \ 419 (struct sockaddr *) &rsp_addr, sizeof(rsp_addr)); 424 bzero(rsp.mac,
sizeof(rsp.mac));
426 rsp.ser_no = htonl(serial_no_);
427 strncpy(rsp.product_name,
"WGE100_camera_simulator.",
sizeof(rsp.product_name));
428 rsp.hw_version = htonl(0x2041);
429 rsp.fw_version = htonl(0x0112);
430 strncpy(rsp.camera_name,
"WGE100_camera_simulator.",
sizeof(rsp.camera_name));
439 rsp.status_type = htonl(type);
440 rsp.status_code = htonl(code);
447 rsp.address = address;
448 bzero(rsp.data,
sizeof(rsp.data));
456 rsp.data = htons(value);
472 int main(
int argc,
char **argv)
476 return wge100sim.
run();
int wge100SocketCreate(const struct in_addr *addr, uint16_t port)
uint8_t imager_register_flags_[NUM_IMAGER_REGISTERS]
#define FILL_HDR(pkttype, pktid)
uint32_t type
The packet type (see list of packet types, above)
void sendFlashData(PacketGeneric *hdr, uint32_t address)
int main(int argc, char **argv)
const struct MT9VMode MT9VModes[MT9V_NUM_MODES]
static const int NUM_IMAGER_REGISTERS
WGE100Simulator(uint32_t serial)
void sendAnnounce(PacketGeneric *hdr)
HeaderVideoLine header
Standard video line header.
uint8_t data[FLASH_PAGE_SIZE]
32-bit address as specified in the PacketFlashPayload definition
uint16_t line_number
Frame/line number as reported by Imager peripheral.
static void setExiting(int i)
#define PKT_ERROR_INVALID
Packet is not valid in this mode.
#define CAST_AND_CHK(type)
static const int IMAGER_REGISTER_UNSUPPORTED
static volatile bool exiting_
void sendStatus(PacketGeneric *hdr, uint32_t type, uint32_t code)
#define PKT_STATUST_ERROR
Command could not be completed. See status_code for details.
#define PKT_STATUST_OK
Command completed successfully.
uint32_t frame_number
Frame number as reported by Imager peripheral.
uint16_t vert_resolution
Number of video line packets per frame (not including EOF packet)
uint16_t imager_registers_[NUM_IMAGER_REGISTERS]
uint16_t horiz_resolution
Number of 8-bit pixels per video line.
static uint8_t get_diag_test_pattern(int x, int y)
void sendSensorData(PacketGeneric *hdr, uint8_t addr, uint16_t value)
ros::Time next_frame_time_
uint32_t magic_no
The Willow Garage Magic number (always WG_MAGIC_NO)
#define CONFIG_PRODUCT_ID
static const int IMAGER_REGISTER_UNDEFINED
#define IMAGER_LINENO_EOF
Flags this video packet as being an normal End Of Frame packet.
uint32_t i2c_valid
Flags that indicate which 'i2c' values were updated during the previous frame.
uint16_t i2c[I2C_REGS_PER_FRAME]
Storage for I2C values read during the frame.