main.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*********************************************************************
  2. * analog example for Versa1.0
  3. * Analog capture on connectors K1, K2, K3 and K5.
  4. *********************************************************************/
  5. #define BOARD Versa1
  6. #include <fruit.h>
  7. #include <analog.h>
  8. #include <dcmotor.h>
  9. t_delay mainDelay;
  10. DCMOTOR_DECLARE(A);
  11. DCMOTOR_DECLARE(B);
  12. void setup(void) {
  13. //----------- Setup ----------------
  14. fruitInit();
  15. pinModeDigitalOut(LED); // set the LED pin mode to digital out
  16. digitalClear(LED); // clear the LED
  17. delayStart(mainDelay, 5000); // init the mainDelay to 5 ms
  18. //----------- Analog setup ----------------
  19. analogInit(); // init analog module
  20. analogSelect(0, MOTA_CURRENT); // assign MotorA current sense to analog channel 0
  21. analogSelect(1, MOTB_CURRENT); // assign MotorB current sense to analog channel 1
  22. //----------- dcmotor setup ----------------
  23. dcmotorInit(A);
  24. dcmotorInit(B);
  25. }
  26. void loop() {
  27. // ---------- Main loop ------------
  28. fraiseService(); // listen to Fraise events
  29. analogService(); // analog management routine
  30. if(delayFinished(mainDelay)) // when mainDelay triggers :
  31. {
  32. delayStart(mainDelay, 5000); // re-init mainDelay
  33. analogSend(); // send analog channels that changed
  34. DCMOTOR_COMPUTE(A,SYM);
  35. DCMOTOR_COMPUTE(B,SYM);
  36. }
  37. }
  38. // Receiving
  39. void fraiseReceiveChar() // receive text
  40. {
  41. unsigned char c;
  42. c=fraiseGetChar();
  43. if(c=='L'){ //switch LED on/off
  44. c=fraiseGetChar();
  45. digitalWrite(LED, c!='0');
  46. }
  47. else if(c=='E') { // echo text (send it back to host)
  48. printf("C");
  49. c = fraiseGetLen(); // get length of current packet
  50. while(c--) printf("%c",fraiseGetChar());// send each received byte
  51. putchar('\n'); // end of line
  52. }
  53. }
  54. void fraiseReceive() // receive raw
  55. {
  56. unsigned char c;
  57. c=fraiseGetChar();
  58. switch(c) {
  59. case 120 : DCMOTOR_INPUT(A) ; break;
  60. case 121 : DCMOTOR_INPUT(B) ; break;
  61. }
  62. }