DenseBrainMatrix

Untitled

Apr 20th, 2022
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.18 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <stddef.h>
  3. #include "pru_uart.h"
  4. #include "pru_ecap.h"
  5.  
  6. typedef struct {
  7. /*000*/ uint32_t volatile REVISION;
  8.  
  9. /*004*/ uint32_t volatile rsvd004[(0x010-0x004)/4];
  10.  
  11. /*010*/ uint32_t volatile SYSCONFIG;
  12.  
  13. /*014*/ uint32_t volatile rsvd014[(0x020-0x014)/4];
  14.  
  15. /*020*/ uint32_t volatile EOI;
  16. /*024*/ uint32_t volatile IRQSTATUS_RAW[2];
  17. /*02c*/ uint32_t volatile IRQSTATUS[2];
  18. /*034*/ uint32_t volatile IRQSTATUS_SET[2];
  19. /*03c*/ uint32_t volatile IRQSTATUS_CLR[2];
  20. /*044*/ uint32_t volatile IRQWAKEN[2];
  21.  
  22. /*04c*/ uint32_t volatile rsvd04c[(0x114-0x04c)/4];
  23.  
  24. /*114*/ uint32_t volatile SYSSTATUS;
  25.  
  26. /*118*/ uint32_t volatile rsvd118[(0x130-0x118)/4];
  27.  
  28. /*130*/ uint32_t volatile CTRL;
  29.  
  30. /*134*/ uint32_t volatile OE;
  31. /*138*/ uint32_t volatile DATAIN;
  32. /*13c*/ uint32_t volatile DATAOUT;
  33.  
  34. /*140*/ uint32_t volatile LEVELDETECT0;
  35. /*144*/ uint32_t volatile LEVELDETECT1;
  36. /*148*/ uint32_t volatile RISINGDETECT;
  37. /*14c*/ uint32_t volatile FALLINGDETECT;
  38.  
  39. /*150*/ uint32_t volatile DEBOUNCENABLE;
  40. /*154*/ uint32_t volatile DEBOUNCINGTIME;
  41.  
  42. /*158*/ uint32_t volatile rsvd158[(0x190-0x158)/4];
  43.  
  44. /*190*/ uint32_t volatile CLEARDATAOUT;
  45. /*194*/ uint32_t volatile SETDATAOUT;
  46. } sysGpio;
  47.  
  48. #define GPIO0 (*(sysGpio *)0x44e07000)
  49. #define GPIO1 (*(sysGpio *)0x4804c000)
  50. #define GPIO2 (*(sysGpio *)0x481ac000)
  51. #define GPIO3 (*(sysGpio *)0x481ae000)
  52.  
  53. // define PWM inputs for motor controll
  54. #define EPWM1A ( *(uint16_t volatile *)0x48302212 )
  55. #define EPWM1B ( *(uint16_t volatile *)0x48302214 )
  56.  
  57.  
  58.  
  59. #define Signal_Array_Size = 100
  60. struct Signal {
  61. int time_points[100] = {0};
  62. int step_points[100] = {0};
  63. };
  64.  
  65. struct Message {
  66. uint32_t id;
  67. uint32_t timestamp;
  68. uint32_t position;
  69. int16_t force;
  70. int8_t control_signal;
  71. int8_t motor_effort;
  72. };
  73.  
  74. // encoder position in other PRU core's memory
  75. #define position_var ((uint32_t const volatile *)0x00002000)
  76.  
  77. // layout of shared ddr3 memory (filled in by loader)
  78. struct DDRLayout {
  79. Message volatile *msgbuf;
  80. uint16_t num_msgs;
  81. uint16_t msg_size;
  82. };
  83.  
  84. struct SharedVars {
  85. // set by pru before halting
  86. char const *abort_file;
  87. int abort_line;
  88. // read-pointer updated by python
  89. uint16_t ridx;
  90. };
  91.  
  92. far struct DDRLayout ddr __attribute__((location(0x10000))) = {};
  93. far struct SharedVars volatile shmem __attribute__((location(0x10100))) = {};
  94.  
  95. static inline uint32_t timestamp_cycles()
  96. {
  97. return CT_ECAP.TSCTR;
  98. }
  99.  
  100. // for easier debugging, record where in the source code we halted
  101. __attribute__((noreturn))
  102. void abort_at( char const *file, int line )
  103. {
  104. shmem.abort_file = file;
  105. shmem.abort_line = line;
  106. for(;;) __halt();
  107. }
  108.  
  109. static inline void assert_at( bool cond, char const *file, int line )
  110. {
  111. if( ! cond )
  112. abort_at( file, line );
  113. }
  114.  
  115. #define abort() abort_at( __FILE__, __LINE__ )
  116. #define assert(cond) assert_at( (cond), __FILE__, __LINE__ )
  117.  
  118. // local copy of write-pointer
  119. static uint16_t widx = 0;
  120.  
  121. // global var for write-pointer is located right after message ringbuffer
  122. #define ddr_msgbuf_end ( ddr.msgbuf + ddr.num_msgs )
  123. #define ddr_widx ( *(uint16_t volatile *)ddr_msgbuf_end )
  124.  
  125. // receive byte from uart
  126. static inline char uart_recv_byte()
  127. {
  128. for(;;) {
  129. uint8_t lsr = CT_UART.LSR;
  130. if( lsr & 0x1e )
  131. abort(); // receive-error occurred
  132. if( lsr & 0x01 )
  133. return (char) CT_UART.RBR;
  134. }
  135. }
  136.  
  137.  
  138. // receive CR-terminated line from uart
  139. static inline uint8_t uart_recv_line( char volatile msg[], uint8_t maxlen )
  140. {
  141. uint8_t len = 0;
  142.  
  143. for(;;) {
  144. char c = uart_recv_byte();
  145. if( c == '\r' )
  146. break; // found end of line
  147. if( len == maxlen )
  148. abort(); // line does not fit in buffer
  149. msg[ len++ ] = c;
  150. }
  151.  
  152. return len;
  153. }
  154.  
  155. int32_t receive_measurement()
  156. {
  157. // allocate buffer at fixed address for debugging convenience
  158. static char volatile msg[8] __attribute__((location(0x1f00)));
  159.  
  160. // receive line from uart
  161. uint8_t len = uart_recv_line( msg, sizeof msg );
  162.  
  163. // verify length and prefix
  164. if( len != 8 || msg[0] != 'N' || (msg[1] != '+' && msg[1] != '-'))
  165. abort();
  166.  
  167. // parse the remainder as integer
  168. int32_t value = 0;
  169. uint8_t i;
  170. for( i = 2; i < len; i++ ) {
  171. if( msg[i] < '0' || msg[i] > '9' )
  172. abort();
  173. value = value * 10 + ( msg[i] - '0' );
  174. }
  175. if (msg[1] == '-')
  176. value *= -1;
  177.  
  178. return value;
  179. }
  180.  
  181.  
  182. void initialize()
  183. {
  184. // perform sanity-checking
  185. assert( 0x80000000 <= (uint32_t)ddr.msgbuf );
  186. assert( ddr.msgbuf < ddr_msgbuf_end );
  187. assert( ddr.msg_size == sizeof(Message) );
  188.  
  189. assert( ddr_widx == widx );
  190. assert( shmem.ridx == widx );
  191. }
  192.  
  193.  
  194. static inline uint16_t next_idx( uint16_t idx )
  195. {
  196. if( ++idx == ddr.num_msgs )
  197. idx = 0;
  198. return idx;
  199. }
  200.  
  201.  
  202. void send_message( uint32_t id)
  203. {
  204. uint16_t next_widx = next_idx( widx );
  205.  
  206. if( next_widx == shmem.ridx ) {
  207. // can't send message, ringbuffer is full
  208. abort();
  209. }
  210.  
  211. Message volatile *msg = &ddr.msgbuf[ widx ];
  212.  
  213. // fill in contents of message
  214. msg->id = id;
  215. msg->position = *position_var;
  216. msg->force = receive_measurement();
  217. msg->timestamp = timestamp_cycles();
  218. // update write-pointer
  219. ddr_widx = widx = next_widx;
  220. }
  221.  
  222. void main() {
  223.  
  224. int32_t control_signal = 0;
  225. uint8_t Kp = 1;
  226. uint8_t Ki = 1;
  227. uint8_t Kd = 1;
  228. uint8_t dt = 1;
  229. uint32_t A[3] = {0};
  230. uint32_t error[3] = {0};
  231. A[0] = Kp + Ki*dt + Kd/dt;
  232. A[1] = -Kp - 2*Kd/dt;
  233. A[2] = Kd/dt;
  234. int count_point = 0;
  235. CT_UART.THR = 'S';
  236. CT_UART.THR = 'N';
  237. CT_UART.THR = '\r';
  238. initialize();
  239.  
  240. uint32_t id =0;
  241. uint32_t position = *position_var;
  242. uint32_t reference_count = position; // initil reading from decoder as unit32
  243. bool finished = false;
  244.  
  245. while (finished == false) {
  246. error[2] = error[1];
  247. error[1] = error[0];
  248. error[0] = (count_point + reference_count) − position; // current reading from decoder
  249. control_signal = control_signal + A[0] * error[0] + A[1] * error[1] + A[2] * error[2];
  250. EPWM1A = control_signal;
  251. send_message( ++id );
  252. __delay_cycles( 10000 );
  253.  
  254. }
  255.  
  256. }
  257.  
  258.  
  259.  
Advertisement
Add Comment
Please, Sign In to add comment