Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.52 KB | None | 0 0
  1. */
  2. /* Mi 15. Nov 07:58:41 CET 2017 */
  3. /* Juergen Quade */
  4. /* */
  5. /* Fuer die Generierung wird die Realzeitbibliothek "rt" benoetigt. */
  6. /* Wenn das folgende Makefile verwendet wird, muss zur Generierung nur */
  7. /* noch "make" auf der Kommandozeile eingegeben werden: */
  8. /* ================================== */
  9. /* CFLAGS=-g -Wall */
  10. /* LDLIBS=-lrt -lpthread */
  11. /* */
  12. /* all: race */
  13. /* */
  14. /* clean: */
  15. /* rm -f race *.o */
  16. /* ================================== */
  17. /*************************************************************************/
  18. #include <stdio.h>
  19. #include <signal.h>
  20. #include <stdlib.h>
  21. #include <fcntl.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <malloc.h>
  25. #include <time.h>
  26. #include <sys/time.h>
  27. #include <asm/types.h>
  28.  
  29. static void set_speed( int fd, unsigned char Speed );
  30.  
  31. static unsigned char basis_speed_in=0x4d, basis_speed_out=0x4d;
  32. static unsigned long last_time;
  33. static int fdc=0;
  34.  
  35. static int auslenkung_innen=-1, auslenkung_aussen=-1;
  36.  
  37. typedef struct _liste {
  38. int type;
  39. int length;
  40. struct _liste *next;
  41. } streckenliste;
  42.  
  43. static streckenliste *root = NULL;
  44.  
  45. static struct _liste *add_to_liste( int type, int length )
  46. {
  47. struct _liste *ptr, *lptr;
  48.  
  49. lptr = malloc( sizeof(struct _liste) );
  50. if( lptr==NULL ) {
  51. printf("out of memory\n");
  52. return NULL;
  53. }
  54. lptr->type = type;
  55. lptr->length = length;
  56. lptr->next = root;
  57. if( root == NULL ) {
  58. root = lptr;
  59. lptr->next = lptr;
  60. return root;
  61. }
  62. for( ptr=root; ptr->next!=root; ptr=ptr->next )
  63. ;
  64. ptr->next = lptr;
  65. return lptr->next;
  66. }
  67.  
  68. static void exithandler( int signr )
  69. {
  70. if( fdc )
  71. set_speed( fdc, 0x0 );
  72. exit( 0 );
  73. }
  74.  
  75. static void set_speed( int fd, unsigned char speed )
  76. {
  77. int ret;
  78. char buffer[256];
  79.  
  80. printf("new speed: 0x%x\n", speed );
  81. if( fd>0 )
  82. ret = write( fd, &speed, sizeof(speed) );
  83. if (ret<0) {
  84. read( fd, buffer, sizeof(buffer) );
  85. printf("%s\n", buffer);
  86. close( fd );
  87. exit( -1 );
  88. }
  89. }
  90.  
  91. static __u16 read_with_time( int fd, unsigned long *time1 )
  92. {
  93. struct timespec timestamp;
  94. __u16 state;
  95. ssize_t ret;
  96.  
  97. ret=read( fd, &state, sizeof(state) );
  98. if (ret<0) {
  99. perror( "read" );
  100. return -1;
  101. }
  102. clock_gettime(CLOCK_MONOTONIC,&timestamp);
  103. *time1 = (timestamp.tv_sec * 1000000)+(timestamp.tv_nsec/1000);
  104. printf("read_with_time %4.4x\n", state);
  105. return state;
  106. }
  107.  
  108. static inline int is_sling( __u16 state )
  109. {
  110. if ( (state&0xf000)!=0x0000 )
  111. return 0; // keine Auslenkung
  112. // Es liegt eine Auslenkung vor...
  113. // TODO: Die Auslenkung wird abhaengig von der Spur
  114. // in auslenkung_innen bzw. auslenkung_aussen abgespeichert
  115.  
  116. //Aussen
  117. if( (state&0x0800) == 0x0800){
  118. auslenkung_aussen = (state&0x00FF);
  119. } else {
  120. //Innen
  121. auslenkung_innen = (state&0x00FF);
  122. }
  123. printf("auslenkung_innen: 0x%04x\n", auslenkung_innen);
  124. printf("auslenkung_aussen: 0x%04x\n", auslenkung_aussen);
  125. return 1;
  126. }
  127.  
  128. static void exploration( int fdc )
  129. {
  130. add_to_liste( 0x1000, 100 );
  131. }
  132.  
  133. static void change_speed()
  134. {
  135. printf("CHANGE SPEED BSI %d(0x%02x) | BSO %d (0x%02x) | BEFORE\n", basis_speed_in, basis_speed_in, basis_speed_out, basis_speed_out);
  136. printf("AI %d | AO %d\n", auslenkung_innen, auslenkung_aussen);
  137. switch(auslenkung_innen){
  138. case 0: basis_speed_in+=3; break;
  139. case 1: basis_speed_in+=3; break;
  140. case 2: basis_speed_in+=3; break;
  141. case 3: basis_speed_in+=1; break;
  142. case 4: basis_speed_in+=0; break;
  143. case 5: basis_speed_in+=-5; break;
  144. case 6: basis_speed_in+=-1; break;
  145. case 7: basis_speed_in+=0; break;
  146. default: basis_speed_in+=4; break;
  147. }
  148.  
  149. switch(auslenkung_aussen){
  150. case 0: basis_speed_out+=5; break;
  151. case 1: basis_speed_out+=0; break;
  152. case 2: basis_speed_out+=4; break;
  153. case 3: basis_speed_out+=0; break;
  154. case 4: basis_speed_out+=-4; break;
  155. case 5: basis_speed_out+=-2; break;
  156. case 6: basis_speed_out+=-4; break;
  157. case 7: basis_speed_out+=-4; break;
  158. default: basis_speed_out+=8; break;
  159. }
  160.  
  161. printf("CHANGE SPEED BSI %d(0x%02x) | BSO %d (0x%02x) | AFTER\n", basis_speed_in, basis_speed_in, basis_speed_out, basis_speed_out);
  162. //printf("CHANGE SPEED BSI %d | BSO %d | AFTER\n", basis_speed_in, basis_speed_out);
  163. }
  164.  
  165. static void tracking( int rounds_to_go )
  166. {
  167. struct _liste *position = root;
  168. int rounds=0;
  169. __u16 state_act;
  170. unsigned long time_act;
  171.  
  172. do {
  173. do {
  174. state_act=read_with_time( fdc, &time_act );
  175. } while( is_sling(state_act) );
  176. state_act=read_with_time( fdc, &time_act );
  177. //printf("0x%04x (expected 0x%04x)\n",state_act,position->type);
  178. if( (state_act&0xf000) != position->type ) {
  179. printf("wrong position 0x%04x (0x%04x)\n",
  180. state_act, position->type);
  181. }
  182. if( (state_act&0xf000)==0x1000 ) { // Start/Ziel
  183. rounds++;
  184. rounds_to_go--;
  185. printf("\n---> Runde: %d\n", rounds );
  186. last_time = time_act;
  187. is_sling(state_act);
  188. change_speed();
  189. //RESET
  190. auslenkung_innen = -1;
  191. auslenkung_aussen = -1;
  192. }
  193. position = position->next;
  194.  
  195. if((state_act&0x0800)==0x0800){
  196. printf("AUSSEN\n");
  197. set_speed(fdc,basis_speed_out);
  198. } else {
  199. printf("INNEN\n");
  200. set_speed(fdc,basis_speed_in);
  201. }
  202. //auslenkung_innen = -1;
  203. //auslenkung_aussen = -1;
  204. } while( rounds_to_go );
  205. }
  206.  
  207. int main( int argc, char **argv )
  208. {
  209. int rounds_to_go=10;
  210. struct sigaction new_action;
  211.  
  212. fdc = open( "/dev/ezsv3", O_RDWR );
  213. if( fdc<0 ) {
  214. perror( "/dev/ezsv3" );
  215. return -1;
  216. }
  217. if( argc > 1 ) {
  218. basis_speed_in=basis_speed_out=
  219. (unsigned char)strtoul(argv[1],NULL,0);
  220. if( argc > 2 ) {
  221. rounds_to_go = (unsigned int)strtoul(argv[2],NULL,0);
  222. }
  223. }
  224. new_action.sa_handler = exithandler;
  225. sigemptyset( &new_action.sa_mask );
  226. new_action.sa_flags = 0;
  227. sigaction( SIGINT, &new_action, NULL );
  228.  
  229. set_speed( fdc, basis_speed_in );
  230. exploration( fdc );
  231. tracking( rounds_to_go );
  232. set_speed( fdc, 0x0 );
  233. return 0;
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement