cuikmove_callbacks.c
Go to the documentation of this file.
1 #include "cuikmove.h"
2 
3 #include "cuikmove_callbacks.h"
4 #include "cuikmove_interface.h"
5 #include "cuikplay_support.h"
6 
7 #include <gtk/gtk.h>
8 
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <time.h>
12 
23 void on_cuikmove_slider_changed(GtkRange *range,
24  gpointer user_data)
25 {
27  signed int k;
28  const char *name;
29 
30 
31  c=(TCuikMoveControl *)user_data;
32 
33  name=gtk_widget_get_name(GTK_WIDGET(range));
34  k=atoi(&(name[1]));
35 
36  c->dof[k]=gtk_range_get_value(range);
37 
38  fprintf(stderr,"Slider %u changed (%g)\n",k,c->dof[k]);
39 
40  c->changed=TRUE;
41 }
42 
43 void on_cuikmove_quit(GtkButton *button,
44  gpointer user_data)
45 {
47 
48  c=(TCuikMoveControl *)user_data;
49  c->end=TRUE;
50 }
51 
52 void on_cuikmove_save(GtkButton *button,
53  gpointer user_data)
54 {
55  Tfilename fn,fm;
56  unsigned int i,n,m;
58  Tbox b;
59  double *sol;
60  FILE *f;
61  boolean *systemVars;
62  char **varNames;
63 
64  c=(TCuikMoveControl *)user_data;
65 
66 
67  #if (0)
68  {
69  boolean done,lessError,increased;
70  double d,e,e1,delta;
71 
72  done=FALSE;
73  delta=0.01;
74  while (!done)
75  {
76  d=0.0;
77  e=WorldErrorFromDOFs(c->p,c->dof,c->w);
78  for(i=0;i<c->ndof;i++)
79  {
80  /*Try to increse*/
81  increased=FALSE;
82  do {
83  c->dof[i]+=delta;
84  e1=WorldErrorFromDOFs(c->p,c->dof,c->w);
85  if (e1<e)
86  {
87  increased=TRUE;
88  d+=(e-e1);
89  e=e1;
90  lessError=TRUE;
91  }
92  else
93  {
94  c->dof[i]-=delta;
95  lessError=FALSE;
96  }
97  } while (lessError);
98 
99  if (!increased)
100  {
101  do {
102  c->dof[i]-=delta;
103  e1=WorldErrorFromDOFs(c->p,c->dof,c->w);
104  if (e1<e)
105  {
106  d+=(e-e1);
107  e=e1;
108  lessError=TRUE;
109  }
110  else
111  {
112  c->dof[i]+=delta;
113  lessError=FALSE;
114  }
115  } while (lessError);
116  }
117  }
118  fprintf(stderr,"Error %f (d: %f)\n",e,d);
119 
120  if (d==0)
121  done=TRUE;
122  }
123  c->changed=TRUE;
124  }
125  #endif
126 
127 
128  /* Print the degrees of freedom defining the current configuration */
129  CreateFileName(NULL,c->fw,NULL,JOINTS_EXT,&fn);
130  f=fopen(GetFileFullName(&fn),"w");
131  if (!f)
132  Error("Can not open output file for dofs from cuikmove");
133 
134  fprintf(stderr,"\n\n\nSaving dof to : %s\n",GetFileFullName(&fn));
135 
136  for(i=0;i<c->ndof;i++)
137  fprintf(f,"%.12f ",c->dof[i]);
138  fprintf(f,"\n");
139 
140  fclose(f);
141  DeleteFileName(&fn);
142 
143  /* Now print the solution in the form of a box and in the cuik variables */
144  CreateFileName(NULL,c->fw,NULL,SOL_EXT,&fn);
145  f=fopen(GetFileFullName(&fn),"w");
146  if (!f)
147  Error("Can not open output file for solutions from cuikmove");
148 
149  fprintf(stderr,"Saving solution to : %s\n",GetFileFullName(&fn));
150 
151  n=WorldDOF2Sol(c->p,c->dof,&sol,&b,c->w);
152 
153  m=GetWorldSystemVars(&systemVars,c->w);
154  if (n!=m)
155  Error("Inconsistent number of variables in on_cuikmove_save");
156 
157  NEW(varNames,n,char *);
158  GetWorldVarNames(varNames,c->w);
159 
160  PrintBoxSubset(f,systemVars,varNames,&b);
161 
162  free(varNames);
163  DeleteBox(&b);
164 
165  fclose(f);
166  DeleteFileName(&fn);
167 
168 #if (0)
169  /* Now print the solution in the form of a point */
170  CreateFileName(NULL,c->fw,"_kin",SAMPLE_EXT,&fn);
171  f=fopen(GetFileFullName(&fn),"w");
172  if (!f)
173  Error("Can not open output file for samples from cuikmove");
174 
175  fprintf(stderr,"Saving sample to : %s\n",GetFileFullName(&fn));
176 
177  for(i=0;i<n;i++)
178  {
179  if (systemVars[i])
180  fprintf(f,"%.12f ",sol[i]);
181  }
182  fprintf(f,"\n\n");
183  free(systemVars);
184 
185  fclose(f);
186  DeleteFileName(&fn);
187 #endif
188 
189  free(sol);
190 
191  /* Now print the a matlab script */
192  if (AnyCollision(c->w))
193  {
194  CreateFileName(NULL,c->fw,NULL,NULL,&fn);
195  CreateFileName(GetFilePath(&fn),"matlab/GenerateContactEquations",NULL,"m",&fm);
196 
197  f=fopen(GetFileFullName(&fm),"w");
198  if (!f)
199  Warning("Matlab script not created (not Matlab folder)");
200  else
201  {
202  fprintf(stderr,"Saving matlab script to: %s\n\n\n",GetFileFullName(&fm));
203 
204  PrintWorldCollisionInfo(f,c->fw,c->w);
205 
206  fclose(f);
207  }
208  DeleteFileName(&fm);
209  DeleteFileName(&fn);
210  }
211 }
#define FALSE
FALSE.
Definition: boolean.h:30
void PrintWorldCollisionInfo(FILE *f, char *fname, Tworld *w)
Prints information collected durint last collision check.
Definition: world.c:2508
#define NEW(_var, _n, _type)
Allocates memory space.
Definition: defines.h:385
Data structure to hold the information about the name of a file.
Definition: filename.h:248
unsigned int WorldDOF2Sol(Tparameters *p, double *dof, double **sol, Tbox *b, Tworld *w)
Transforms from degrees of freedom to cuik variables.
Definition: world.c:2543
void on_cuikmove_quit(GtkButton *button, gpointer user_data)
Callback function for the quit button.
Definition of the cuikmove control structure.
void GetWorldVarNames(char **vn, Tworld *w)
Return the variable names.
Definition: world.c:1842
double WorldErrorFromDOFs(Tparameters *p, double *dof, Tworld *w)
Error in equations from DOFs.
Definition: world.c:2129
#define TRUE
TRUE.
Definition: boolean.h:21
char * GetFilePath(Tfilename *fn)
Gets the file path.
Definition: filename.c:156
void Error(const char *s)
General error function.
Definition: error.c:80
boolean changed
Definition: cuikmove.h:25
void on_cuikmove_slider_changed(GtkRange *range, gpointer user_data)
Callback function for the slider bar.
Definition of the cuikmove control structure.
Definition: cuikmove.h:23
void DeleteFileName(Tfilename *fn)
Destructor.
Definition: filename.c:205
Headers of the GTK interface functions for cuikmove.
void Warning(const char *s)
General warning function.
Definition: error.c:116
void CreateFileName(char *path, char *name, char *suffix, char *ext, Tfilename *fn)
Constructor.
Definition: filename.c:22
Tparameters * p
Definition: cuikmove.h:31
boolean AnyCollision(Tworld *w)
Determines if we want to avoid any collision.
Definition: world.c:1721
void on_cuikmove_save(GtkButton *button, gpointer user_data)
Callback function for the save button.
char * GetFileFullName(Tfilename *fn)
Gets the file full name (paht+name+extension).
Definition: filename.c:151
#define SOL_EXT
File extension for solution files.
Definition: filename.h:137
A box.
Definition: box.h:83
Tworld * w
Definition: cuikmove.h:32
#define JOINTS_EXT
File extension for files with samples represented by the joint values.
Definition: filename.h:187
unsigned int GetWorldSystemVars(boolean **sv, Tworld *w)
Gets the system vars of the kinematic cuiksystem.
Definition: world.c:1810
void DeleteBox(void *b)
Destructor.
Definition: box.c:1259
void PrintBoxSubset(FILE *f, boolean *used, char **varNames, Tbox *b)
Prints a (sub-)box.
Definition: box.c:1138
double * dof
Definition: cuikmove.h:30
Headers of the GTK callback functions for cuikmove.
unsigned int ndof
Definition: cuikmove.h:27
boolean end
Definition: cuikmove.h:24
Headers of the GTK support functions for cuikplay.