Advertisement
DenseBrainMatrix

Untitled

Apr 22nd, 2022
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <stddef.h>
  3. #include "pru_uart.h"
  4. #include "pru_ecap.h"
  5. #include "sys_gpio.h"
  6.  
  7. // define PWM inputs for motor controll
  8. #define EPWM1A ( *(uint16_t volatile *)0x48302212 )
  9. #define EPWM1B ( *(uint16_t volatile *)0x48302214 )
  10.  
  11. // encoder position in other PRU core's memory
  12. #define position_var ((uint32_t const volatile *)0x00002000)
  13.  
  14. struct Message {
  15. uint32_t id;
  16. uint32_t timestamp;
  17. uint32_t position;
  18. int16_t force;
  19. int16_t control_signal;
  20. int16_t motor_effort;
  21. };
  22.  
  23. // layout of shared ddr3 memory (filled in by loader)
  24. struct DDRLayout {
  25. Message volatile *msgbuf;
  26. uint16_t num_msgs;
  27. uint16_t msg_size;
  28. };
  29.  
  30. struct SharedVars {
  31. // set by pru before halting
  32. char const *abort_file;
  33. int abort_line;
  34. // read-pointer updated by python
  35. uint16_t ridx;
  36. uint16_t signal[1000];
  37. };
  38.  
  39. far struct DDRLayout ddr __attribute__((location(0x10000))) = {};
  40. far struct SharedVars volatile shmem __attribute__((location(0x10100))) = {};
  41.  
  42. static inline uint32_t timestamp_cycles()
  43. {
  44. return CT_ECAP.TSCTR;
  45. }
  46.  
  47. // for easier debugging, record where in the source code we halted
  48. __attribute__((noreturn))
  49. void abort_at( char const *file, int line )
  50. {
  51. shmem.abort_file = file;
  52. shmem.abort_line = line;
  53. for(;;) __halt();
  54. }
  55.  
  56. static inline void assert_at( bool cond, char const *file, int line )
  57. {
  58. if( ! cond )
  59. abort_at( file, line );
  60. }
  61.  
  62. #define abort() abort_at( __FILE__, __LINE__ )
  63. #define assert(cond) assert_at( (cond), __FILE__, __LINE__ )
  64.  
  65. // local copy of write-pointer
  66. static uint16_t widx = 0;
  67.  
  68. // global var for write-pointer is located right after message ringbuffer
  69. #define ddr_msgbuf_end ( ddr.msgbuf + ddr.num_msgs )
  70. #define ddr_widx ( *(uint16_t volatile *)ddr_msgbuf_end )
  71.  
  72. // receive byte from uart
  73. static inline char uart_recv_byte()
  74. {
  75. for(;;) {
  76. uint8_t lsr = CT_UART.LSR;
  77. if( lsr & 0x1e )
  78. abort(); // receive-error occurred
  79. if( lsr & 0x01 )
  80. return (char) CT_UART.RBR;
  81. }
  82. }
  83.  
  84.  
  85. // receive CR-terminated line from uart
  86. static inline uint8_t uart_recv_line( char volatile msg[], uint8_t maxlen )
  87. {
  88. uint8_t len = 0;
  89.  
  90. for(;;) {
  91. char c = uart_recv_byte();
  92. if( c == '\r' )
  93. break; // found end of line
  94. if( len == maxlen )
  95. abort(); // line does not fit in buffer
  96. msg[ len++ ] = c;
  97. }
  98.  
  99. return len;
  100. }
  101.  
  102. int16_t receive_measurement()
  103. {
  104. // allocate buffer at fixed address for debugging convenience
  105. static char volatile msg[8] __attribute__((location(0x1f00)));
  106.  
  107. // receive line from uart
  108. uint8_t len = uart_recv_line( msg, sizeof msg );
  109.  
  110. // verify length and prefix
  111. if( len != 8 || msg[0] != 'N' || (msg[1] != '+' && msg[1] != '-'))
  112. abort();
  113.  
  114. // parse the remainder as integer
  115. int16_t value = 0;
  116. uint8_t i;
  117. for( i = 2; i < len; i++ ) {
  118. if( msg[i] < '0' || msg[i] > '9' )
  119. abort();
  120. value = value * 10 + ( msg[i] - '0' );
  121. }
  122. if (msg[1] == '-')
  123. value *= -1;
  124.  
  125. return value;
  126. }
  127.  
  128.  
  129. void initialize()
  130. {
  131. // perform sanity-checking
  132. assert( 0x80000000 <= (uint32_t)ddr.msgbuf );
  133. assert( ddr.msgbuf < ddr_msgbuf_end );
  134. assert( ddr.msg_size == sizeof(Message) );
  135.  
  136. assert( ddr_widx == widx );
  137. assert( shmem.ridx == widx );
  138. }
  139.  
  140.  
  141. static inline uint16_t next_idx( uint16_t idx )
  142. {
  143. if( ++idx == ddr.num_msgs )
  144. idx = 0;
  145. return idx;
  146. }
  147.  
  148.  
  149. void send_message( uint32_t id, int16_t current_force,uint32_t current_position, int16_t input, int16_t output)
  150. {
  151. uint16_t next_widx = next_idx( widx );
  152.  
  153. if( next_widx == shmem.ridx ) {
  154. // can't send message, ringbuffer is full
  155. abort();
  156. }
  157.  
  158. Message volatile *msg = &ddr.msgbuf[ widx ];
  159.  
  160. // fill in contents of message
  161. msg->id = id;
  162. msg->position = current_position;
  163. msg->force = current_force;
  164. msg->timestamp = timestamp_cycles();
  165. msg->control_signal = input;
  166. msg->motor_effort = output;
  167. // update write-pointer
  168. ddr_widx = widx = next_widx;
  169. }
  170.  
  171. void main() {
  172. int16_t control_signal = 0;
  173. int32_t error[3];
  174. CT_UART.THR = 'S';
  175. CT_UART.THR = 'N';
  176. CT_UART.THR = '\r';
  177. initialize();
  178.  
  179. uint32_t id =0;
  180. uint32_t reference_count = *position_var; // initil reading from decoder as unit32
  181.  
  182. uint8_t counter = 0;
  183. gpio_set_one_low(&GPIO0,31);
  184.  
  185. while (true) {
  186. int16_t force = receive_measurement();
  187. uint32_t position = *position_var;
  188.  
  189. // PID parameters
  190. int16_t Kp = 100;
  191. int16_t Ki = 0;
  192. int16_t Kd = 0;
  193. int16_t A[3] = {0};
  194. A[0] = Kp + Ki + Kd;
  195. A[1] = -Kp -2*Kd;
  196. A[2] = Kd;
  197. int16_t input_signal = shmem.signal[counter];
  198.  
  199. // Control logic work in progress
  200. error[2] = error[1];
  201. error[1] = error[0];
  202. error[0] = input_signal + reference_count; // current reading from decoder
  203. error[0] = error[0] - position;
  204. control_signal = control_signal + A[0] * error[0] + A[1] * error[1] + A[2] * error[2];
  205.  
  206. // check if motor direction must change
  207. if (control_signal < 0) {
  208. gpio_set_one_low(&GPIO0,31);
  209. } else {
  210. gpio_set_one_high(&GPIO0,31);
  211. }
  212.  
  213. // place upper bound on control signal
  214. if (control_signal > 4000)
  215. control_signal = 4000;
  216. // place lower bound on control signal
  217. if (control_signal < 600)
  218. control_signal = 600;
  219. // check for fault condition
  220. if (position > 5000 && position < 4294967000)
  221. EPWM1A = 0;
  222. else
  223. EPWM1A = control_signal;
  224. //report to python
  225. send_message( ++id,force, position, input_signal,control_signal);
  226. ++counter;
  227. // reset counter to enforce periodicy
  228. if (counter > 999)
  229. counter = 0;
  230. }
  231. }
  232.  
  233.  
  234.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement