Viel Spaß mit der MAYA!


SUBMITTED BY: bitcoinsachen

DATE: April 26, 2017, 5 p.m.

FORMAT: C

SIZE: 11.6 kB

HITS: 344

  1. #include "stm32f30x_conf.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include "thetable.c"
  5. #define PI 3.14159265358979323846
  6. #define BUFFERSIZE 64
  7. #define UNCERT 20
  8. #define PERIOD 20
  9. // IN DIESEM PROJEKT GILT:
  10. // PROZESSORTAKT = 72 MHz
  11. // AHB Prescaler = 1
  12. // APB1 Prescaler = 2
  13. // APB2 Prescaler = 1
  14. volatile char buffer[BUFFERSIZE];
  15. volatile int bufferread = 0;
  16. volatile int vertstate = 0;
  17. volatile int horistate = 0;
  18. volatile int pos = 0;
  19. volatile char directions = 0;
  20. volatile char photo [4];
  21. volatile double rps = 0;
  22. void vertstep(int direction);
  23. void horistep(int direction);
  24. int main(void){
  25. // ---------------------------------------------------------------------
  26. // Enabling clocks
  27. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  28. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
  29. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
  30. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
  31. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
  32. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  33. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  34. RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  35. // ---------------------------------------------------------------------
  36. // Init GPIO (A) for USART1 (Maya serial) and USART2 (PC serial)
  37. GPIO_InitTypeDef GPIO_Init_Struct;
  38. GPIO_Init_Struct.GPIO_Mode = GPIO_Mode_AF;
  39. GPIO_Init_Struct.GPIO_OType = GPIO_OType_PP;
  40. GPIO_Init_Struct.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_2 | GPIO_Pin_3;
  41. GPIO_Init_Struct.GPIO_PuPd = GPIO_PuPd_NOPULL;
  42. GPIO_Init_Struct.GPIO_Speed = GPIO_Speed_50MHz;
  43. GPIO_Init(GPIOA, &GPIO_Init_Struct);
  44. GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_7);
  45. GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_7);
  46. GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_7);
  47. GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_7);
  48. // ---------------------------------------------------------------------
  49. // Init GPIO (A) for debugging with LED2 and extra PA6 for osci
  50. // GPIO_Init_Struct.GPIO_Mode = GPIO_Mode_OUT;
  51. // GPIO_Init_Struct.GPIO_OType = GPIO_OType_PP;
  52. // GPIO_Init_Struct.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6;
  53. // GPIO_Init_Struct.GPIO_PuPd = GPIO_PuPd_NOPULL;
  54. // GPIO_Init_Struct.GPIO_Speed = GPIO_Speed_50MHz;
  55. //
  56. // GPIO_Init(GPIOA,&GPIO_Init_Struct);
  57. // ---------------------------------------------------------------------
  58. // Init GPIO (B) for Vertical Stepper
  59. GPIO_Init_Struct.GPIO_Mode = GPIO_Mode_OUT;
  60. GPIO_Init_Struct.GPIO_OType = GPIO_OType_PP;
  61. GPIO_Init_Struct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
  62. GPIO_Init_Struct.GPIO_PuPd = GPIO_PuPd_NOPULL;
  63. GPIO_Init_Struct.GPIO_Speed = GPIO_Speed_50MHz;
  64. GPIO_Init(GPIOB, &GPIO_Init_Struct);
  65. GPIOB->ODR &= 0xF<<6;
  66. // ---------------------------------------------------------------------
  67. // Init GPIO (B) for Lichtschranken
  68. GPIO_Init_Struct.GPIO_Mode = GPIO_Mode_IN;
  69. GPIO_Init_Struct.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4;
  70. GPIO_Init_Struct.GPIO_PuPd = GPIO_PuPd_DOWN;
  71. GPIO_Init(GPIOB, &GPIO_Init_Struct);
  72. // ---------------------------------------------------------------------
  73. // Init GPIO (C) for Horizontal Stepper
  74. GPIO_Init_Struct.GPIO_Mode = GPIO_Mode_OUT;
  75. GPIO_Init_Struct.GPIO_OType = GPIO_OType_PP;
  76. GPIO_Init_Struct.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;
  77. GPIO_Init_Struct.GPIO_PuPd = GPIO_PuPd_NOPULL;
  78. GPIO_Init_Struct.GPIO_Speed = GPIO_Speed_50MHz;
  79. GPIO_Init(GPIOC, &GPIO_Init_Struct);
  80. GPIOC->ODR = 0;
  81. // ---------------------------------------------------------------------
  82. // Init TIM2 for Stepping every PERIOD ms
  83. TIM_TimeBaseInitTypeDef TIM_Init_Struct;
  84. TIM_Init_Struct.TIM_Prescaler = 7200 - 1; // 100 mcs per tick (0.1ms)
  85. TIM_Init_Struct.TIM_Period = PERIOD - 1;
  86. TIM_TimeBaseInit(TIM2, &TIM_Init_Struct);
  87. TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //IT enable
  88. TIM_Cmd(TIM2,ENABLE);
  89. // ---------------------------------------------------------------------
  90. // Init TIM3 for rps calculation
  91. TIM_Init_Struct.TIM_Prescaler = 7200 - 1; // 100 mcs per tick (0.1ms)
  92. TIM_Init_Struct.TIM_Period = 0xFFFF;
  93. TIM_TimeBaseInit(TIM3, &TIM_Init_Struct);
  94. TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
  95. TIM_Cmd(TIM3,ENABLE);
  96. // ---------------------------------------------------------------------
  97. // Init USART1 (Maya serial)
  98. USART_InitTypeDef USART_Init_Struct;
  99. USART_Init_Struct.USART_BaudRate = 115200;
  100. USART_Init_Struct.USART_Parity = USART_Parity_No;
  101. USART_Init_Struct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  102. USART_Init_Struct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  103. USART_Init_Struct.USART_StopBits = USART_StopBits_1;
  104. USART_Init_Struct.USART_WordLength = USART_WordLength_8b;
  105. USART_Init(USART1,&USART_Init_Struct);
  106. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // RXNE = ReceiveDataRegister Not Empty
  107. USART_ITConfig(USART1, USART_IT_ORE, DISABLE);
  108. USART_Cmd(USART1,ENABLE);
  109. // ---------------------------------------------------------------------
  110. // Init USART2 (PC serial)
  111. USART_Init_Struct.USART_BaudRate = 115200;
  112. USART_Init_Struct.USART_Parity = USART_Parity_No;
  113. USART_Init_Struct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  114. USART_Init_Struct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  115. USART_Init_Struct.USART_StopBits = USART_StopBits_1;
  116. USART_Init_Struct.USART_WordLength = USART_WordLength_8b;
  117. USART_Init(USART2,&USART_Init_Struct);
  118. USART_ITConfig(USART2, USART_IT_TC, DISABLE);
  119. USART_Cmd(USART2,ENABLE);
  120. // ---------------------------------------------------------------------
  121. // Init EXTI for Lichtschranke MAYA
  122. SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource4);
  123. EXTI_InitTypeDef EXTI_Init_Struct;
  124. EXTI_Init_Struct.EXTI_Line = EXTI_Line4;
  125. EXTI_Init_Struct.EXTI_LineCmd = ENABLE;
  126. EXTI_Init_Struct.EXTI_Mode = EXTI_Mode_Interrupt;
  127. EXTI_Init_Struct.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
  128. EXTI_Init(&EXTI_Init_Struct);
  129. // ---------------------------------------------------------------------
  130. // NVIC
  131. NVIC_InitTypeDef NVIC_Init_Struct;
  132. // USART2 (PC serial)
  133. NVIC_Init_Struct.NVIC_IRQChannel = USART2_IRQn;
  134. NVIC_Init_Struct.NVIC_IRQChannelCmd = ENABLE;
  135. NVIC_Init_Struct.NVIC_IRQChannelPreemptionPriority = 4;
  136. NVIC_Init_Struct.NVIC_IRQChannelSubPriority = 4;
  137. NVIC_Init(&NVIC_Init_Struct);
  138. // EXTI LICHTSCHRANKE
  139. NVIC_Init_Struct.NVIC_IRQChannel = EXTI4_IRQn;
  140. NVIC_Init_Struct.NVIC_IRQChannelCmd = ENABLE;
  141. NVIC_Init_Struct.NVIC_IRQChannelPreemptionPriority = 3;
  142. NVIC_Init_Struct.NVIC_IRQChannelSubPriority = 3;
  143. NVIC_Init(&NVIC_Init_Struct);
  144. // TIM3 (rps measurement)
  145. NVIC_Init_Struct.NVIC_IRQChannel = TIM3_IRQn;
  146. NVIC_Init_Struct.NVIC_IRQChannelCmd = ENABLE;
  147. NVIC_Init_Struct.NVIC_IRQChannelPreemptionPriority = 2;
  148. NVIC_Init_Struct.NVIC_IRQChannelSubPriority = 2;
  149. NVIC_Init(&NVIC_Init_Struct);
  150. // TIM2 (Stepping every PERIOD ms)
  151. NVIC_Init_Struct.NVIC_IRQChannel = TIM2_IRQn;
  152. NVIC_Init_Struct.NVIC_IRQChannelCmd = ENABLE;
  153. NVIC_Init_Struct.NVIC_IRQChannelPreemptionPriority = 1;
  154. NVIC_Init_Struct.NVIC_IRQChannelSubPriority = 1;
  155. NVIC_Init(&NVIC_Init_Struct);
  156. // USART1 (Maya serial)
  157. NVIC_Init_Struct.NVIC_IRQChannel = USART1_IRQn;
  158. NVIC_Init_Struct.NVIC_IRQChannelCmd = ENABLE;
  159. NVIC_Init_Struct.NVIC_IRQChannelPreemptionPriority = 0;
  160. NVIC_Init_Struct.NVIC_IRQChannelSubPriority = 0;
  161. NVIC_Init(&NVIC_Init_Struct);
  162. // ---------------------------------------------------------------------
  163. // Wrapup
  164. // GPIO_ResetBits(GPIOA, GPIO_Pin_5);
  165. // GPIO_ResetBits(GPIOA, GPIO_Pin_6);
  166. // ---------------------------------------------------------------------
  167. /* Infinite loop */
  168. while (1);
  169. }
  170. void USART1_IRQHandler(void) {
  171. // GPIO_SetBits(GPIOA, GPIO_Pin_6);
  172. char recvd1 = USART_ReceiveData(USART1);
  173. photo[(int)recvd1>>0x6] = recvd1 & 0x3F; // recvd>>0x6 = 0b 0000 00xx // 0x3F = 0b 0011 1111
  174. USART_ClearFlag(USART1, USART_FLAG_ORE);
  175. // GPIO_ResetBits(GPIOA, GPIO_Pin_6);
  176. }
  177. void USART2_IRQHandler(void) {
  178. if (buffer[bufferread] == '\r') {
  179. bufferread = 0;
  180. USART_SendData(USART2, '\r');
  181. USART_ITConfig(USART2, USART_IT_TC, DISABLE);
  182. }else {
  183. USART_SendData(USART2, buffer[bufferread++]);
  184. }
  185. }
  186. void EXTI4_IRQHandler(void){
  187. //GPIO_SetBits(GPIOA, GPIO_Pin_5);
  188. int ls1 = GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_3);
  189. int ls2 = GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_4);
  190. directions = directions<<1;
  191. if (ls1 == ls2) {
  192. pos++;
  193. directions |= 1;
  194. }
  195. else
  196. pos--;
  197. char last3 = directions & 7; // 7 = 0b0111
  198. if ((last3 == 5) || (last3 == 2)) { // 5 = 0b0101 // 2 = 0b0010 // if NULLPOSITION
  199. long temp = TIM_GetCounter(TIM3);
  200. if (temp != 0){
  201. rps = (10000) / (double)temp;
  202. TIM_SetCounter(TIM3, 0);
  203. sprintf(buffer, "%lf rps \r", rps);
  204. USART_ITConfig(USART2, USART_IT_TC, ENABLE);
  205. }else {
  206. TIM_Cmd(TIM3, ENABLE);
  207. rps = 0;
  208. sprintf(buffer, "%lf rps \r", rps);
  209. USART_ITConfig(USART2, USART_IT_TC, ENABLE);
  210. }
  211. if (pos >= 0)
  212. pos = 0;
  213. else
  214. pos = 229;
  215. }
  216. //GPIO_ResetBits(GPIOA, GPIO_Pin_5);
  217. EXTI_ClearITPendingBit(EXTI_Line4);
  218. }
  219. void TIM3_IRQHandler(void) {
  220. TIM_Cmd(TIM3, DISABLE);
  221. TIM_SetCounter(TIM3, 0);
  222. TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
  223. }
  224. void TIM2_IRQHandler(void) {
  225. // GPIO_SetBits(GPIOA, GPIO_Pin_5);
  226. // int x = photo[0] + photo[1] - photo[2] - photo[3];
  227. // int y = photo[0] - photo[1] + photo[2] - photo[3];
  228. //
  229. // double abs = sqrt(x*x+y*y);
  230. // double angle = atan(y/x);
  231. // if (x < 0)
  232. // angle += PI;
  233. // double rot = -pos * 2 * PI / 230;
  234. //
  235. // x = (int)(abs * cos(angle + rot));
  236. // y = (int)(abs * sin(angle + rot));
  237. double x1 = photo[0] + photo[1] - photo[2] - photo[3];
  238. double y1 = photo[0] - photo[1] + photo[2] - photo[3];
  239. double x = x1 * cos_lookup[pos] + y1 * sin_lookup[pos];
  240. double y = y1 * cos_lookup[pos] - x1 * sin_lookup[pos];
  241. //INVERSION IS INSIDE HERE AS WELL
  242. if (x > UNCERT)
  243. horistep(1);
  244. else if (x < -UNCERT)
  245. horistep(-1);
  246. if (y > UNCERT)
  247. vertstep(1);
  248. else if (y < -UNCERT)
  249. vertstep(-1);
  250. // GPIO_ResetBits(GPIOA, GPIO_Pin_5);
  251. TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
  252. }
  253. void vertstep(int direction){ // POSITIVE direction = DOWN
  254. vertstate += direction;
  255. if(vertstate > 7)
  256. vertstate = 0;
  257. if(vertstate < 0)
  258. vertstate = 7;
  259. switch(vertstate){
  260. case 0:
  261. GPIOB->ODR = 1<<9;
  262. break;
  263. case 1:
  264. GPIOB->ODR = 1<<9 | 1<<8;
  265. break;
  266. case 2:
  267. GPIOB->ODR = 1<<8;
  268. break;
  269. case 3:
  270. GPIOB->ODR = 1<<8 | 1<<7;
  271. break;
  272. case 4:
  273. GPIOB->ODR = 1<<7;
  274. break;
  275. case 5:
  276. GPIOB->ODR = 1<<7 | 1<<6;
  277. break;
  278. case 6:
  279. GPIOB->ODR = 1<<6;
  280. break;
  281. case 7:
  282. GPIOB->ODR = 1<<6 | 1<<9;
  283. break;
  284. default:
  285. GPIOB->ODR &= 0xF<<6;
  286. break;
  287. }
  288. }
  289. void horistep(int direction) {// POSITIVE direction = LEFT
  290. horistate += direction;
  291. if(horistate > 7)
  292. horistate = 0;
  293. if(horistate < 0)
  294. horistate = 7;
  295. switch(horistate){
  296. case 0:
  297. GPIOC->ODR = 1<<12;
  298. break;
  299. case 1:
  300. GPIOC->ODR = 1<<12 | 1<<11;
  301. break;
  302. case 2:
  303. GPIOC->ODR = 1<<11;
  304. break;
  305. case 3:
  306. GPIOC->ODR = 1<<11 | 1<<10;
  307. break;
  308. case 4:
  309. GPIOC->ODR = 1<<10;
  310. break;
  311. case 5:
  312. GPIOC->ODR = 1<<10 | 1<<9;
  313. break;
  314. case 6:
  315. GPIOC->ODR = 1<<9;
  316. break;
  317. case 7:
  318. GPIOC->ODR = 1<<9 | 1<<12;
  319. break;
  320. default:
  321. GPIOC->ODR &= 0xF<<9;
  322. break;
  323. }
  324. }

comments powered by Disqus