Skip to content
This repository
tree: e54fcb1871
Fetching contributors…

Cannot retrieve contributors at this time

file 431 lines (401 sloc) 14.998 kb
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 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
using V4l2;
#if (RRAEW)
using rraew;
#endif
[DBus (name = "com.ridgerun.mt9p031Interface")]
public class Ipiped_mt9p031 : AbstcSensor{
    /**
* Create a new instance of a Ipipe daemon
*/
    public Ipiped_mt9p031(){
        /* Initialize private variables */
        this.debug = false;
        this.capture_fd = -1;
        this.owner_capture_fd = 1;
    }

    /**
* Open sensor
* Open the video capture device, obtain the correspondly file
* descriptor */
    private int open_sensor(){
        /* Open the sensor device*/
        this.capture_fd = Posix.open("/dev/video0", Posix.O_RDWR | Posix.O_NONBLOCK, 0);
        if (this.capture_fd < 0){
            Posix.stderr.printf("Failed to open sensor\n");
            return -1;
        }
        this.owner_capture_fd = 0;
        return 0;
    }

   /**
* Sets sensor gain for each color R, Gr, Gb, B (mt9p031)
* Each gain component can range from 1 to 128 in steps of:
* 0.125 if gain between 1 and 4
* 0.250 if gain between 4.25 and 8
* 1.000 if gain betwewn 8 and 128
* @param fd structure with file descriptors and owner flags
* @param r_gain red gain [1, 128]
* @param g_gain green gain [1, 128]
* @param b_gain blue gain [1, 128]
*/
    private int set_sensor_gain_i(int *capture_fd, char *owner_capture_fd,
        uint32 q10r_gain, uint32 q10g_gain, uint32 q10b_gain){

        Control ctrl = Control();
        if (this.capture_fd < 0){
            if (open_sensor() < 0)
                return -1;
        }

        ctrl.id =UserClassControlIds.RED_BALANCE;
        ctrl.value =(int)(q10r_gain >> 7);
        if (-1 == Posix.ioctl(this.capture_fd , VIDIOC_S_CTRL, &ctrl)) {
            if (debug)
                Posix.stderr.printf("ioctl:VIDIOC_S_CTRL failed:red\n");
            return -1;
        }

        ctrl.id =UserClassControlIds.BRIGHTNESS; //V4L2_CID_GREEN1_BALANCE;
        ctrl.value =(int)(q10g_gain >> 7);
        if (-1 == Posix.ioctl(this.capture_fd , VIDIOC_S_CTRL, &ctrl)) {
            if (debug)
                Posix.stderr.printf("ioctl:VIDIOC_S_CTRL failed:green1\n");
            return -1;
        }

        ctrl.id =UserClassControlIds.AUTOGAIN; //V4L2_CID_GREEN2_BALANCE;
        if (-1 == Posix.ioctl(this.capture_fd , VIDIOC_S_CTRL, &ctrl)) {
            if (debug)
                Posix.stderr.printf("ioctl:VIDIOC_S_CTRL failed:green2\n");
            return -1;
        }

        ctrl.id =UserClassControlIds.BLUE_BALANCE;
        ctrl.value =(int)(q10b_gain >> 7);
        if (-1 == Posix.ioctl(this.capture_fd , VIDIOC_S_CTRL, &ctrl)) {
            if (debug)
                Posix.stderr.printf("ioctl:VIDIOC_S_CTRL failed:blue\n");
            return -1;
        }

        if (debug)
            Posix.stdout.printf("Set gain in the sensor r=%f, g=%f ,b=%f\n",
            (float)(q10r_gain)/1024, (float)(q10g_gain)/1024,
            (float)(q10b_gain)/1024);
        return 0;
    }

    /**
* Set sensor gain
* Cast the float RGB gains to fixed-point q3 and use the
* set_sensor_gain_st function to ajust the sensor gains
*/
    public int set_sensor_gain(double r_gain, double g_gain, double b_gain) throws IOError{
        if (this.capture_fd < 0){
            if (open_sensor() < 0)
                return -1;
        }
        uint32 q10r_gain = (uint32)((r_gain + 0.125/2) * 1024);
        uint32 q10g_gain = (uint32)((g_gain + 0.125/2) * 1024);
        uint32 q10b_gain = (uint32)((b_gain+ 0.125/2) * 1024);
        return set_sensor_gain_i(&this.capture_fd, &this.owner_capture_fd,
            q10r_gain, q10g_gain, q10b_gain);
    }

    /**
* Get the combined gain of each color in the sensor registers.
* Each gain component can range from 1 to 128 in steps of:
* 0.125 if gain between 1 and 4
* 0.250 if gain between 4.25 and 8
* 1.000 if gain betwewn 8 and 128
* @param fd structure with file descriptors and owner flags
* @param r_gain pointer to get the red gain
* @param g_gain pointer to get the green gain
* @param b_gain pointer to get the blue gain
*/
     private int get_sensor_gain_i(int *capture_fd, char *owner_capture_fd,
        uint32 *q10r_gain, uint32 *q10g_gain, uint32 *q10b_gain){
        Control ctrl = Control();
        if (this.capture_fd < 0){
            Posix.stderr.printf("Fail get sensor gain\n");
            return -1;
        }
        /* Get Red Gain */
        ctrl.id = UserClassControlIds.RED_BALANCE;
        if (-1 == Posix.ioctl(this.capture_fd , VIDIOC_G_CTRL, &ctrl)){
            if (debug)
                Posix.stderr.printf("ioctl:VIDIOC_G_CTRL failed: get red balance\n");
            return -1;
        }
        *q10r_gain = ctrl.value << 7;
        /* Get Blue Gain */
        ctrl.id = UserClassControlIds.BLUE_BALANCE;
        if (-1 == Posix.ioctl(this.capture_fd , VIDIOC_G_CTRL, &ctrl)){
            if (debug)
                Posix.stderr.printf("ioctl:VIDIOC_G_CTRL failed: get red balance\n");
            return -1;
        }
        *q10b_gain = ctrl.value << 7;
        /* Get Green1 Gain */
        ctrl.id = UserClassControlIds.BRIGHTNESS;
        if (-1 == Posix.ioctl(this.capture_fd , VIDIOC_G_CTRL, &ctrl)){
            if (debug)
                Posix.stderr.printf("ioctl:VIDIOC_G_CTRL failed: get red balance\n");
            return -1;
        }
        *q10g_gain = ctrl.value << 7;
        return 0;
    }
    /**
* Get sensor gain
* Use the get_sensor_gain_i function to obtain the RGB sensor gains
* and print them
*/
    public bool get_sensor_gain(out double _red_gain, out double _green_gain,
        out double _blue_gain) throws IOError{
        uint32 q10red_gain=0,q10green_gain=0,q10blue_gain=0;
        if (this.capture_fd < 0){
            if (open_sensor() < 0)
                return false;
        }

        if (get_sensor_gain_i(&this.capture_fd, &this.owner_capture_fd,
                   &q10red_gain, &q10green_gain, &q10blue_gain) != 0) {
            Posix.stderr.printf("Error:\n Failed to get the sensor gain\n");
            return false;
        } else {
            _red_gain = ((double)(q10red_gain))/1024;
            _green_gain= ((double)(q10green_gain))/1024;
            _blue_gain = ((double)(q10blue_gain))/1024;
            return true;
        }
    }

    /**
* Flip the image vetically
* Change the state of the vertical flip flag to reverse the image's
* rows. When mirroring the rows, the bayer pattern is preserved for
* the pixel order, that means the bayer pattern readout is reversed as the pixels.
* @param state is an on/off flag. If the value is 1 image's columns
* must be reversed, if 0 they must not
*/
    public int sensor_flip_vertically(string state) throws IOError{
        Control ctrl = Control();
        if (this.capture_fd < 0){
            if (open_sensor() < 0)
                return -1;
        }
        if (state == "ON"){
            ctrl.value = 1;
        }else if (state == "OFF"){
            ctrl.value = 0;
        }else {
            if (debug)
                Posix.stderr.printf("Ipiped: Invalid vertical flip state\n");
                return -1;
        }
        ctrl.id = UserClassControlIds.VFLIP;
        if (-1 == Posix.ioctl(this.capture_fd , VIDIOC_S_CTRL, &ctrl)){
            if (debug)
                Posix.stderr.printf("ioctl:VIDIOC_S_CTRL failed:" +
                    "vertical flip\n");
            return -1;
        }
        return 0;
    }

    /**
* Flip the image horizontally
* Change the state of the horizontal flip flag to reverse the image's
* columns. When mirroring the columns, the bayer pattern is preserved
* for the pixel order, that means the bayer pattern readout is reversed
* as the pixels.
* @param state is an on/off flag. If the value is 1 image's columns
* must be reversed, if 0 they must not
*/
    public int sensor_flip_horizontally(string state) throws IOError{
        Control ctrl = Control();

        if (this.capture_fd < 0){
            if (open_sensor() < 0)
                return -1;
        }
        if (state == "ON"){
            ctrl.value = 1;
        }else if (state == "OFF"){
            ctrl.value = 0;
        }else {
            if (debug)
                Posix.stderr.printf("Ipiped: Invalid horizontal flip state\n");
            return -1;
        }
        ctrl.id = UserClassControlIds.HFLIP;
        if (-1 == Posix.ioctl(this.capture_fd , VIDIOC_S_CTRL, &ctrl)){
            if (debug)
                Posix.stderr.printf("ioctl:VIDIOC_S_CTRL failed: " +
                "horizontal flip\n");
            return -1;
        }
        return 0;
    }

    /**
* Sets exposure time
* Sets the effective shutter time that is the integration time for
* the light in the CMOS sensor receptors
* @param fd structure with file descriptors and owner flags
* @param exp_time exposure time in us
*/
    private int set_exposure_time_i(int *capture_fd, char *owner_capture_fd,
        uint32 exp_time){
        Control ctrl = Control();
        ctrl.id = UserClassControlIds.EXPOSURE;
        ctrl.value = (int32)(exp_time)<<8;
        if (-1 == Posix.ioctl(this.capture_fd , VIDIOC_S_CTRL, &ctrl)){
            if (debug)
                Posix.stderr.printf("ioctl:VIDIOC_S_CTRL failed: set exposure time\n");
            return -1;
        }
        if (debug)
            Posix.stdout.printf("Set exposure time to: %u\n", exp_time);
        return 0;
    }

    public int set_exposure_time(uint _exp_time) throws IOError{
        if (this.capture_fd < 0){
            if (open_sensor() < 0)
                return -1;
        }
        return set_exposure_time_i(&this.capture_fd, &this.owner_capture_fd,
            _exp_time);
    }

    /**
* Get exposure time
* Obtain from the sensor the actual ligth integration time
* @param fd structure with file descriptors and owner flags
* @param exp_time pointer where the function leave the exposure time in us
*/
    private int get_exposure_time_i(int *capture_fd, char *owner_capture_fd,
        uint32* exp){
        Control ctrl = Control();
        ctrl.id = UserClassControlIds.EXPOSURE;
        if (-1 == Posix.ioctl(this.capture_fd , VIDIOC_G_CTRL, &ctrl)){
            if (debug == true)
                Posix.stderr.printf("ioctl:VIDIOC_G_CTRL failed: get exposure"+
                " time\n");
            return -1;
        }
        *exp = ctrl.value>>8;
        return 0;
    }


    public bool get_exposure_time(out uint exp_time) throws IOError{
        if (this.capture_fd < 0){
            if (open_sensor() < 0)
                return false;
        }

        if (get_exposure_time_i(&this.capture_fd, &this.owner_capture_fd, &exp_time) != 0) {
            Posix.stderr.printf("Error:\n Failed to get the exposure time\n");
            return false;
        }

        return true;
    }

#if (RRAEW)

    /*Internal methods*/
    /**
* Get the frame vertical orientation (mt9p031)
* @param vf is an on/off flag. If the value is 1 image's rows are
* reversed, if 0 they are not
*/
    private int get_vertical_flip(int *vf){
        Control ctrl = Control();
        if (this.capture_fd < 0){
            if (open_sensor() < 0)
                return -1;
        }
        /* Get vertical flip of the sensor */
        ctrl.id = UserClassControlIds.VFLIP;
        if (-1 == Posix.ioctl(this.capture_fd , VIDIOC_G_CTRL, &ctrl)){
            if (debug)
                Posix.stderr.printf("ioctl:VIDIOC_G_CTRL failed: get vertical flip\n");
            return -1;
        }
        *vf = ctrl.value;
        return 0;
    }

    /**
* Get the frame horizontal orientation (mt9p031)
* @param hf is an on/off flag. If the value is 1 image's columns are
* reversed, if 0 they are not
*/
    private int get_horizontal_flip(int *hf){
        Control ctrl = Control();
        if (this.capture_fd < 0){
            if (open_sensor() < 0)
                return -1;
        }
        /* Get horizontal flip of the sensor */
        ctrl.id = UserClassControlIds.HFLIP;
        if (-1 == Posix.ioctl(this.capture_fd , VIDIOC_G_CTRL, &ctrl)){
            if (debug)
                Posix.stderr.printf("ioctl:VIDIOC_G_CTRL failed: get vertical flip\n");
            return -1;
        }
        *hf = ctrl.value;
        return 0;
    }

    /**
* Get the pixel position for each color pattern
* @param color_pattern, array that contains the pixel position
* of R, Gr, Gb and B
*/
    private bool get_color_pattern(RraewColorPattern color_pattern){
        int vf=0, hf=0;
        int ret = 0;

        /* Get the frame orientation */
        ret = get_vertical_flip(&vf);
        ret += get_horizontal_flip(&hf);
        if (ret != 0) {
            if (debug)
                Posix.stderr.printf ("Error:\n Failed to get frame orientation\n");
            return false;
        }
        /* Define pixels order */
        if (vf==1 && hf==1){
            /* Pattern:
* Gb B
* R Gr */
            color_pattern = colorptn_GbBRGr;
        } else if (vf==0 && hf ==1){
            /* Pattern:
* R Gr
* Gb B */
            color_pattern = colorptn_RGrGbB;
        } else if (vf==1 && hf ==0){
            /* Pattern:
* B Gb
* Gr R*/
            color_pattern = colorptn_BGbGrR;
        } else {
            /* Pattern:
* Gr R
* B Gb */
            color_pattern = colorptn_GrRBGb;
        }
        return true;
    }

    public override int get_sensor_data(RraewSensor *sensor){
        RraewColorPattern colorptn = RraewColorPattern();
        if (!get_color_pattern (colorptn)){
            Posix.stderr.printf ("Error:\n Failed to get color pattern\n");
            return -1;
        }
        sensor.colorptn = colorptn;
        sensor.max_exp_time = 0x7fffffff;
        sensor.min_exp_time = 1;
        sensor.max_gain = 32; // Use only analog gain
        sensor.min_gain = 1;
        sensor.n_gain_steps = 3;
        sensor.gain_steps = (RraewGainStep*)malloc(sensor.n_gain_steps*sizeof(RraewGainStep));
        sensor.gain_steps[0].range_end = 4;
        sensor.gain_steps[0].step_n = 1;
        sensor.gain_steps[0].step_d = 8;

        sensor.gain_steps[1].range_end = 8;
        sensor.gain_steps[1].step_n = 1;
        sensor.gain_steps[1].step_d = 4;

        sensor.gain_steps[2].range_end = 128;
        sensor.gain_steps[2].step_n = 1;
        sensor.gain_steps[2].step_d = 1;

        sensor.set_gain = set_sensor_gain_i;
        sensor.get_gain = get_sensor_gain_i;
        sensor.set_exposure = set_exposure_time_i;
        sensor.get_exposure = get_exposure_time_i;
        return 0;
    }
#endif
}
Something went wrong with that request. Please try again.