40 #define VMODEDEF(width, height, fps, hblank, vblank) { #width"x"#height"x"#fps, width, height, fps, hblank, vblank } 98 reg_column_start_ = 0x01;
99 reg_row_start_ = 0x02;
100 reg_window_width_ = 0x04;
101 reg_window_height_ = 0x03;
104 reg_shutter_width_ = 0x0B;
105 reg_max_shutter_width_ = 0xBD;
106 reg_analog_gain_ = 0x35;
107 reg_agc_aec_enable_ = 0xAF;
108 reg_read_mode_ = 0x0D;
109 reg_companding_mode_ = 0x1C;
110 reg_desired_bin_ = 0xA5;
112 agc_aec_enable_shift_ = 0;
113 companding_mode_shift_ = 0;
115 max_max_shutter_width_ = 2047;
116 max_shutter_width_ = 32767;
118 read_mode_cache_ = 0x300;
120 *agc_aec_mode_cache_ = 3;
122 *companding_mode_cache_ = 2;
128 ROS_WARN(
"MT9VImager::getInstance Unable to read imager version.");
141 uint16_t val = (agc ? 2 : 0) | (aec ? 1 : 0);
144 *agc_aec_mode_cache_ = (*agc_aec_mode_cache_ & ~mask) | val;
148 ROS_WARN(
"Error setting AGC/AEC mode. Exposure and gain may be incorrect.");
158 ROS_WARN(
"Error setting brightness.");
168 ROS_WARN(
"Error setting analog gain.");
178 ROS_ERROR(
"setExposure called before setMode in class MT9VImager. This is a bug.");
184 ROS_WARN(
"Requested exposure too short, setting to %f s", line_time_);
187 if (explines > max_shutter_width_)
190 ROS_WARN(
"Requested exposure too long, setting to %f s", explines * line_time_);
192 ROS_DEBUG(
"Setting exposure lines to %i (%f s).", explines, explines * line_time_);
195 ROS_WARN(
"Error setting exposure.");
205 ROS_ERROR(
"setMaxExposure called before setMode in class MT9VImager. This is a bug.");
211 ROS_WARN(
"Requested max exposure too short, setting to %f s", line_time_);
214 if (explines > max_max_shutter_width_)
217 ROS_WARN(
"Requested max exposure too long, setting to %f s", explines * line_time_);
219 ROS_DEBUG(
"Setting max exposure lines to %i (%f s).", explines, explines * line_time_);
222 ROS_WARN(
"Error setting max exposure.");
230 uint16_t mask = 0x30;
231 uint16_t val = (mirrory ? 0x10 : 0) | (mirrorx ? 0x20 : 0);
233 read_mode_cache_ = (read_mode_cache_ & ~mask) | val;
237 ROS_WARN(
"Error setting mirror mode. Read mode register settings may have been corrupted.");
243 virtual bool setBlackLevel(
bool manual_override,
int calibration_value,
int step_size,
int filter_length)
249 ROS_WARN(
"Error setting black level correction mode.");
256 virtual bool setMode(
int x,
int y,
int binx,
int biny,
double rate,
int xoffset,
int yoffset)
258 if (binx < 0 || binx > 2)
260 ROS_ERROR(
"x binning should 0, 1 or 2, not %i", binx);
264 if (biny < 0 || biny > 2)
266 ROS_ERROR(
"y binning should 0, 1 or 2, not %i", biny);
273 if (x < 1 || x > 752)
275 ROS_ERROR(
"Requested x resolution (includes binning) %i is not between 1 and 752.", x);
279 if (y < 1 || y > 480)
281 ROS_ERROR(
"Requested y resolution (includes binning) %i is not between 1 and 480.", y);
290 startx = 57 + xoffset;
291 starty = 4 + yoffset;
293 if (startx < 1 || startx + x > 752 + 1)
295 ROS_ERROR(
"Image x offset exceeds imager bounds.");
299 if (starty < 4 || starty + y > 480 + 4)
301 ROS_ERROR(
"Image y offset exceeds imager bounds.");
309 int target_tics = clock / rate;
310 int besterror = INT_MAX;
311 int packet_size = 70 + (x >> binx);
312 double max_peak_data_rate = 90e6;
313 double best_peak_data_rate = 0;
315 ROS_DEBUG(
"Target tics %i", target_tics);
317 for (
int hblank = 100; hblank < 1023; hblank++)
319 int htics = x + hblank;
321 int vblank = round(((
double) target_tics - extra_tics) / htics) - y;
326 int vtics = vblank + y;
328 int tics = htics * vtics + extra_tics;
329 int err = abs(tics - target_tics);
330 double peak_data_rate = 8 * packet_size * clock / htics;
332 if (peak_data_rate <= max_peak_data_rate && err < besterror)
338 best_peak_data_rate = peak_data_rate;
342 if (besterror == INT_MAX)
344 ROS_ERROR(
"Could not find suitable vblank and hblank for %ix%i mode.\n", x, y);
348 line_time_ = (besthblank + x) / clock;
350 ROS_DEBUG(
"Selected vblank=%i hblank=%i data_rate=%f err=%f period=%f", bestvblank, besthblank, best_peak_data_rate, besterror / clock, best_tics / clock);
354 ROS_ERROR(
"Failed to set vertical blanking. Stream will probably be corrupt.");
360 ROS_ERROR(
"Failed to set horizontal blanking. Stream will probably be corrupt.");
366 ROS_ERROR(
"Failed to set start column. Stream will probably be corrupt.");
372 ROS_ERROR(
"Failed to set start row. Stream will probably be corrupt.");
378 ROS_ERROR(
"Failed to set image width. Stream will probably be corrupt.");
384 ROS_ERROR(
"Failed to set image height. Stream will probably be corrupt.");
388 uint16_t mask = 0x0F;
389 uint16_t val = (binx << 2) | biny;
391 read_mode_cache_ = (read_mode_cache_ & ~mask) | val;
395 ROS_ERROR(
"Failed to set binning modes. Read mode may be corrupted.");
405 uint16_t val = (activated ? 3 : 2) << companding_mode_shift_;
406 *companding_mode_cache_ = (*companding_mode_cache_ & ~mask) | val;
410 ROS_WARN(
"Error setting companding mode.");
479 ROS_WARN(
"Error setting magic sensor register.");
483 ROS_WARN(
"Error turning off row-noise correction.");
487 ROS_WARN(
"Error setting V2 Voltge level for context B.");
491 ROS_WARN(
"Error turning off high dynamic range for alternate configuration.");
495 ROS_WARN(
"Error turning on exposure knee auto-adjust for alternate configuration.");
516 ROS_WARN(
"Error setting magic sensor register.");
520 ROS_WARN(
"Error turning off row-noise correction");
531 uint16_t imager_version;
534 ROS_ERROR(
"MT9VImager::getInstance Unable to read imager version.");
538 switch (imager_version)
544 ROS_ERROR(
"MT9VImager::getInstance Unknown imager version 0x%04x. Assuming it is compatible with MT9V032.", imager_version);
virtual bool setMaxExposure(double exp)
uint16_t read_mode_cache_
const struct MT9VMode MT9VModes[MT9V_NUM_MODES]
MT9VImagerImpl(IpCamList &cam)
MT9V034Alternate(IpCamList &cam, MT9V034 *parent)
uint8_t reg_shutter_width_
uint8_t reg_companding_mode_
virtual bool setGain(int gain)
uint16_t * companding_mode_cache_
int max_max_shutter_width_
uint16_t companding_mode_cache_backing_
uint8_t reg_column_start_
int wge100ReliableSensorRead(const IpCamList *camInfo, uint8_t reg, uint16_t *data, int *retries)
int wge100ReliableSensorWrite(const IpCamList *camInfo, uint8_t reg, uint16_t data, int *retries)
virtual bool setCompanding(bool activated)
#define VMODEDEF(width, height, fps, hblank, vblank)
virtual bool setMode(int x, int y, int binx, int biny, double rate, int xoffset, int yoffset)
uint16_t agc_aec_mode_cache_backing_
virtual MT9VImagerPtr getAlternateContext()
int agc_aec_enable_shift_
virtual bool setExposure(double exp)
virtual ~MT9VImagerImpl()
virtual uint16_t getVersion()
virtual bool setBrightness(int brightness)
boost::shared_ptr< MT9VImager > MT9VImagerPtr
int companding_mode_shift_
virtual bool setMirror(bool mirrorx, bool mirrory)
uint16_t * agc_aec_mode_cache_
uint8_t reg_agc_aec_enable_
virtual bool setBlackLevel(bool manual_override, int calibration_value, int step_size, int filter_length)
static MT9VImagerPtr getInstance(IpCamList &cam)
uint8_t reg_window_height_
uint8_t reg_max_shutter_width_
virtual std::string getModel()
uint8_t reg_window_width_
virtual bool setAgcAec(bool agc, bool aec)