• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

kstars

  • sources
  • kde-4.12
  • kdeedu
  • kstars
  • kstars
  • ekos
  • guide
gmath.cpp
Go to the documentation of this file.
1 /* Ekos guide tool
2  Copyright (C) 2012 Andrew Stepanenko
3 
4  Modified by Jasem Mutlaq <mutlaqja@ikarustech.com> for KStars.
5 
6  This application is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10  */
11 
12 #include "gmath.h"
13 
14 #include <math.h>
15 #include <string.h>
16 
17 #include "vect.h"
18 #include "matr.h"
19 
20 #include "fitsviewer/fitsview.h"
21 
22 #define DEF_SQR_0 (16-0)
23 #define DEF_SQR_1 (32-0)
24 #define DEF_SQR_2 (64-0)
25 #define DEF_SQR_3 (128-0)
26 
27 //#define GUIDE_LOG
28 
29 const guide_square_t guide_squares[] = { {DEF_SQR_0, DEF_SQR_0*DEF_SQR_0*1.0},
30  {DEF_SQR_1, DEF_SQR_1*DEF_SQR_1*1.0},
31  {DEF_SQR_2, DEF_SQR_2*DEF_SQR_2*1.0},
32  {DEF_SQR_3, DEF_SQR_3*DEF_SQR_3*1.0},
33  {-1, -1}
34  };
35 
36 
37 const square_alg_t guide_square_alg[] = {
38  { SMART_THRESHOLD, "Smart" },
39  { CENTROID_THRESHOLD, "Fast"},
40  { AUTO_THRESHOLD, "Auto" },
41  { NO_THRESHOLD, "No thresh." },
42  { -1, {0} }
43  };
44 
45 cgmath::cgmath()
46 {
47  // sys...
48  ticks = 0;
49  pdata = NULL;
50  video_width = -1;
51  video_height = -1;
52  ccd_pixel_width = 0;
53  ccd_pixel_height = 0;
54  focal = 0;
55  aperture = 0;
56  ROT_Z = Matrix(0);
57  preview_mode = true;
58  suspended = false;
59  lost_star = false;
60  pimage = NULL;
61 
62  // square variables
63  square_idx = DEFAULT_SQR;
64  square_alg_idx = SMART_THRESHOLD;
65  square_size = guide_squares[square_idx].size;
66  square_square = guide_squares[square_idx].square;
67  square_pos = Vector(0);
68 
69  // sky coord. system vars.
70  star_pos = Vector(0);
71  scr_star_pos = Vector(0);
72  reticle_pos = Vector(0);
73  reticle_orts[0] = Vector(0);
74  reticle_orts[1] = Vector(0);
75  reticle_angle = 0;
76 
77  // overlays
78  memset( &overlays, 0, sizeof(overlays) );
79 
80  // processing
81  in_params.reset();
82  out_params.reset();
83  channel_ticks[GUIDE_RA] = channel_ticks[GUIDE_DEC] = 0;
84  accum_ticks[GUIDE_RA] = accum_ticks[GUIDE_DEC] = 0;
85  drift[GUIDE_RA] = new double[MAX_ACCUM_CNT];
86  drift[GUIDE_DEC] = new double[MAX_ACCUM_CNT];
87  memset( drift[GUIDE_RA], 0, sizeof(double)*MAX_ACCUM_CNT );
88  memset( drift[GUIDE_DEC], 0, sizeof(double)*MAX_ACCUM_CNT );
89  drift_integral[GUIDE_RA] = drift_integral[GUIDE_DEC] = 0;
90 
91 
92  // statistics
93  do_statistics = true;
94  sum = sqr_sum = 0;
95  delta_prev = sigma_prev = sigma = 0;
96 }
97 
98 cgmath::~cgmath()
99 {
100  delete [] drift[GUIDE_RA];
101  delete [] drift[GUIDE_DEC];
102 
103 
104 }
105 
106 
107 bool cgmath::set_video_params( int vid_wd, int vid_ht )
108 {
109  if( vid_wd <= 0 || vid_ht <= 0 )
110  return false;
111 
112  video_width = vid_wd;
113  video_height = vid_ht;
114 
115  //set_reticle_params( video_width/2, video_height/2, -1 ); // keep orientation
116 
117  return true;
118 }
119 
120 void cgmath::set_buffer(float *buffer)
121 {
122  pdata = buffer;
123 }
124 
125 void cgmath::set_image(FITSView *image)
126 {
127  pimage = image;
128 
129  FITSImage *image_data = pimage->getImageData();
130 
131  set_buffer(image_data->getImageBuffer());
132  set_video_params(image_data->getWidth(), image_data->getHeight());
133 }
134 
135 float *cgmath::get_data_buffer( int *width, int *height, int *length, int *size )
136 {
137  if( width )
138  *width = video_width;
139  if( height )
140  *height = video_height;
141  if( length )
142  *length = video_width * video_height;
143  if( size )
144  *size = video_width * video_height * sizeof(float);
145 
146  return pdata;
147 }
148 
149 
150 bool cgmath::set_guider_params( double ccd_pix_wd, double ccd_pix_ht, double guider_aperture, double guider_focal )
151 {
152  if( ccd_pix_wd < 0 )
153  ccd_pix_wd = 0;
154  if( ccd_pix_ht < 0 )
155  ccd_pix_ht = 0;
156  if( guider_focal <= 0 )
157  guider_focal = 1;
158 
159  ccd_pixel_width = ccd_pix_wd / 1000.0; // from mkm to mm
160  ccd_pixel_height = ccd_pix_ht / 1000.0; // from mkm to mm
161  aperture = guider_aperture;
162  focal = guider_focal;
163 
164  return true;
165 }
166 
167 void cgmath::get_guider_params( double *ccd_pix_wd, double *ccd_pix_ht, double *guider_aperture, double *guider_focal )
168 {
169  *ccd_pix_wd = ccd_pixel_width * 1000.0;
170  *ccd_pix_ht = ccd_pixel_height * 1000.0;
171  *guider_aperture = aperture;
172  *guider_focal = focal;
173 }
174 
175 
176 bool cgmath::set_reticle_params( double x, double y, double ang )
177 {
178  Vector ort;
179 
180  // check frame ranges
181  if( x < 0 )
182  x = 0;
183  if( y < 0 )
184  y = 0;
185  if( x >= (double)video_width-1 )
186  x = (double)video_width-1;
187  if( y >= (double)video_height-1 )
188  y = (double)video_height-1;
189 
190  reticle_pos = Vector( x, y, 0 );
191 
192  if( ang >= 0)
193  reticle_angle = ang;
194 
195  ROT_Z = RotateZ( -M_PI*reticle_angle/180.0 ); // NOTE!!! sing '-' derotates star coordinate system
196 
197  reticle_orts[0] = Vector(1, 0, 0) * 100;
198  reticle_orts[1] = Vector(0, 1, 0) * 100;
199 
200  reticle_orts[0] = reticle_orts[0] * ROT_Z;
201  reticle_orts[1] = reticle_orts[1] * ROT_Z;
202 
203  // lets position static overlay
204  overlays.reticle_axis_ra[0].x = reticle_pos.x;
205  overlays.reticle_axis_ra[0].y = reticle_pos.y;
206  overlays.reticle_axis_ra[1].x = reticle_pos.x + reticle_orts[0].x;
207  overlays.reticle_axis_ra[1].y = reticle_pos.y + reticle_orts[0].y;
208 
209  overlays.reticle_axis_dec[0].x = reticle_pos.x;
210  overlays.reticle_axis_dec[0].y = reticle_pos.y;
211  overlays.reticle_axis_dec[1].x = reticle_pos.x - reticle_orts[1].x;
212  overlays.reticle_axis_dec[1].y = reticle_pos.y - reticle_orts[1].y; // invert y-axis
213 
214  overlays.reticle_pos.x = reticle_pos.x;
215  overlays.reticle_pos.y = reticle_pos.y;
216 
217  if (pimage)
218  pimage->setGuideSquare(reticle_pos.x, reticle_pos.y);
219 
220  return true;
221 }
222 
223 
224 bool cgmath::get_reticle_params( double *x, double *y, double *ang ) const
225 {
226  *x = reticle_pos.x;
227  *y = reticle_pos.y;
228 
229  *ang = reticle_angle;
230 
231  return true;
232 }
233 
234 
235 int cgmath::get_square_index( void ) const
236 {
237  return square_idx;
238 }
239 
240 
241 int cgmath::get_square_algorithm_index( void ) const
242 {
243  return square_alg_idx;
244 }
245 
246 
247 
248 cproc_in_params * cgmath::get_in_params( void )
249 {
250  return &in_params;
251 }
252 
253 
254 void cgmath::set_in_params( const cproc_in_params *v )
255 {
256  //in_params.threshold_alg_idx = v->threshold_alg_idx;
257  set_square_algorithm( v->threshold_alg_idx );
258  in_params.guiding_rate = v->guiding_rate;
259  in_params.enabled[GUIDE_RA] = v->enabled[GUIDE_RA];
260  in_params.enabled[GUIDE_DEC] = v->enabled[GUIDE_DEC];
261  in_params.average = v->average;
262  in_params.accum_frame_cnt[GUIDE_RA] = v->accum_frame_cnt[GUIDE_RA];
263  in_params.accum_frame_cnt[GUIDE_DEC] = v->accum_frame_cnt[GUIDE_DEC];
264  in_params.proportional_gain[GUIDE_RA] = v->proportional_gain[GUIDE_RA];
265  in_params.proportional_gain[GUIDE_DEC] = v->proportional_gain[GUIDE_DEC];
266  in_params.integral_gain[GUIDE_RA] = v->integral_gain[GUIDE_RA];
267  in_params.integral_gain[GUIDE_DEC] = v->integral_gain[GUIDE_DEC];
268  in_params.derivative_gain[GUIDE_RA] = v->derivative_gain[GUIDE_RA];
269  in_params.derivative_gain[GUIDE_DEC] = v->derivative_gain[GUIDE_DEC];
270  in_params.max_pulse_length[GUIDE_RA] = v->max_pulse_length[GUIDE_RA];
271  in_params.max_pulse_length[GUIDE_DEC] = v->max_pulse_length[GUIDE_DEC];
272  in_params.min_pulse_length[GUIDE_RA] = v->min_pulse_length[GUIDE_RA];
273  in_params.min_pulse_length[GUIDE_DEC] = v->min_pulse_length[GUIDE_DEC];
274 }
275 
276 
277 const cproc_out_params * cgmath::get_out_params( void ) const
278 {
279  return &out_params;
280 }
281 
282 
283 info_params_t cgmath::get_info_params( void ) const
284 {
285  info_params_t ret;
286  Vector p;
287 
288  ret.aperture = aperture;
289  ret.focal = focal;
290  ret.focal_ratio = focal / aperture;
291  p = Vector(video_width, video_height, 0);
292  p = point2arcsec( p );
293  p /= 60; // convert to minutes
294  ret.fov_wd = p.x;
295  ret.fov_ht = p.y;
296 
297  return ret;
298 }
299 
300 
301 uint32_t cgmath::get_ticks( void ) const
302 {
303  return ticks;
304 }
305 
306 
307 void cgmath::get_star_drift( double *dx, double *dy ) const
308 {
309  *dx = star_pos.x;
310  *dy = star_pos.y;
311 }
312 
313 
314 void cgmath::get_star_screen_pos( double *dx, double *dy ) const
315 {
316  *dx = scr_star_pos.x;
317  *dy = scr_star_pos.y;
318 }
319 
320 
321 bool cgmath::reset( void )
322 {
323  square_idx = DEFAULT_SQR;
324  square_alg_idx = AUTO_THRESHOLD;
325  square_size = guide_squares[square_idx].size;
326  square_square = guide_squares[square_idx].square;
327  square_pos = Vector(0);
328 
329  // sky coord. system vars.
330  star_pos = Vector(0);
331  scr_star_pos = Vector(0);
332 
333  set_reticle_params( video_width/2, video_height/2, 0.0 );
334 
335  return true;
336 }
337 
338 
339 void cgmath::move_square( double newx, double newy )
340 {
341  if (pimage == NULL)
342  return;
343 
344  square_pos.x = newx;
345  square_pos.y = newy;
346 
347  // check frame ranges
348  if( square_pos.x < 0 )
349  square_pos.x = 0;
350  if( square_pos.y < 0 )
351  square_pos.y = 0;
352  if( square_pos.x+(double)square_size > (double)video_width )
353  square_pos.x = (double)(video_width - square_size);
354  if( square_pos.y+(double)square_size > (double)video_height )
355  square_pos.y = (double)(video_height - square_size);
356 
357  // FITS Image takes center coords
358  pimage->setGuideSquare(square_pos.x+square_size/2, square_pos.y+square_size/2);
359 }
360 
361 
362 void cgmath::resize_square( int size_idx )
363 {
364  if( size_idx < 0 || size_idx >= (int)(sizeof(guide_squares)/sizeof(guide_square_t))-1 || pimage == NULL)
365  return;
366 
367  square_size = guide_squares[size_idx].size;
368  square_square = guide_squares[size_idx].square;
369  square_idx = size_idx;
370 
371  // check position
372  pimage->setGuideBoxSize(square_size);
373 
374  // TODO: FIXME
375  //move_square( square_pos.x, square_pos.y );
376 }
377 
378 
379 void cgmath::set_square_algorithm( int alg_idx )
380 {
381  if( alg_idx < 0 || alg_idx >= (int)(sizeof(guide_square_alg)/sizeof(square_alg_t))-1 )
382  return;
383 
384  square_alg_idx = alg_idx;
385 
386  in_params.threshold_alg_idx = square_alg_idx;
387 }
388 
389 
390 ovr_params_t *cgmath::prepare_overlays( void )
391 {
392  // square
393  overlays.square_size = square_size;
394  overlays.square_pos.x = (int)square_pos.x;
395  overlays.square_pos.y = (int)square_pos.y;
396 
397  // reticle
398  // sets by set_reticle_params() as soon as it does not move every frame
399 
400 
401  return &overlays;
402 }
403 
404 
405 Vector cgmath::point2arcsec( const Vector &p ) const
406 {
407  Vector arcs;
408 
409  // arcs = 3600*180/pi * (pix*ccd_pix_sz) / focal_len
410  arcs.x = 206264.8062470963552 * p.x * ccd_pixel_width / focal;
411  arcs.y = 206264.8062470963552 * p.y * ccd_pixel_height / focal;
412 
413  return arcs;
414 }
415 
416 
417 double cgmath::precalc_proportional_gain( double g_rate )
418 {
419  if( g_rate <= 0.01 )
420  return 0;
421 
422  return 1000.0 / (g_rate * 15.0);
423 }
424 
425 
426 bool cgmath::calc_and_set_reticle( double start_x, double start_y, double end_x, double end_y )
427 {
428  double phi;
429 
430 
431  phi = calc_phi( start_x, start_y, end_x, end_y );
432 
433  if( phi < 0 )
434  return false;
435 
436  set_reticle_params( start_x, start_y, phi );
437 
438  return true;
439 }
440 
441 
442 bool cgmath::calc_and_set_reticle2( double start_ra_x, double start_ra_y, double end_ra_x, double end_ra_y, double start_dec_x, double start_dec_y, double end_dec_x, double end_dec_y)
443 {
444  double phi_ra = 0; // angle calculated by GUIDE_RA drift
445  double phi_dec = 0; // angle calculated by GUIDE_DEC drift
446  double phi = 0;
447 
448  Vector ra_vect = Normalize( Vector(end_ra_x - start_ra_x, -(end_ra_y - start_ra_y), 0) );
449  Vector dec_vect = Normalize( Vector(end_dec_x - start_dec_x, -(end_dec_y - start_dec_y), 0) );
450 
451  Vector try_increase = dec_vect * RotateZ( M_PI/2 );
452  Vector try_decrease = dec_vect * RotateZ( -M_PI/2 );
453 
454  double cos_increase = try_increase & ra_vect;
455  double cos_decrease = try_decrease & ra_vect;
456 
457  bool do_increase = cos_increase > cos_decrease ? true : false;
458 
459  phi_ra = calc_phi( start_ra_x, start_ra_y, end_ra_x, end_ra_y );
460  if( phi_ra < 0 )
461  return false;
462 
463  phi_dec = calc_phi( start_dec_x, start_dec_y, end_dec_x, end_dec_y );
464  if( phi_dec < 0 )
465  return false;
466 
467  if( do_increase )
468  phi_dec += 90;
469  else
470  phi_dec -= 90;
471 
472  if( phi_dec > 360 )phi_dec -= 360.0;
473  if( phi_dec < 0 )phi_dec += 360.0;
474 
475  if( fabs(phi_dec - phi_ra) > 180 )
476  {
477  if( phi_ra > phi_dec )
478  phi_ra -= 360;
479  else
480  phi_dec -= 360;
481  }
482 
483  // average angles
484  phi = (phi_ra + phi_dec) / 2;
485  if( phi < 0 )phi += 360.0;
486 
487  // TODO: FIXME
488 // if( DBG_VERBOSITY )
489 // log_i( "PHI_RA = %f\nPHI_DEC = %f\nPHI_AVG = %f", phi_ra, phi_dec, phi);
490 
491  set_reticle_params( start_ra_x, start_ra_y, phi );
492 
493  return true;
494 }
495 
496 
497 double cgmath::calc_phi( double start_x, double start_y, double end_x, double end_y ) const
498 {
499  double delta_x, delta_y;
500  double phi;
501 
502  delta_x = end_x - start_x;
503  delta_y = -(end_y - start_y);
504 
505  if( !Vector(delta_x, delta_y, 0) < 5.0 )
506  return -1;
507 
508  // 90 or 270 degrees
509  if( fabs(delta_x) < fabs(delta_y) / 1000000.0 )
510  {
511  phi = delta_y > 0 ? 90.0 : 270;
512  }
513  else
514  {
515  phi = 180.0/M_PI*atan2( delta_y, delta_x );
516  if( phi < 0 )phi += 360.0;
517  }
518 
519  return phi;
520 }
521 
522 
523 void cgmath::do_ticks( void )
524 {
525  ticks++;
526 
527  channel_ticks[GUIDE_RA]++;
528  channel_ticks[GUIDE_DEC]++;
529  if( channel_ticks[GUIDE_RA] >= MAX_ACCUM_CNT )
530  channel_ticks[GUIDE_RA] = 0;
531  if( channel_ticks[GUIDE_DEC] >= MAX_ACCUM_CNT )
532  channel_ticks[GUIDE_DEC] = 0;
533 
534  accum_ticks[GUIDE_RA]++;
535  accum_ticks[GUIDE_DEC]++;
536  if( accum_ticks[GUIDE_RA] >= in_params.accum_frame_cnt[GUIDE_RA] )
537  accum_ticks[GUIDE_RA] = 0;
538  if( accum_ticks[GUIDE_DEC] >= in_params.accum_frame_cnt[GUIDE_DEC] )
539  accum_ticks[GUIDE_DEC] = 0;
540 }
541 
542 
543 //-------------------- Processing ---------------------------
544 void cgmath::start( void )
545 {
546  ticks = 0;
547  channel_ticks[GUIDE_RA] = channel_ticks[GUIDE_DEC] = 0;
548  accum_ticks[GUIDE_RA] = accum_ticks[GUIDE_DEC] = 0;
549  drift_integral[GUIDE_RA] = drift_integral[GUIDE_DEC] = 0;
550  out_params.reset();
551 
552  memset( drift[GUIDE_RA], 0, sizeof(double)*MAX_ACCUM_CNT );
553  memset( drift[GUIDE_DEC], 0, sizeof(double)*MAX_ACCUM_CNT );
554 
555  // cleanup stat vars.
556  sum = sqr_sum = 0;
557  delta_prev = sigma_prev = sigma = 0;
558 
559  preview_mode = false;
560 }
561 
562 
563 void cgmath::stop( void )
564 {
565  preview_mode = true;
566 }
567 
568 
569 void cgmath::suspend( bool mode )
570 {
571  suspended = mode;
572 }
573 
574 
575 bool cgmath::is_suspended( void ) const
576 {
577  return suspended;
578 }
579 
580 bool cgmath::is_lost_star(void) const
581 {
582  return lost_star;
583 }
584 
585 void cgmath::set_lost_star(bool is_lost)
586 {
587  lost_star = is_lost;
588 }
589 
590 Vector cgmath::find_star_local_pos( void ) const
591 {
592 
593  Vector ret;
594  int i, j;
595  double resx, resy, mass, threshold, pval;
596  float *psrc = NULL, *porigin = NULL;
597  float *pptr;
598 
599  psrc = porigin = pdata + (int)square_pos.y*video_width + (int)square_pos.x;
600 
601  resx = resy = 0;
602  threshold = mass = 0;
603 
604  // several threshold adaptive smart agorithms
605  switch( square_alg_idx )
606  {
607 
608  // Using FITSView centroid algorithm by Jasem Mutlaq
609  case CENTROID_THRESHOLD:
610  {
611  float center_x=-1, center_y=-1;
612  int max_val = -1;
613  int x1=square_pos.x;
614  int x2=square_pos.x + square_size;
615  int y1=square_pos.y;
616  int y2=square_pos.y + square_size;
617  FITSImage *image_data = pimage->getImageData();
618 
619  //qDebug() << "Search Region: X1: " << x1 << ", X2: " << x2 << " , Y1: " << y1 << " , Y2: " << y2 << endl;
620 
621  foreach(Edge *center, image_data->getStarCenters())
622  {
623 
624  //qDebug() << "Star X: " << center->x << ", Y: " << center->y << endl;
625 
626  if (center->x > x1 && center->x < x2 && center->y > y1 && center->y < y2 )
627  {
628  if (center->val > max_val)
629  {
630  max_val = center->val;
631  center_x = center->x;
632  center_y = center->y;
633  }
634  }
635  }
636 
637  return (ret = Vector(center_x , center_y, 0));
638  break;
639  }
640  // Alexander's Stepanenko smart threshold algorithm
641  case SMART_THRESHOLD:
642  {
643  point_t bbox_lt = { (int)square_pos.x-SMART_FRAME_WIDTH, (int)square_pos.y-SMART_FRAME_WIDTH };
644  point_t bbox_rb = { (int)square_pos.x+square_size+SMART_FRAME_WIDTH, (int)square_pos.y+square_size+SMART_FRAME_WIDTH };
645  int offset = 0;
646 
647  // clip frame
648  if( bbox_lt.x < 0 )
649  bbox_lt.x = 0;
650  if( bbox_lt.y < 0 )
651  bbox_lt.y = 0;
652  if( bbox_rb.x > video_width )
653  bbox_rb.x = video_width;
654  if( bbox_rb.y > video_height )
655  bbox_rb.y = video_height;
656 
657  // calc top bar
658  int box_wd = bbox_rb.x - bbox_lt.x;
659  int box_ht = (int)square_pos.y - bbox_lt.y;
660  int pix_cnt = 0;
661  if( box_wd > 0 && box_ht > 0 )
662  {
663  pix_cnt += box_wd * box_ht;
664  for( j = bbox_lt.y;j < (int)square_pos.y;++j )
665  {
666  offset = j*video_width;
667  for( i = bbox_lt.x;i < bbox_rb.x;++i )
668  {
669  pptr = pdata + offset + i;
670  threshold += *pptr;
671  }
672  }
673  }
674  // calc left bar
675  box_wd = (int)square_pos.x - bbox_lt.x;
676  box_ht = square_size;
677  if( box_wd > 0 && box_ht > 0 )
678  {
679  pix_cnt += box_wd * box_ht;
680  for( j = (int)square_pos.y;j < (int)square_pos.y+box_ht;++j )
681  {
682  offset = j*video_width;
683  for( i = bbox_lt.x;i < (int)square_pos.x;++i )
684  {
685  pptr = pdata + offset + i;
686  threshold += *pptr;
687  }
688  }
689  }
690  // calc right bar
691  box_wd = bbox_rb.x - (int)square_pos.x - square_size;
692  box_ht = square_size;
693  if( box_wd > 0 && box_ht > 0 )
694  {
695  pix_cnt += box_wd * box_ht;
696  for( j = (int)square_pos.y;j < (int)square_pos.y+box_ht;++j )
697  {
698  offset = j*video_width;
699  for( i = (int)square_pos.x+square_size;i < bbox_rb.x;++i )
700  {
701  pptr = pdata + offset + i;
702  threshold += *pptr;
703  }
704  }
705  }
706  // calc bottom bar
707  box_wd = bbox_rb.x - bbox_lt.x;
708  box_ht = bbox_rb.y - (int)square_pos.y - square_size;
709  if( box_wd > 0 && box_ht > 0 )
710  {
711  pix_cnt += box_wd * box_ht;
712  for( j = (int)square_pos.y+square_size;j < bbox_rb.y;++j )
713  {
714  offset = j*video_width;
715  for( i = bbox_lt.x;i < bbox_rb.x;++i )
716  {
717  pptr = pdata + offset + i;
718  threshold += *pptr;
719  }
720  }
721  }
722  // find maximum
723  double max_val = 0;
724  for( j = 0;j < square_size;++j )
725  {
726  for( i = 0;i < square_size;++i )
727  {
728  pptr = psrc+i;
729  if( *pptr > max_val )
730  max_val = *pptr;
731  }
732  psrc += video_width;
733  }
734  threshold /= (double)pix_cnt;
735  // cut by 10% higher then average threshold
736  if( max_val > threshold )
737  threshold += (max_val - threshold) * SMART_CUT_FACTOR;
738 
739  //log_i("smart thr. = %f cnt = %d", threshold, pix_cnt);
740  break;
741  }
742  // simple adaptive threshold
743  case AUTO_THRESHOLD:
744  {
745  for( j = 0;j < square_size;++j )
746  {
747  for( i = 0;i < square_size;++i )
748  {
749  pptr = psrc+i;
750  threshold += *pptr;
751  }
752  psrc += video_width;
753  }
754  threshold /= square_square;
755  break;
756  }
757  // no threshold subtracion
758  default:
759  {
760  }
761  }
762 
763  psrc = porigin;
764  for( j = 0;j < square_size;++j )
765  {
766  for( i = 0;i < square_size;++i )
767  {
768  pptr = psrc+i;
769  pval = *pptr - threshold;
770  pval = pval < 0 ? 0 : pval;
771 
772  resx += (double)i * pval;
773  resy += (double)j * pval;
774 
775  mass += pval;
776  }
777  psrc += video_width;
778  }
779 
780  if( mass == 0 )mass = 1;
781 
782  resx /= mass;
783  resy /= mass;
784 
785  ret = square_pos + Vector( resx, resy, 0 );
786 
787  return ret;
788 }
789 
790 
791 void cgmath::process_axes( void )
792 {
793  int cnt = 0;
794  double t_delta = 0;
795 
796  #ifdef GUIDE_LOG
797  qDebug() << "Processing Axes" << endl;
798  #endif
799 
800  // process axes...
801  for( int k = GUIDE_RA;k <= GUIDE_DEC;k++ )
802  {
803  // zero all out commands
804  out_params.pulse_dir[k] = NO_DIR;
805 
806  if( accum_ticks[k] < in_params.accum_frame_cnt[k]-1 )
807  continue;
808 
809  t_delta = 0;
810  drift_integral[k] = 0;
811 
812  cnt = in_params.accum_frame_cnt[ k ];
813 
814  for( int i = 0, idx = channel_ticks[k];i < cnt;++i )
815  {
816  t_delta += drift[k][idx];
817 
818  #ifdef GUIDE_LOG
819  qDebug() << "At #" << i << "drift[" << k << "][" << idx << "] = " << drift[k][idx] << " , t_delta: " << t_delta << endl;
820  #endif
821 
822  if( idx > 0 )
823  --idx;
824  else
825  idx = MAX_ACCUM_CNT-1;
826  }
827 
828  for( int i = 0;i < MAX_ACCUM_CNT;++i )
829  drift_integral[k] += drift[k][i];
830 
831  out_params.delta[k] = t_delta / (double)cnt;
832  drift_integral[k] /= (double)MAX_ACCUM_CNT;
833 
834  #ifdef GUIDE_LOG
835  qDebug() << "cnt: " << cnt << endl;
836  qDebug() << "delta[" << k << "]= " << out_params.delta[k] << endl;
837  qDebug() << "drift_integral[" << k << "]= " << drift_integral[k] << endl;
838  #endif
839  //if( k == GUIDE_RA )
840  // log_i( "PROP = %f INT = %f", out_params.delta[k], drift_integral[k] );
841 
842  out_params.pulse_length[k] = fabs(out_params.delta[k]*in_params.proportional_gain[k] + drift_integral[k]*in_params.integral_gain[k]);
843  out_params.pulse_length[k] = out_params.pulse_length[k] <= in_params.max_pulse_length[k] ? out_params.pulse_length[k] : in_params.max_pulse_length[k];
844 
845  #ifdef GUIDE_LOG
846  qDebug() << "pulse_length[" << k << "]= " << out_params.pulse_length[k] << endl;
847  #endif
848 
849  // calc direction
850  if( !in_params.enabled[k] )
851  {
852  out_params.pulse_dir[k] = NO_DIR;
853  continue;
854  }
855 
856  if( out_params.pulse_length[k] >= in_params.min_pulse_length[k] )
857  {
858  if( k == GUIDE_RA )
859  out_params.pulse_dir[k] = out_params.delta[k] > 0 ? RA_DEC_DIR : RA_INC_DIR; // GUIDE_RA. right dir - decreases GUIDE_RA
860  else
861  {
862  out_params.pulse_dir[k] = out_params.delta[k] > 0 ? DEC_INC_DIR : DEC_DEC_DIR; // GUIDE_DEC.
863 
864  if (ROT_Z.x[1][1] < 0)
865  out_params.pulse_dir[k] = (out_params.pulse_dir[k] == DEC_INC_DIR) ? DEC_DEC_DIR : DEC_INC_DIR;
866  }
867  }
868  else
869  out_params.pulse_dir[k] = NO_DIR;
870 
871  #ifdef GUIDE_LOG
872  if (out_params.pulse_dir[k] == NO_DIR)
873  qDebug() << "Direction: NO_DIR" << endl;
874  else if (out_params.pulse_dir[k] == RA_DEC_DIR)
875  qDebug() << "Direction: Decrease RA" << endl;
876  else if (out_params.pulse_dir[k] == RA_INC_DIR)
877  qDebug() << "Direction: Increase RA" << endl;
878  else if (out_params.pulse_dir[k] == DEC_INC_DIR)
879  qDebug() << "Direction: Increase DEC" << endl;
880  else if (out_params.pulse_dir[k] == DEC_DEC_DIR)
881  qDebug() << "Direction: Decrease DEC" << endl;
882  #endif
883 
884  }
885 
886 }
887 
888 
889 void cgmath::do_processing( void )
890 {
891  Vector arc_star_pos, arc_reticle_pos, pos, p;
892 
893  // do nothing if suspended
894  if( suspended )
895  return;
896 
897  // find guiding star location in
898  scr_star_pos = star_pos = find_star_local_pos();
899 
900  if (star_pos.x == -1 || star_pos.y == -1)
901  {
902  lost_star = true;
903  return;
904  }
905  else
906  lost_star = false;
907 
908 
909  // move square overlay
910  //move_square( round(star_pos.x) - (double)square_size/2, round(star_pos.y) - (double)square_size/2 );
911  move_square( ceil(star_pos.x) - (double)square_size/2, ceil(star_pos.y) - (double)square_size/2 );
912 
913  if( preview_mode )
914  return;
915 
916  #ifdef GUIDE_LOG
917  qDebug() << "################## BEGIN PROCESSING ##################" << endl;
918  #endif
919 
920  // translate star coords into sky coord. system
921 
922  // convert from pixels into arcsecs
923  arc_star_pos = point2arcsec( star_pos );
924  arc_reticle_pos = point2arcsec( reticle_pos );
925 
926 
927  #ifdef GUIDE_LOG
928  qDebug() << "Star X: " << star_pos.x << " Y: " << star_pos.y << endl;
929  qDebug() << "Reticle X: " << reticle_pos.x << " Y:" << reticle_pos.y << endl;
930  qDebug() << "Star Sky Coords RA: " << arc_star_pos.x << " DEC: " << arc_star_pos.y << endl;
931  qDebug() << "Reticle Sky Coords RA: " << arc_reticle_pos.x << " DEC: " << arc_reticle_pos.y << endl;
932  #endif
933 
934  // translate into sky coords.
935  star_pos = arc_star_pos - arc_reticle_pos;
936  star_pos.y = -star_pos.y; // invert y-axis as y picture axis is inverted
937 
938  #ifdef GUIDE_LOG
939  qDebug() << "-------> BEFORE ROTATION Diff RA: " << star_pos.x << " DEC: " << star_pos.y << endl;
940  #endif
941 
942  star_pos = star_pos * ROT_Z;
943 
944  // both coords are ready for math processing
945  //put coord to drift list
946  drift[GUIDE_RA][channel_ticks[GUIDE_RA]] = star_pos.x;
947  drift[GUIDE_DEC][channel_ticks[GUIDE_DEC]] = star_pos.y;
948 
949  #ifdef GUIDE_LOG
950  qDebug() << "-------> AFTER ROTATION Diff RA: " << star_pos.x << " DEC: " << star_pos.y << endl;
951  qDebug() << "RA channel ticks: " << channel_ticks[GUIDE_RA] << " DEC channel ticks: " << channel_ticks[GUIDE_DEC] << endl;
952  #endif
953 
954  // make decision by axes
955  process_axes();
956 
957  // process statistics
958  calc_square_err();
959 
960  // finally process tickers
961  do_ticks();
962 
963  #ifdef GUIDE_LOG
964  qDebug() << "################## FINISH PROCESSING ##################" << endl;
965  #endif
966 }
967 
968 
969 
970 void cgmath::calc_square_err( void )
971 {
972 
973 
974  if( !do_statistics )
975  return;
976 /*
977  double avg;
978 
979  // around avarage
980  sum += out_params.delta[GUIDE_RA];
981  avg = sum / ((double)ticks + 1.0);
982 
983  sqr_sum += ((avg - out_params.delta[GUIDE_RA]) * (avg - out_params.delta[GUIDE_RA]));
984 
985  out_params.sigma[GUIDE_RA] = sqrt( sqr_sum / ((double)ticks + 1.0) );
986 */
987 /*
988  // though all values around 0
989  if( ticks == 0 )
990  {
991  delta_prev = out_params.delta[GUIDE_RA];
992  return;
993  }
994  if( ticks == 1 )
995  {
996  sigma = delta_prev*delta_prev + out_params.delta[GUIDE_RA]*out_params.delta[GUIDE_RA];
997  }
998  else
999  {
1000  sigma = sigma_prev*((double)ticks-1)/(double)ticks + (1/(double)ticks)*out_params.delta[GUIDE_RA]*out_params.delta[GUIDE_RA];
1001  }
1002  sigma_prev = sigma;
1003  delta_prev = out_params.delta[GUIDE_RA];
1004 
1005  out_params.sigma[GUIDE_RA] = sqrt( sigma );
1006 
1007 // sigma[i] = sigma[i-1]*(i-1)/i + (1/i) * X[i]*X[i];
1008 //i = 1, sigma[1] = x[0]*x[0] + x[1]*x[1]
1009 */
1010 
1011  // through MAX_ACCUM_CNT values
1012  if( ticks == 0 )
1013  return;
1014 
1015  for( int k = GUIDE_RA;k <= GUIDE_DEC;k++ )
1016  {
1017  double sqr_avg = 0;
1018  for( int i = 0;i < MAX_ACCUM_CNT;++i )
1019  sqr_avg += drift[k][i] * drift[k][i];
1020 
1021  out_params.sigma[k] = sqrt( sqr_avg / (double)MAX_ACCUM_CNT );
1022  }
1023 
1024 }
1025 
1026 
1027 
1028 
1029 
1030 
1031 
1032 
1033 
1034 //---------------------------------------------------------------------------------------
1035 cproc_in_params::cproc_in_params()
1036 {
1037  reset();
1038 }
1039 
1040 
1041 void cproc_in_params::reset( void )
1042 {
1043  threshold_alg_idx = SMART_THRESHOLD;
1044  guiding_rate = 0.5;
1045  average = true;
1046 
1047  for( int k = GUIDE_RA;k <= GUIDE_DEC;k++ )
1048  {
1049  enabled[k] = true;
1050  accum_frame_cnt[k] = 1;
1051  proportional_gain[k] = cgmath::precalc_proportional_gain( guiding_rate );
1052  integral_gain[k] = 0;
1053  derivative_gain[k] = 0;
1054  max_pulse_length[k] = 5000;
1055  min_pulse_length[k] = 100;
1056  }
1057 }
1058 
1059 
1060 cproc_out_params::cproc_out_params()
1061 {
1062  reset();
1063 }
1064 
1065 
1066 void cproc_out_params::reset( void )
1067 {
1068  for( int k = GUIDE_RA;k <= GUIDE_DEC;k++ )
1069  {
1070  delta[k] = 0;
1071  pulse_dir[k] = NO_DIR;
1072  pulse_length[k] = 0;
1073  sigma[k] = 0;
1074  }
1075 }
1076 
1077 
1078 
cgmath::~cgmath
virtual ~cgmath()
Definition: gmath.cpp:98
FITSView::getImageData
FITSImage * getImageData()
Definition: fitsview.h:98
cproc_in_params::average
bool average
Definition: gmath.h:84
FITSView::setGuideSquare
void setGuideSquare(int x, int y)
Definition: fitsview.cpp:442
cproc_in_params::derivative_gain
double derivative_gain[CHANNEL_CNT]
Definition: gmath.h:88
cproc_in_params::integral_gain
double integral_gain[CHANNEL_CNT]
Definition: gmath.h:87
Vector::x
double x
Definition: vect.h:21
SMART_FRAME_WIDTH
#define SMART_FRAME_WIDTH
Definition: gmath.h:42
FITSImage
Definition: fitsimage.h:73
DEF_SQR_2
#define DEF_SQR_2
Definition: gmath.cpp:24
gmath.h
point_t::y
int y
Definition: common.h:38
ovr_params_t::square_size
int square_size
Definition: gmath.h:66
DEC_INC_DIR
Definition: indicommon.h:28
cgmath::get_out_params
const cproc_out_params * get_out_params(void) const
Definition: gmath.cpp:277
cgmath::get_star_drift
void get_star_drift(double *dx, double *dy) const
Definition: gmath.cpp:307
guide_square_alg
const square_alg_t guide_square_alg[]
Definition: gmath.cpp:37
Vector
Definition: vect.h:18
cgmath::get_ticks
uint32_t get_ticks(void) const
Definition: gmath.cpp:301
cgmath::set_guider_params
bool set_guider_params(double ccd_pix_wd, double ccd_pix_ht, double guider_aperture, double guider_focal)
Definition: gmath.cpp:150
guide_square_t::size
int size
Definition: gmath.h:25
ovr_params_t::square_pos
point_t square_pos
Definition: gmath.h:67
GUIDE_RA
#define GUIDE_RA
Definition: gmath.h:47
info_params_t
Definition: gmath.h:108
point_t
Definition: common.h:36
info_params_t::focal
double focal
Definition: gmath.h:112
cproc_in_params::cproc_in_params
cproc_in_params()
Definition: gmath.cpp:1035
MAX_ACCUM_CNT
#define MAX_ACCUM_CNT
Definition: gmath.h:52
FITSView
Definition: fitsview.h:81
cgmath::get_in_params
cproc_in_params * get_in_params(void)
Definition: gmath.cpp:248
cgmath::get_square_algorithm_index
int get_square_algorithm_index(void) const
Definition: gmath.cpp:241
Edge::x
float x
Definition: fitsimage.h:64
cproc_out_params::reset
void reset(void)
Definition: gmath.cpp:1066
cproc_in_params::min_pulse_length
int min_pulse_length[CHANNEL_CNT]
Definition: gmath.h:90
vect.h
cproc_in_params::max_pulse_length
int max_pulse_length[CHANNEL_CNT]
Definition: gmath.h:89
NO_THRESHOLD
#define NO_THRESHOLD
Definition: gmath.h:32
cgmath::suspend
void suspend(bool mode)
Definition: gmath.cpp:569
cproc_in_params::accum_frame_cnt
uint32_t accum_frame_cnt[CHANNEL_CNT]
Definition: gmath.h:85
cproc_in_params::guiding_rate
double guiding_rate
Definition: gmath.h:82
ovr_params_t::reticle_pos
point_t reticle_pos
Definition: gmath.h:70
Edge
Definition: fitsimage.h:61
fitsview.h
cgmath::reset
bool reset(void)
Definition: gmath.cpp:321
cgmath::is_lost_star
bool is_lost_star(void) const
Definition: gmath.cpp:580
FITSImage::getHeight
long getHeight()
Definition: fitsimage.h:99
cproc_out_params::pulse_dir
GuideDirection pulse_dir[2]
Definition: gmath.h:102
cproc_out_params
Definition: gmath.h:95
Edge::y
float y
Definition: fitsimage.h:65
point_t::x
int x
Definition: common.h:38
cgmath::do_processing
void do_processing(void)
Definition: gmath.cpp:889
cproc_in_params::reset
void reset(void)
Definition: gmath.cpp:1041
cproc_out_params::cproc_out_params
cproc_out_params()
Definition: gmath.cpp:1060
cgmath::set_lost_star
void set_lost_star(bool is_lost)
Definition: gmath.cpp:585
AUTO_THRESHOLD
#define AUTO_THRESHOLD
Definition: gmath.h:31
cgmath::is_suspended
bool is_suspended(void) const
Definition: gmath.cpp:575
cproc_out_params::delta
double delta[2]
Definition: gmath.h:101
cgmath::get_star_screen_pos
void get_star_screen_pos(double *dx, double *dy) const
Definition: gmath.cpp:314
DEC_DEC_DIR
Definition: indicommon.h:29
cgmath::get_info_params
info_params_t get_info_params(void) const
Definition: gmath.cpp:283
cgmath::resize_square
void resize_square(int size_idx)
Definition: gmath.cpp:362
DEFAULT_SQR
#define DEFAULT_SQR
Definition: gmath.h:50
cgmath::stop
void stop(void)
Definition: gmath.cpp:563
DEF_SQR_0
#define DEF_SQR_0
Definition: gmath.cpp:22
cgmath::move_square
void move_square(double newx, double newy)
Definition: gmath.cpp:339
matr.h
DEF_SQR_3
#define DEF_SQR_3
Definition: gmath.cpp:25
guide_square_t
Definition: gmath.h:23
RA_INC_DIR
Definition: indicommon.h:26
FITSImage::getStarCenters
QList< Edge * > getStarCenters()
Definition: fitsimage.h:97
info_params_t::focal_ratio
double focal_ratio
Definition: gmath.h:110
cgmath::get_data_buffer
float * get_data_buffer(int *width, int *height, int *length, int *size)
Definition: gmath.cpp:135
cproc_in_params
Definition: gmath.h:75
cgmath::set_buffer
void set_buffer(float *buffer)
Definition: gmath.cpp:120
Matrix
Definition: matr.h:18
Normalize
Vector Normalize(const Vector &v)
Definition: vect.h:158
cproc_in_params::threshold_alg_idx
int threshold_alg_idx
Definition: gmath.h:81
Matrix::x
double x[4][4]
Definition: matr.h:21
cgmath::set_video_params
bool set_video_params(int vid_wd, int vid_ht)
Definition: gmath.cpp:107
FITSImage::getImageBuffer
float * getImageBuffer()
Definition: fitsimage.h:91
ovr_params_t
Definition: gmath.h:58
cgmath::get_reticle_params
bool get_reticle_params(double *x, double *y, double *ang) const
Definition: gmath.cpp:224
ovr_params_t::reticle_axis_dec
point_t reticle_axis_dec[2]
Definition: gmath.h:69
guide_square_t::square
double square
Definition: gmath.h:26
FITSImage::getWidth
long getWidth()
Definition: fitsimage.h:98
cgmath::start
void start(void)
Definition: gmath.cpp:544
cgmath::precalc_proportional_gain
static double precalc_proportional_gain(double g_rate)
Definition: gmath.cpp:417
cproc_out_params::pulse_length
int pulse_length[2]
Definition: gmath.h:103
square_alg_t
Definition: gmath.h:34
cgmath::calc_and_set_reticle2
bool calc_and_set_reticle2(double start_ra_x, double start_ra_y, double end_ra_x, double end_ra_y, double start_dec_x, double start_dec_y, double end_dec_x, double end_dec_y)
Definition: gmath.cpp:442
cgmath::calc_phi
double calc_phi(double start_x, double start_y, double end_x, double end_y) const
Definition: gmath.cpp:497
info_params_t::aperture
double aperture
Definition: gmath.h:112
DEF_SQR_1
#define DEF_SQR_1
Definition: gmath.cpp:23
GUIDE_DEC
#define GUIDE_DEC
Definition: gmath.h:48
NO_DIR
Definition: indicommon.h:25
cgmath::set_reticle_params
bool set_reticle_params(double x, double y, double ang)
Definition: gmath.cpp:176
cproc_out_params::sigma
double sigma[2]
Definition: gmath.h:104
ovr_params_t::reticle_axis_ra
point_t reticle_axis_ra[2]
Definition: gmath.h:68
RA_DEC_DIR
Definition: indicommon.h:27
cgmath::get_square_index
int get_square_index(void) const
Definition: gmath.cpp:235
cgmath::set_in_params
void set_in_params(const cproc_in_params *v)
Definition: gmath.cpp:254
cgmath::get_guider_params
void get_guider_params(double *ccd_pix_wd, double *ccd_pix_ht, double *guider_aperture, double *guider_focal)
Definition: gmath.cpp:167
SMART_THRESHOLD
#define SMART_THRESHOLD
Definition: gmath.h:29
info_params_t::fov_ht
double fov_ht
Definition: gmath.h:111
cproc_in_params::enabled
bool enabled[CHANNEL_CNT]
Definition: gmath.h:83
Edge::val
int val
Definition: fitsimage.h:66
cgmath::prepare_overlays
ovr_params_t * prepare_overlays(void)
Definition: gmath.cpp:390
FITSView::setGuideBoxSize
void setGuideBoxSize(int size)
Definition: fitsview.cpp:452
cgmath::calc_and_set_reticle
bool calc_and_set_reticle(double start_x, double start_y, double end_x, double end_y)
Definition: gmath.cpp:426
RotateZ
Matrix RotateZ(double Angle)
Definition: matr.cpp:271
CENTROID_THRESHOLD
#define CENTROID_THRESHOLD
Definition: gmath.h:30
cgmath::set_image
void set_image(FITSView *image)
Definition: gmath.cpp:125
info_params_t::fov_wd
double fov_wd
Definition: gmath.h:111
cgmath::set_square_algorithm
void set_square_algorithm(int alg_idx)
Definition: gmath.cpp:379
Vector::y
double y
Definition: vect.h:21
guide_squares
const guide_square_t guide_squares[]
Definition: gmath.cpp:29
cproc_in_params::proportional_gain
double proportional_gain[CHANNEL_CNT]
Definition: gmath.h:86
cgmath::cgmath
cgmath()
Definition: gmath.cpp:45
SMART_CUT_FACTOR
#define SMART_CUT_FACTOR
Definition: gmath.h:44
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:36:19 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kstars

Skip menu "kstars"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal