Advertisement
Guest User

CANSAT

a guest
Jan 22nd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1.  
  2.  
  3. #include <SoftwareSerial.h>
  4. /* plug in apc220 at pin 8-GND with apc pcb pointing away from Arduino
  5. Jens(AAU), Thomas(ARS)
  6. */
  7. const int vrs = 102;
  8. const int rxPin = 10; //rx pin is tx pin on apc
  9. const int txPin = 11; //tx pin is rx pin on apc
  10. const int setPin = 8; //set pin
  11.  
  12. const int enPin = 12; // no use
  13. const int fiveV = 13; // 5V to the APC220
  14. int echo_mode = 0;
  15. #define TIMEOUT 10000L
  16. #define PING_INTERVAL 2000L
  17. unsigned long timeout, lastPing;
  18. #define PARM_LGT 10
  19. #define CMD_BUF_LGT 50
  20. char var1[PARM_LGT], var2[PARM_LGT], var3[PARM_LGT], var4[PARM_LGT], var5[PARM_LGT],
  21. var6[PARM_LGT];
  22. SoftwareSerial apc220(rxPin, txPin); // Crt softserial port and bind tx/rx to appropriate PINS
  23. void drawRadio()
  24. {
  25. Serial.println(F("Attach radio directly to Arduino on pin 8 to gnd"));
  26. Serial.println(F(" "));
  27. Serial.println(F("---------------"));
  28. Serial.println(F(" A aref"));
  29. Serial.println(F(" R GND-------------------"));
  30. Serial.println(F(" D 13 |=antenna connector ->"));
  31. Serial.println(F("IOREF U 12 |"));
  32. Serial.println(F("RESET I 11 |"));
  33. Serial.println(F("3.3V N 10 |"));
  34. Serial.println(F("5V O 09 |"));
  35. Serial.println(F("GND 08--------------------"));
  36. Serial.println(F("GND P |"));
  37. Serial.println(F("Vin C 07"));
  38. Serial.println(F("| B ~ 06"));
  39. Serial.println(F("A0 ~ 05"));
  40. Serial.println(F("A1 04"));
  41. Serial.println(F("A2 ~ 03"));
  42. Serial.println(F("A3 02"));
  43. Serial.println(F("A4 TX 01"));
  44. Serial.println(F("A5 RX 00"));
  45. Serial.println(F("---------------"));
  46. }
  47. void set_para(char hz[], char rf_rate[], char pwr[], char uart_rate[], char sc[]) {
  48. // sanity chk
  49. if (strlen(hz) != 6) {
  50. Serial.println("Freq parm not 6 digits... - legal is 418000 - 455000");
  51. return;
  52. }
  53. if (strlen(rf_rate) != 1 ) {
  54. Serial.println("RF parm is not 1 digit: legal values is 1/2/3/4");
  55. return;
  56. }
  57. if (strlen(pwr) != 1 ) {
  58. Serial.println("Power parm is not 1 digit: legal values is 1..9");
  59. return;
  60. }
  61. if (strlen(uart_rate) != 1 ) {
  62. Serial.println("Uart baudrate parm is not 1 digit: legal values is 0..6");
  63. return;
  64. }
  65. if (strlen(sc) != 1 ) {
  66. Serial.println("Parity parm is not 1 digit: legal values is 0/1/2");
  67. return;
  68. }
  69. digitalWrite(setPin, LOW); // set radio in config mode
  70. delay(50);
  71. apc220.print("WR");
  72. apc220.print(" ");
  73.  
  74. apc220.print(hz);
  75. apc220.print(" ");
  76. apc220.print(rf_rate);
  77. apc220.print(" ");
  78.  
  79. apc220.print(pwr);
  80. apc220.print(" ");
  81.  
  82. apc220.print(uart_rate);
  83. apc220.print(" ");
  84.  
  85. apc220.print(sc);
  86.  
  87. apc220.write(0x0D);
  88. apc220.write(0x0A);
  89. delay(10);
  90. // read feedback from radio
  91. while (apc220.available()) {
  92. Serial.write(apc220.read());
  93. }
  94. digitalWrite(setPin, HIGH); // set radio back in normal rx/tx mode
  95. }
  96. void get_para(void) {
  97. int i = 0;
  98. char buff[CMD_BUF_LGT];
  99. digitalWrite(setPin, LOW); // set radio in config mode
  100.  
  101. delay(10); // stabilize please
  102.  
  103. apc220.println("RD"); // ask for data
  104. delay(10);
  105.  
  106. while (apc220.available()) {
  107. Serial.write(apc220.read());
  108. }
  109. digitalWrite(setPin, HIGH); // set radio back in normal tx/rx mode
  110. }
  111. void resetPing(void) {
  112. echo_mode = 0;
  113. timeout = millis();
  114. }
  115. void helpMenu(void) {
  116. Serial.println("");
  117. Serial.println(F("commands:"));
  118. Serial.println(F(" r : Read apc220 radio config"));
  119. Serial.println(F(" e : go into echo mode: receive char, add 1 and return"));
  120. Serial.println(F(" n : no more echo - back to normal"));
  121. Serial.println(F(" p: print how to attach radio to Arduino"));
  122. Serial.println(F(" w : Write apc radio config ..."));
  123. Serial.println(F(" 'W' FFFFFF R P B C - number of letters indicates precise number of digits" ));
  124. Serial.println(F(" FFFFFF: frequency: 434000 (434 MHz) range 418000-455000 "));
  125. Serial.println(F(" R: Rf data rate - 1/2/3/4 equals 2400/4800/9600/19200bps"));
  126. Serial.println(F(" P: Radio output power - 0 .. 9 9 equals 13dBm(20mW)."));
  127. Serial.println(F(" B: UART baudrate - 0/1/2/3/4/5/6 equals 1200/2400/4800/9600/19200/38400/57600bps"));
  128. Serial.println(F(" C: Byte Chek Parity - 0/1/2 equals NoCheck(8N1)/EvenParity(8E1)/OddParity(8O1)"));
  129. Serial.println(F(""));
  130. Serial.println(F("Write example: w 434000 3 9 3 0 is..."));
  131. Serial.println(F(" 434,000 MHz 9600 baud in air, 20mW, 9600baud on UART, No Parity(8N1)"));
  132. Serial.println(F("After 10 seconds with no keyboard input we will emit a char every two second"));
  133. }
  134. void get_cmd(void) {
  135. if ((echo_mode == 0) && (TIMEOUT < (millis() - timeout))) { // time and state for beacon mode ?
  136. apc220.write('!');
  137. Serial.println("going into ping mode - emit a char every two second on apc220 !");
  138. lastPing = millis();
  139. echo_mode = 2;
  140. goto xxx;
  141. }
  142. if (echo_mode == 2) { // beacon mode
  143. if (PING_INTERVAL < (millis() - lastPing)) {
  144. apc220.write('x');
  145. Serial.println(lastPing / 1000);
  146. lastPing = millis();
  147. }
  148. goto xxx;
  149. }
  150. if (echo_mode == 1) { // echo mode
  151. char c;
  152.  
  153. if (apc220.available()) {
  154. c = apc220.read();
  155. Serial.write(c);
  156. apc220.write(c + 1);
  157. }
  158. goto xxx;
  159. }
  160.  
  161. xxx:
  162. if (Serial.available()) {
  163. int i = 0;
  164. char buff[CMD_BUF_LGT];
  165.  
  166. delay(100);
  167.  
  168. while (Serial.available() && (i < CMD_BUF_LGT - 1)) {
  169. buff[i++] = Serial.read();
  170. }
  171. buff[i] = '\0';
  172. Serial.println(buff); //echo input
  173. resetPing();
  174. var1[0] = 0x00; // reset
  175. sscanf(buff, "%s %s %s %s %s %s", var1, var2, var3, var4, var5, var6);
  176. switch (var1[0]) { // one letter commands :-)
  177. case 'r': {
  178. get_para();
  179. break;
  180. }
  181. case 'w': {
  182. set_para(var2, var3, var4, var5, var6);
  183. break;
  184. }
  185. case 'e' : {
  186. echo_mode = 1;
  187. break;
  188. }
  189. case 'n' : {
  190. echo_mode = 0;
  191. break;
  192. }
  193. case 'p': {
  194. drawRadio();
  195. delay(2000);
  196. break;
  197. }
  198. default:
  199. echo_mode = 0;
  200. helpMenu();
  201. }
  202. }
  203. }
  204. void setupSoftAPC(void)
  205. {
  206. pinMode(setPin, OUTPUT);
  207. digitalWrite(setPin, HIGH);
  208. pinMode(fiveV, OUTPUT); // 5V
  209. digitalWrite(fiveV, HIGH); // turn on 5V
  210. delay(50);
  211. pinMode(enPin, OUTPUT); // ENABLE
  212. digitalWrite(enPin, HIGH); //
  213. delay(100);
  214. apc220.begin(9600);
  215. }
  216. void setup() {
  217. Serial.begin(9600);
  218. Serial.print("APC version: ");
  219. Serial.println(vrs);
  220. Serial.println(__DATE__);
  221. setupSoftAPC();
  222. timeout = millis(); // just to start
  223. }
  224. void loop() {
  225. get_cmd();
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement