Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.17 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include "includes.h"
  4. #include <math.h>
  5. #include "system.h"
  6. #include "altera_up_avalon_parallel_port.h"
  7. #include "alt_types.h"
  8. #include "sys/alt_irq.h"
  9. #include "io.h"
  10. #include <cmath>
  11.  
  12. /* Definition of Task Stacks */
  13. #define TASK_STACKSIZE 2048
  14. OS_STK task1_stk[TASK_STACKSIZE];
  15. OS_STK task2_stk[TASK_STACKSIZE];
  16. /* Definition of Task Priorities */
  17. #define TASK1_PRIORITY 1
  18. #define TASK2_PRIORITY 2
  19. OS_EVENT * SemBeamEdgeChange;
  20. #define CE(x) if ((err = x) != OS_NO_ERR) printf("Runtime error: %d line %d - see ucos_ii.h\n", err, __LINE__);
  21. INT8U err;
  22. void * context;
  23.  
  24. INT32U prevBeamTime;
  25. int BeamPeriod[5];
  26. int beamCount=3; //Start from case 3
  27. int speed;
  28. int halfPeriod;
  29. int delayPeriod;
  30. int thetaMax;
  31. int signed direction;
  32. #define SIZE_EMULATION 11 //represents a quarter of the emulation size sine wave
  33.  
  34. int signed emulatedPosition[4*SIZE_EMULATION-3];
  35.  
  36. float tempLookUpTable[4*SIZE_EMULATION-3];
  37. int signed lookUpTable[4*SIZE_EMULATION-3];
  38. int multiplier = 10000;
  39.  
  40.  
  41.  
  42.  
  43.  
  44. //Performs the calculations for half period, direction, speed and thetaMax
  45. static void beamCalcs(void){
  46.  
  47. if(BeamPeriod[0]+BeamPeriod[1]<BeamPeriod[2]+5){
  48. direction= -1;
  49. }
  50. if(BeamPeriod[1]+BeamPeriod[2]<BeamPeriod[0]+5){
  51. direction= 1;
  52. }
  53.  
  54. thetaMax = (((halfPeriod*2*84*speed)+500000)/1000000);
  55. speed = 40*1000/(BeamPeriod[0]+BeamPeriod[1]+BeamPeriod[2]); //*1000 to use fixed point
  56.  
  57. //halfPeriod = BeamPeriod[0]+2*BeamPeriod[1]+BeamPeriod[2]+BeamPeriod[3]; // I CHANGE THIS
  58. halfPeriod = 4*BeamPeriod[1]+BeamPeriod[3];
  59. }
  60.  
  61. //Prints the beam calculations (debug)
  62. void printBeamCalcs(void){
  63. printf("%d\n",BeamPeriod[0]);
  64. printf("%d\n",BeamPeriod[1]);
  65. printf("%d\n",BeamPeriod[2]);
  66. printf("%d\n",BeamPeriod[3]);
  67. printf("Speed: %d\n",speed);
  68. printf("thetaMax = %d \n",thetaMax);
  69. printf("Half Period: %d\n\n\n",halfPeriod);
  70. }
  71.  
  72.  
  73. void lookUpTableGenerate(void){
  74. int i;
  75. for (i = 0; i < (SIZE_EMULATION-1)*4 ; i++){ //double the [0,pi] period
  76. tempLookUpTable[i] = multiplier*sin(i*(M_PI/2)/(SIZE_EMULATION-1));
  77. lookUpTable[i] = round(tempLookUpTable[i] );
  78. printf("%d\n",lookUpTable[i]);
  79. }
  80. //update lookUpTable
  81. }
  82.  
  83. int currThetaMax; //if the difference between the curr and new theta max, re-emulate
  84. int newThetaMax; //recalculated in trajectoryTask
  85.  
  86. //Simulates the next positions given the current theta, direction, updates emulatedPosition..
  87. void emulatePosition(void){
  88. currThetaMax = thetaMax; //theta max used in current position emulation
  89.  
  90.  
  91. //inputs - thetaMax, direction,
  92. int i;
  93. //printf("emulatePosition()\n");
  94. for (i = 0; i < (SIZE_EMULATION-1)*4 ; i++){
  95. emulatedPosition[i] = direction*currThetaMax*lookUpTable[i]/multiplier; //replace with sin lookup fn, will have floating point error here (because int)
  96. //printf("%d,%d\n",emulatedPosition[i],i);
  97. }
  98. //outputs - emulatedPosition
  99. }
  100.  
  101. void trajectorytask(void* pdata){
  102.  
  103. int i=0;
  104. int targetAngleOutput;
  105.  
  106. while(1){ // This will run through array of emulatedPosition, recalc thetaMax and reset i if too big diff
  107.  
  108. delayPeriod = (halfPeriod*2)/(4*SIZE_EMULATION-3);
  109. //targetAngleOutput = direction*thetaMax*lookUpTable[i+1]/multiplier;
  110. //targetAngleOutput = thetaMax*lookUpTable[i+1]/multiplier;
  111. targetAngleOutput = emulatedPosition[i];
  112. IOWR(0x81030,0,targetAngleOutput);
  113. i++;
  114.  
  115. OSTimeDlyHMSM(0,0,0,delayPeriod);
  116.  
  117. //newThetaMax = direction*(((halfPeriod*2*84*speed)+500000)/1000000);
  118. newThetaMax = (((halfPeriod*2*84*speed)+500000)/1000000);
  119.  
  120. if( (abs(newThetaMax) - abs(currThetaMax)) > 30 ){ //if the angle used to emulate position changes too much, recalc position
  121. i = 0;
  122. //printf("BUMPED!\n");
  123. emulatePosition();
  124. newThetaMax = thetaMax;
  125. currThetaMax = thetaMax;
  126. }
  127.  
  128.  
  129. if(i==4*SIZE_EMULATION-3){
  130. i=0;
  131. emulatePosition();
  132. }
  133.  
  134. }
  135. }
  136.  
  137. //beamTask, pends the beam interrupt
  138. void beamtask(void* pdata){
  139. while (1){
  140. OSSemPend(SemBeamEdgeChange,0,&err); //Do everything below when interrupt happens
  141.  
  142. //if(BeamPeriod[0]+BeamPeriod[1]+BeamPeriod[2]==0)printf("Initialised\n");
  143.  
  144. if(BeamPeriod[1]+BeamPeriod[2]+BeamPeriod[3]<BeamPeriod[0])beamCount=3;
  145. if(BeamPeriod[2]+BeamPeriod[3]+BeamPeriod[0]<BeamPeriod[1])beamCount=3;
  146. if(BeamPeriod[3]+BeamPeriod[0]+BeamPeriod[1]<BeamPeriod[2])beamCount=3;
  147.  
  148. //printf("%ld %ld %ld %ld\n\n",BeamPeriod[0],BeamPeriod[1],BeamPeriod[2],BeamPeriod[3]);
  149. INT32U currTime = OSTimeGet();
  150.  
  151.  
  152. switch(beamCount){
  153. case 0:
  154. BeamPeriod[0] = currTime-prevBeamTime;
  155. prevBeamTime = currTime;
  156. beamCount++;
  157. break;
  158.  
  159. case 1:
  160. BeamPeriod[1] = currTime-prevBeamTime;
  161. prevBeamTime = currTime;
  162. beamCount++;
  163. break;
  164.  
  165. case 2:
  166. BeamPeriod[2] = currTime-prevBeamTime;
  167. prevBeamTime = currTime;
  168. beamCount++;
  169. break;
  170.  
  171. case 3:
  172. BeamPeriod[3] = currTime-prevBeamTime;
  173. prevBeamTime = currTime;
  174. beamCount = 0;
  175.  
  176.  
  177. beamCalcs();
  178. //printBeamCalcs(); //OUTPUT THE DEBUG PRINTF STATEMENTS FOR THE BEAM
  179. emulatePosition();
  180.  
  181. break;
  182.  
  183. default: break;
  184. }
  185. }
  186. }
  187.  
  188. //Beam ISR routine, services the interrupt flag
  189. static void BeamISR(void * isr_context, alt_u32 id){
  190. IOWR(BEAM_BASE,3,0); //Clear the interrupt
  191.  
  192. OSSemPost(SemBeamEdgeChange);
  193. }//Beam ISR
  194.  
  195.  
  196. /* The main function creates one task */
  197. int main(void) {
  198. lookUpTableGenerate();
  199. SemBeamEdgeChange = OSSemCreate(0);
  200.  
  201. // clear KEY PIO interrupt capture register, // hence clears any pending interrupts
  202. alt_irq_register(BEAM_IRQ, context, BeamISR); // registers and enables KEY_IRQ to interrupt
  203. IOWR(BEAM_BASE,2,0x1); // enable edge capture bits to generate interrupt for lowest 4 bits
  204.  
  205.  
  206.  
  207. OSTaskCreateExt(beamtask,
  208. NULL,
  209. (void *)&task1_stk[TASK_STACKSIZE-1],
  210. TASK1_PRIORITY,
  211. TASK1_PRIORITY,
  212. task1_stk,
  213. TASK_STACKSIZE,
  214. NULL,
  215. 0);
  216.  
  217. OSTaskCreateExt(trajectorytask,
  218. NULL,
  219. (void *)&task2_stk[TASK_STACKSIZE-1],
  220. TASK2_PRIORITY,
  221. TASK2_PRIORITY,
  222. task2_stk,
  223. TASK_STACKSIZE,
  224. NULL,
  225. 0);
  226.  
  227. OSStart();
  228. return 0;
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement