avr/modules_avr/asserv/microb/trajectory/trajectory.c

Go to the documentation of this file.
00001 /*  
00002  *  Copyright Droids Corporation, Microb Technology, Eirbot (2005)
00003  * 
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  *
00018  *  Revision : $Id: trajectory.c,v 1.7 2005/05/02 09:10:56 zer0 Exp $
00019  *
00020  */
00021 
00026 #include <utils/utils.h>
00027 #include <time/scheduler/scheduler.h>
00028 #include <asserv/microb/asserv_main/asserv_main.h>
00029 #include <position/xy/xy.h>
00030 #include <asserv/eirbot/vect2/vect2.h>
00031 #include <asserv/microb/trajectory/trajectory.h>
00032 #include <trajectory_config.h>
00033 
00034 #define TRAJECTORY_XY_ANGLE_END_RAD (TRAJECTORY_XY_ANGLE_END*M_PI/180.0)
00035 #define TRAJECTORY_XY_ANGLE_MIN_MOVE_RAD (TRAJECTORY_XY_ANGLE_MIN_MOVE*M_PI/180.0)
00036 #define TRAJECTORY_XY_FORWARD_BACKWARD_PREFERENCY_ANGLE_RAD (TRAJECTORY_XY_FORWARD_BACKWARD_PREFERENCY_ANGLE*M_PI/180.0)
00037 
00038 #define DEBUG 0
00039 
00040 #include <stdio.h>
00041 
00042 #define debug_printf(args...) do { if(DEBUG) printf(args); } while(0)
00043 
00044 static s08 id=-1;
00045 extern struct asserv_config asserv;
00046 
00047 void trajectory_gotoxy_update(void);
00048 void trajectory_gotoa_update(void);
00049 
00053 void trajectory_init(void)
00054 {
00055 }
00056 
00058 void trajectory_del_cur_traj_event(void)
00059 {
00060   if(id != -1) {
00061   IRQ_LOCK(); 
00062   scheduler_del_event(id);    
00063   id=-1;
00064   IRQ_UNLOCK(); 
00065   }
00066 }
00067 
00068 void trajectory_set_speed_alpha(u16 speed_deg_s)
00069 {
00070   float s = (float)speed_deg_s * asserv.imp_alpha_per_deg * asserv.period;
00071   IRQ_LOCK();
00072   ramp_generator_set_speed(&asserv.ramp_alpha, s, -s);
00073   IRQ_UNLOCK();
00074 }
00075 
00076 void trajectory_set_speed_theta(u16 speed_cm_s)
00077 {
00078   float s = (float)speed_cm_s * asserv.imp_theta_per_cm * asserv.period;
00079   IRQ_LOCK();
00080   ramp_generator_set_speed(&asserv.ramp_theta, s, -s);
00081   IRQ_UNLOCK();
00082 }
00083 
00084 void trajectory_straight(s16 pos_cm)
00085 {
00086   double destx, desty, a;
00087 
00088   destx = xy_get_x_float(&asserv.pos);
00089   desty = xy_get_y_float(&asserv.pos);
00090   a     = xy_get_a_rad_float(&asserv.pos);
00091 
00092   debug_printf("%2.2f %2.2f %2.2f\r\n", destx, desty, a);
00093 
00094   destx = destx + cos(a)*(double)pos_cm ;
00095   desty = desty + sin(a)*(double)pos_cm ;
00096   
00097   debug_printf("%2.2f %2.2f\r\n", destx, desty);
00098 
00099   trajectory_gotoxy(destx, desty);
00100 }
00101 
00102 
00105 void __trajectory_gotoxy(s16 x, s16 y, u08 update)
00106 {
00107   static vect2 dest;
00108   vect2 cur,diff;
00109   double dest_angle;
00110   s32 ramp_alpha, ramp_theta;
00111 
00112   /* init all values */
00113   if(!update) {
00114     IRQ_LOCK();
00115     trajectory_del_cur_traj_event();
00116     asserv_reset_block(&asserv);
00117     asserv.status &= (~ASSERV_STATUS_BIT_END_TRAJ);
00118     IRQ_UNLOCK();
00119     dest.cart.type = VECT2_CARTESIEN ;
00120     dest.cart.x = (double)x;
00121     dest.cart.y = (double)y;
00122     id=scheduler_add_periodical_event_priority(trajectory_gotoxy_update, 100000l/SCHEDULER_UNIT, 50);
00123     debug_printf("EVT\r\n");
00124   }
00125 
00126   /* get cur pos */
00127   cur.cart.x = xy_get_x_float(&asserv.pos);
00128   cur.cart.y = xy_get_y_float(&asserv.pos);
00129   
00130   /* process difference diff=dest-cur*/
00131   diff = dest;
00132   vect2_sub(&diff, &cur, 2 * M_PI);
00133   vect2_2pol(&diff, 2 * M_PI);
00134 
00135   /* process dest_angle */
00136   dest_angle = diff.pol.teta-(double)xy_get_a_rad_float(&asserv.pos) ;
00137   if(dest_angle < -M_PI)
00138     dest_angle += (M_PI*2);
00139   else if (dest_angle > (M_PI))
00140     dest_angle -= (M_PI*2);
00141   
00142   if (diff.pol.u < TRAJECTORY_XY_DIST_END) {
00143     /* end of traj */
00144     debug_printf("Fin\r\n");
00145     trajectory_del_cur_traj_event();
00146     asserv.status |= (ASSERV_STATUS_BIT_END_TRAJ);
00147   }
00148   
00149   if (ABS(dest_angle) < TRAJECTORY_XY_ANGLE_MIN_MOVE_RAD) {
00150     /* start moving forward to the dest */
00151     debug_printf("Avance ");
00152     ramp_theta = asserv.ta_pos.theta + (s32)(diff.pol.u * asserv.imp_theta_per_cm);
00153     ramp_alpha = asserv.ta_pos.alpha + (s32)(dest_angle * asserv.imp_alpha_per_deg * 180.0/M_PI);
00154   }
00155   else if (ABS(dest_angle) < TRAJECTORY_XY_FORWARD_BACKWARD_PREFERENCY_ANGLE_RAD) {
00156     /* turn to have the dest point forward */
00157     debug_printf("Tourne AV ");
00158     ramp_theta = asserv.ta_pos.theta;
00159     ramp_alpha = asserv.ta_pos.alpha + (s32)(dest_angle * asserv.imp_alpha_per_deg * 180.0/M_PI);
00160   }
00161   else if (ABS(dest_angle) < M_PI - TRAJECTORY_XY_ANGLE_MIN_MOVE_RAD) {
00162     /* turn to have the dest point backward */
00163     debug_printf("Tourne AR ");
00164     ramp_theta = asserv.ta_pos.theta;
00165     if(dest_angle < 0) {
00166       ramp_alpha = asserv.ta_pos.alpha + (s32)((dest_angle + M_PI)* asserv.imp_alpha_per_deg * 180.0/M_PI);
00167     }
00168     else {
00169       ramp_alpha = asserv.ta_pos.alpha + (s32)((dest_angle - M_PI)* asserv.imp_alpha_per_deg * 180.0/M_PI);
00170     }
00171   }
00172   else {
00173     /* start moving backward to the dest */
00174     debug_printf("Recule ");
00175     ramp_theta = asserv.ta_pos.theta - (s32)(diff.pol.u * asserv.imp_theta_per_cm);
00176     if(dest_angle < 0) {
00177       ramp_alpha = asserv.ta_pos.alpha + (s32)((dest_angle + M_PI)* asserv.imp_alpha_per_deg * 180.0/M_PI);
00178     }
00179     else {
00180       ramp_alpha = asserv.ta_pos.alpha + (s32)((dest_angle - M_PI)* asserv.imp_alpha_per_deg * 180.0/M_PI);
00181     }
00182   }
00183 
00184   debug_printf("theta %ld, alpha %ld, cur_theta %ld\r\n", ramp_theta, ramp_alpha, asserv.ta_pos.theta);
00185 
00186   IRQ_LOCK(); 
00187   if(update) {
00188     ramp_generator_update_pos(&asserv.ramp_theta, ramp_theta);
00189     ramp_generator_update_pos(&asserv.ramp_alpha, ramp_alpha);
00190   }
00191   else {
00192     ramp_generator_new(&asserv.ramp_theta, asserv.ta_pos.theta,
00193                        ramp_theta, asserv.ta_speed.theta);
00194     ramp_generator_new(&asserv.ramp_alpha, asserv.ta_pos.alpha, 
00195                        ramp_alpha, asserv.ta_speed.alpha);
00196   }
00197   IRQ_UNLOCK(); 
00198 
00199   debug_printf("d%2.2f a%2.2f\r\n", diff.pol.u, dest_angle);
00200 }
00201 
00202 /* this is the callback function for gotoxy */
00203 void trajectory_gotoxy_update(void)
00204 {
00205   __trajectory_gotoxy(0, 0, 1);
00206 }
00207 
00208 inline void trajectory_gotoxy(s16 x, s16 y)
00209 {
00210   __trajectory_gotoxy(x,y,0);
00211 }
00212 
00215 void __trajectory_gotoa(s16 a, u08 update)
00216 {
00217   static double dest_angle;
00218   s32 ramp_alpha, ramp_theta;
00219   double diff_angle;
00220   double cur_angle=xy_get_a_rad_float(&asserv.pos);
00221 
00222   if(!update) {
00223     /* init all values */
00224     IRQ_LOCK();
00225     trajectory_del_cur_traj_event();
00226     asserv_reset_block(&asserv);
00227     asserv.status &= (~ASSERV_STATUS_BIT_END_TRAJ);
00228     dest_angle = a * (M_PI/180.0);
00229     if(dest_angle < -M_PI)
00230       dest_angle += (M_PI*2);
00231     else if (dest_angle > (M_PI))
00232       dest_angle -= (M_PI*2);
00233     id=scheduler_add_periodical_event_priority(trajectory_gotoa_update, 100000l/SCHEDULER_UNIT, 50);
00234     IRQ_UNLOCK();
00235   }
00236 
00237   /* process diff_angle */
00238   diff_angle = (double)dest_angle - cur_angle ;
00239   if(diff_angle < -M_PI)
00240     diff_angle += (M_PI*2);
00241   else if (diff_angle > (M_PI))
00242     diff_angle -= (M_PI*2);
00243   
00244   if(update)
00245     debug_printf("Running gotoa : dest=%2.2f cur=%2.2f diff=%2.2f\r\n", dest_angle, cur_angle, diff_angle);  
00246   else
00247     debug_printf("Init gotoa : dest=%2.2f cur=%2.2f diff=%2.2f\r\n", dest_angle, cur_angle, diff_angle);  
00248     
00249 
00250   /* end of traj ? */
00251   if (ABS(diff_angle) < TRAJECTORY_XY_ANGLE_END_RAD) {
00252     /* end of traj */
00253     debug_printf("Fin\r\n");
00254     trajectory_del_cur_traj_event();
00255     asserv.status |= (ASSERV_STATUS_BIT_END_TRAJ);
00256   }
00257 
00258   
00259   /* turn to have the dest point forward */
00260   ramp_theta = asserv.ta_pos.theta;
00261   ramp_alpha = asserv.ta_pos.alpha + (s32)(diff_angle * asserv.imp_alpha_per_deg * 180.0/M_PI);
00262   
00263   IRQ_LOCK(); 
00264   if(update) {
00265     ramp_generator_update_pos(&asserv.ramp_theta, ramp_theta);
00266     ramp_generator_update_pos(&asserv.ramp_alpha, ramp_alpha);
00267   }
00268   else {
00269     ramp_generator_new(&asserv.ramp_theta, asserv.ta_pos.theta,
00270                        ramp_theta, asserv.ta_speed.theta);
00271     ramp_generator_new(&asserv.ramp_alpha, asserv.ta_pos.alpha, 
00272                        ramp_alpha, asserv.ta_speed.alpha);
00273   }
00274   IRQ_UNLOCK(); 
00275   
00276   
00277 
00278   
00279 
00280 }
00281 
00282 inline void trajectory_gotoa(s16 a)
00283 {
00284   __trajectory_gotoa(a, 0);
00285 }
00286 
00287 /* this is the callback function for gotoa */
00288 void trajectory_gotoa_update(void)
00289 {
00290   __trajectory_gotoa(0, 1);
00291 }
00292 
00293 
00294 inline void trajectory_gotoa_fromxy(s16 x, s16 y)
00295 {
00296   vect2 dest, cur;
00297 
00298   dest.cart.type = VECT2_CARTESIEN ;
00299   dest.cart.x = (double)x;
00300   dest.cart.y = (double)y;
00301 
00302   /* get cur pos */
00303   cur.cart.x = xy_get_x_float(&asserv.pos);
00304   cur.cart.y = xy_get_y_float(&asserv.pos);
00305   
00306   dest = dest;
00307   vect2_sub(&dest, &cur, 360.0);
00308   vect2_2pol(&dest, 360.0);
00309 
00310   trajectory_gotoa(dest.pol.teta);
00311 }
00312 
00313 
00314 
00315 
00316 
00317 
00318 
00319 
00320 

Generated on Fri Nov 11 06:44:22 2005 for AVR by  doxygen 1.4.5