Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.64 KB | None | 0 0
  1. /*
  2. * HW06-RobotRC.xc
  3. *
  4. * Created on: Oct 27, 2016
  5. * Author: harrison
  6. */
  7.  
  8. #include <xs1.h>
  9. #include <print.h>
  10. #include <string.h>
  11. #include <stdio.h>
  12. //defines
  13. #define BAUD_RATE 9600
  14. #define TICKS_PER_US (XS1_TIMER_HZ/1000000)
  15. #define TICKS_PER_SEC (XS1_TIMER_HZ)
  16. #define TICKS_PER_MS (XS1_TIMER_HZ/1000)
  17. #define PWM_FRAME_TICKS (TICKS_PER_MS)
  18.  
  19. #define MESSAGE_SIZE 128
  20. #define BIN1_ON 0b0010 //BIN1 -> D17 -> P4D1
  21. #define BIN2_ON 0b1000 //BIN2 -> D19 -> P4D3
  22. #define AIN1_ON 0b0100 //AIN1 -> D18 -> P4D2 //CCW
  23. #define AIN2_ON 0b0001 //AIN2 -> D16 -> P4D0 //CW
  24.  
  25. //Ports
  26. in port iButton = XS1_PORT_1C;
  27. out port oButtonDriver = XS1_PORT_1B;
  28. out port oLED = XS1_PORT_1A;
  29. out port oSTB = XS1_PORT_1O;
  30. out port oWiFiRX = XS1_PORT_1F;
  31. in port iWiFiTX = XS1_PORT_1H;
  32.  
  33. out port oMotorPWMA = XS1_PORT_1P; //PWMA -> D39 -> XS1_PORT_1P
  34. out port oMotorPWMB = XS1_PORT_1I; //PWMB -> D24 -> P1I
  35. out port oMotorControl = XS1_PORT_4D;
  36. //out port oLED = XS1_PORT_1A;
  37. //out port oSTB = XS1_PORT_1O; //stanby, you need to send a 1 or else IT WONT WORK
  38. in port iEncoders = XS1_PORT_4C;
  39. //out port oLEDOne = XS1_PORT_1A;
  40. out port oLEDTwo = XS1_PORT_1D;
  41.  
  42. //Prototypes
  43.  
  44. void multi_motor_task(out port oLeftPWM, out port oRightPWM, out port oMotorControl, chanend in_motor_cmd_chan);
  45. void uart_transmit_byte(out port oPort, char value, unsigned int baudrate);
  46. char uart_receive_byte(in port iPort, unsigned int baudrate);
  47. void toggle_port(out port oLED, unsigned int hz);
  48. void uart_transmit_bytes(out port oPort, const char values[], unsigned int baudrate);
  49. void uart_to_console_task(chanend trigger_chan, chanend in_motor_cmd_chan);
  50. void line(const char buffer[]);
  51. void send_hello_world_program();
  52. void output_task(chanend trigger_chan);
  53. void send_wifi_setup();
  54. void full_speed(chanend in_motor_cmd_chan);
  55. void half_speed(chanend in_motor_cmd_chan);
  56. void full_reverse(chanend in_motor_cmd_chan);
  57. void half_reverse(chanend in_motor_cmd_chan);
  58. void leftTurn(chanend in_motor_cmd_chan);
  59. void rightTurn(chanend in_motor_cmd_chan);
  60. void stop(chanend in_motor_cmd_chan);
  61. void computeDifference();
  62.  
  63.  
  64. typedef struct {
  65. char data[MESSAGE_SIZE];
  66. } message_t;
  67.  
  68. typedef struct {
  69. int left_duty_cycle;
  70. int right_duty_cycle;
  71. } motor_cmd_t;
  72.  
  73.  
  74.  
  75. int main_single();
  76. int main_array();
  77.  
  78. int main()
  79. {
  80. chan trigger;
  81. chan motor_cmd_chan;
  82.  
  83. oWiFiRX <: 1;
  84. par
  85. {
  86. uart_to_console_task(trigger,motor_cmd_chan);
  87. multi_motor_task(oMotorPWMA, oMotorPWMB, oMotorControl, motor_cmd_chan);
  88. output_task(trigger);
  89. }
  90. }
  91.  
  92.  
  93. void full_speed(chanend in_motor_cmd_chan)
  94. {
  95. motor_cmd_t full_speed;
  96. full_speed.left_duty_cycle = 100;
  97. full_speed.right_duty_cycle = 100;
  98. in_motor_cmd_chan <: full_speed;
  99. }
  100.  
  101. void half_speed(chanend in_motor_cmd_chan)
  102. {
  103. motor_cmd_t half_speed;
  104. half_speed.left_duty_cycle = 50;
  105. half_speed.right_duty_cycle = 50;
  106. in_motor_cmd_chan <: half_speed;
  107. }
  108.  
  109. void full_reverse(chanend in_motor_cmd_chan)
  110. {
  111. motor_cmd_t full_reverse;
  112. full_reverse.left_duty_cycle = -100;
  113. full_reverse.right_duty_cycle = -100;
  114. in_motor_cmd_chan <: full_reverse;
  115. }
  116.  
  117. void half_reverse(chanend in_motor_cmd_chan)
  118. {
  119. motor_cmd_t half_reverse;
  120. half_reverse.left_duty_cycle = -50;
  121. half_reverse.right_duty_cycle = -50;
  122. in_motor_cmd_chan <: half_reverse;
  123. }
  124.  
  125. void stop(chanend in_motor_cmd_chan)
  126. {
  127. motor_cmd_t stop;
  128. stop.left_duty_cycle = 0;
  129. stop.right_duty_cycle = 0;
  130. in_motor_cmd_chan <: stop;
  131. }
  132.  
  133. void rightTurn(chanend in_motor_cmd_chan)
  134. {
  135. motor_cmd_t rightTurn;
  136. rightTurn.left_duty_cycle = 50;
  137. rightTurn.right_duty_cycle = 75;
  138. in_motor_cmd_chan <: rightTurn;
  139. }
  140.  
  141. void leftTurn(chanend in_motor_cmd_chan)
  142. {
  143. motor_cmd_t leftTurn;
  144. leftTurn.left_duty_cycle = 75;
  145. leftTurn.right_duty_cycle = 50;
  146. in_motor_cmd_chan <: leftTurn;
  147. }
  148.  
  149. void output_task(chanend trigger_chan)
  150. {
  151. while(1)
  152. {
  153. select
  154. {
  155. case trigger_chan :> message_t value :
  156. if(strcmp(value.data, "send_wifi_setup") == 0)
  157. {
  158. send_wifi_setup();
  159. }
  160. else
  161. {
  162. line(value.data);
  163. }
  164. break;
  165.  
  166. }
  167. }
  168. }
  169.  
  170. void send_wifi_setup()
  171. {
  172. line("wifi.setmode(wifi.SOFTAP)");
  173. line("cfg={}");
  174. line("cfg.ssid=\"MydadisObama\"");
  175. line("cfg.pwd=\"trump\"");
  176. line("cfg.ip=\"192.168.0.1\"");
  177. line("cfg.netmask=\"255.255.255.0\"");
  178. line("cfg.gateway=\"192.168.0.1\"");
  179. line("port = 9876");
  180. line("wifi.ap.setip(cfg)");
  181. line("wifi.ap.config(cfg)");
  182. line("print(\"ESP8266 TCP to Serial Bridge v1.0 by RoboRemo\")");
  183. line("tmr.alarm(0,200,0,function() -- run after a delay ");
  184. //line("uart.setup(0, 9600, 8, 0, 1, 1)");
  185. line("srv=net.createServer(net.TCP, 28800)");
  186. line("srv:listen(port,function(conn)");
  187. line("uart.on(\"data\", 0, function(data)");
  188. line("conn:send(data) end, 0)");
  189. line("conn:on(\"receive\",function(conn,payload)");
  190. line("uart.write(0, payload)");
  191. line("end)");
  192. line("conn:on(\"disconnection\",function(c)");
  193. line(" uart.on(\"data\")");
  194. line("end)");
  195. line("end)");
  196. line("end)");
  197. //line("");
  198. //line("");
  199. //line("");
  200.  
  201. }
  202.  
  203. void send_hello_world_program()
  204. {
  205. // printstr("running...");
  206. line("gpio.mode(3, gpio.OUTPUT)");
  207. line("while 1 do");
  208. line("gpio.write(3, gpio.HIGH)");
  209. line("tmr.delay(1000000)");
  210. line("gpio.write(3, gpio.LOW)");
  211. line("tmr.delay(1000000)");
  212. line("end");
  213. }
  214.  
  215.  
  216. void line(const char buffer[])
  217. {
  218. timer tmr;
  219. unsigned int time;
  220. tmr :> time;
  221. time += (TICKS_PER_SEC/8);
  222. tmr when timerafter(time) :> void;
  223. uart_transmit_bytes(oWiFiRX, buffer, BAUD_RATE);
  224. uart_transmit_bytes(oWiFiRX, "\r\n", BAUD_RATE);
  225. //printstr("hey");
  226.  
  227. }
  228.  
  229. void uart_to_console_task(chanend trigger_chan, chanend in_motor_cmd_chan)
  230. {
  231. char currChar;
  232. char buffer[64];
  233. int currElement = 0;
  234. message_t m;
  235. strncpy(m.data, "send_wifi_setup", 20);
  236.  
  237. while(1)
  238. {
  239. currChar = uart_receive_byte(iWiFiTX, BAUD_RATE);
  240. buffer[currElement] = currChar;
  241. currElement++;
  242. if(currElement == 63 || currChar == '\r' || currChar == '\n')
  243. {
  244. //printstrln(buffer);
  245. buffer[currElement] = '\0';
  246. if(strcmp(buffer, "lua: cannot open init.lua\r") == 0)
  247. {
  248. //printstrln(buffer);
  249. trigger_chan <: m;
  250. }
  251. else if(strcmp(buffer, "F\r") == 0)
  252. {
  253. full_speed(in_motor_cmd_chan);
  254. }
  255. else if(strcmp(buffer, "f\r") == 0)
  256. {
  257. half_speed(in_motor_cmd_chan);
  258. }
  259. else if(strcmp(buffer, "R\r") == 0)
  260. {
  261. full_reverse(in_motor_cmd_chan);
  262. }
  263. else if(strcmp(buffer, "r\r") == 0)
  264. {
  265. half_reverse(in_motor_cmd_chan);
  266. }
  267. else if(strcmp(buffer, "<\r") == 0)
  268. {
  269. leftTurn(in_motor_cmd_chan);
  270. }
  271. else if(strcmp(buffer, ">\r") == 0)
  272. {
  273. rightTurn(in_motor_cmd_chan);
  274. }
  275. else if(strcmp(buffer, "x\r") == 0)
  276. {
  277. stop(in_motor_cmd_chan);
  278. }
  279. // else if(strcmp(buffer, "?\r") == 0)
  280. // {
  281. // computeDifference();
  282. // }
  283.  
  284.  
  285. currElement = 0;
  286. }
  287.  
  288. }
  289. }
  290.  
  291. void toggle_port(out port oLED, unsigned int hz)
  292. {
  293. timer tmr;
  294. unsigned int t;
  295. unsigned pattern = 0b1;
  296. unsigned int delay = XS1_TIMER_HZ / hz;
  297.  
  298. oLED <: pattern;
  299. tmr :> t;
  300. while (1)
  301. {
  302. oLED <: pattern;
  303. t += delay;
  304. tmr when timerafter(t) :> void;
  305. pattern = ~pattern;
  306. }
  307.  
  308. return 0;
  309.  
  310. }
  311.  
  312. void uart_transmit_bytes(out port oPort, const char values[], unsigned int baudrate)
  313. {
  314. //for(int i = 0; i != '\0'; i++)
  315. //{
  316. char val;
  317. int i = 0;
  318. while(values[i] != '\0')
  319. {
  320.  
  321. uart_transmit_byte(oPort, values[i], baudrate);
  322. i++;
  323. //printstr("hey");
  324. }
  325. // }
  326.  
  327. }
  328.  
  329.  
  330.  
  331. //Function Defnitions
  332. void uart_transmit_byte(out port oPort, char value, unsigned int baudrate)
  333. {
  334.  
  335. timer tmr;
  336. unsigned int time;
  337. const unsigned int bitTime = XS1_TIMER_HZ / baudrate;
  338. unsigned int val = value;
  339.  
  340.  
  341. tmr :> time;
  342. //output start bit
  343. oPort <: 0;
  344. time += bitTime;
  345. tmr when timerafter(time) :> void;
  346.  
  347. //output byte
  348. for(int i = 0; i < 8; i++)
  349. {
  350. oPort <: (val & 0x1);
  351. val >>= 1;
  352. //might need to do shift in seperate statement
  353. time += bitTime;
  354. tmr when timerafter(time) :> void;
  355. }
  356.  
  357. //output stop bit
  358. oPort <: 1;
  359. time += bitTime;
  360. tmr when timerafter(time) :> void;
  361.  
  362. }
  363.  
  364. char uart_receive_byte(in port iPort, unsigned int baudrate)
  365. {
  366. timer tmr;
  367. unsigned int time, value;
  368. const unsigned int bitTime = XS1_TIMER_HZ / baudrate;
  369.  
  370. iPort when pinseq(1) :> void;
  371.  
  372. iPort when pinseq(0) :> void;
  373. tmr :> time;
  374. time += bitTime/2;
  375.  
  376. //input data bits
  377. value = 0;
  378. for(int i = 0; i < 8; i++)
  379. {
  380. time += bitTime;
  381. tmr when timerafter(time) :> void;
  382. iPort :> >> value;
  383.  
  384. }
  385.  
  386. // input stop bit //
  387. time += bitTime;
  388. tmr when timerafter(time) :> void;
  389.  
  390. return (char) (value >> 24);
  391.  
  392. }
  393.  
  394. void multi_motor_task(out port oLeftPWM, out port oRightPWM, out port oMotorControl, chanend in_motor_cmd_chan)
  395. {
  396. int lastLeft = 0;
  397. int lastRight = 0;
  398. int timeout = 0;
  399. unsigned int leftTicks;
  400. unsigned int rightTicks;
  401. unsigned int delayLeft;
  402. unsigned int delayRight;
  403. unsigned int delayPWM;
  404. unsigned int mask;
  405. timer timerLeft;
  406. timer timerRight;
  407. timer timerPWM;
  408.  
  409. while(1){
  410.  
  411. leftTicks = lastLeft * (PWM_FRAME_TICKS / 100);
  412. rightTicks = lastRight * (PWM_FRAME_TICKS / 100);
  413. oMotorControl <: mask;
  414.  
  415. timeout = 0;
  416.  
  417. oLeftPWM <: 1;
  418. oRightPWM <: 1;
  419.  
  420. timerLeft :> delayLeft;
  421. timerRight :> delayRight;
  422. timerPWM :> delayPWM;
  423. delayLeft += leftTicks;
  424. delayRight += rightTicks;
  425. delayPWM += PWM_FRAME_TICKS;
  426.  
  427. while(timeout == 0){
  428. select{
  429.  
  430. case timerLeft when timerafter(delayLeft) :> void:
  431. oLeftPWM <: 0;
  432. delayLeft += XS1_TIMER_HZ;
  433. break;
  434. case timerRight when timerafter(delayRight) :> void:
  435. oRightPWM <: 0;
  436. delayRight += XS1_TIMER_HZ;
  437. break;
  438. case timerPWM when timerafter(delayPWM) :> void:
  439. timeout = 1;
  440. break;
  441. case in_motor_cmd_chan :> motor_cmd_t currentDuty:
  442.  
  443. lastLeft = currentDuty.left_duty_cycle;
  444. lastRight = currentDuty.right_duty_cycle;
  445.  
  446. if(currentDuty.left_duty_cycle < 0){
  447. lastLeft = currentDuty.left_duty_cycle * -1;
  448. mask = AIN1_ON;
  449. }else{
  450. mask = AIN2_ON;
  451. }
  452.  
  453. if(currentDuty.right_duty_cycle < 0){
  454. lastRight = currentDuty.right_duty_cycle * -1;
  455. mask = (BIN2_ON | mask);
  456. }else{
  457. mask = (BIN1_ON | mask);
  458. }
  459. break;
  460. }
  461. }
  462. }
  463. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement