Advertisement
Guest User

Untitled

a guest
Jun 25th, 2010
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.51 KB | None | 0 0
  1. #include <NewSoftSerial.h>
  2. #include <TinyGPS.h>
  3. #include <Servo.h>
  4. #include <math.h>
  5. #include <EEPROM.h>
  6.  
  7. /* This sample code demonstrates the normal use of a TinyGPS object.
  8. It requires the use of NewSoftSerial, and assumes that you have a
  9. 4800-baud serial GPS device hooked up on pins 2(rx) and 3(tx).
  10. */
  11. TinyGPS gps;
  12. NewSoftSerial nss(4,13);
  13. Servo servo;
  14.  
  15.  
  16. //long targetLat = 3028369; //elyk
  17. //long targetLon = -8151467;
  18.  
  19.  
  20. //float targetLat = 29.898436; //frt
  21. //float targetLon = -81.312795;
  22.  
  23. float targetLat = 30.283151; //dog park
  24. float targetLon = -81.406214;
  25.  
  26. int targetDistance = 10000;//distance to target in meters
  27.  
  28. float targets[][2] =
  29. {
  30. {30.283151, -81.406214},
  31. {30.28369, -81.51467}
  32. };
  33.  
  34. char* hints[2] =
  35. {
  36. "Riley"
  37. "Andreas's Work",
  38. };
  39.  
  40. void gpsdump(TinyGPS &gps);
  41. bool feedgps();
  42. bool open = false;
  43. int offPin = 12;
  44. NewSoftSerial lcdSerialPort(5, 6);
  45. int currentWaypoint;
  46. void setup()
  47. {
  48. currentWaypoint = (int)getCurrentWaypoint();
  49. setBaudRate(115200);
  50.  
  51. clearScreen();
  52.  
  53. // Set a dim display
  54. setBackgroundBrightness(5);
  55. servo.attach(9);
  56. pinMode(offPin, OUTPUT);
  57. digitalWrite(offPin, LOW);
  58.  
  59. Serial.begin(115200);
  60. nss.begin(4800);
  61.  
  62. Serial.print("Testing TinyGPS library v. "); Serial.println(TinyGPS::library_version());
  63. Serial.println("by Mikal Hart");
  64. Serial.println();
  65. Serial.print("Sizeof(gpsobject) = "); Serial.println(sizeof(TinyGPS));
  66. Serial.println();
  67.  
  68. print("Hello. Acquiring signal. Please wait");
  69. }
  70.  
  71. void loop()
  72. {
  73. lockBox();
  74. bool newdata = false;
  75. int not_found = 0;
  76.  
  77. while(!newdata && not_found < 12)
  78. {
  79. Serial.print("newdata: ");
  80. Serial.println(newdata);
  81. long start = millis();
  82. while(millis()-start<5000)
  83. {
  84. if(feedgps())
  85. newdata = true;
  86. }
  87. print(".");
  88. not_found++;
  89. }
  90.  
  91.  
  92. if (newdata)
  93. {
  94. clearScreen();
  95. Serial.println("Acquired Data");
  96. Serial.println("-------------");
  97. gpsdump(gps);
  98. Serial.println("-------------");
  99. Serial.println();
  100. }
  101. else
  102. {
  103. if(not_found == 12) //60 seconds
  104. {
  105. clearScreen();
  106. print("No signal.");
  107. setX(0);
  108. setY(50);
  109. //servo.write(1000);
  110. unlockBox();
  111. print("Powering down. Bye");
  112. delay(10000);
  113. digitalWrite(offPin, HIGH);
  114. }
  115. }
  116. }
  117.  
  118. void gpsdump(TinyGPS &gps)
  119. {
  120. char buffer[12];
  121. char *str;
  122. float lat, lon;
  123. unsigned long age, date, time, chars;
  124. int year;
  125. float d;
  126. byte month, day, hour, minute, second, hundredths;
  127. unsigned short sentences, failed;
  128.  
  129. gps.f_get_position(&lat, &lon, &age);
  130. d = distance_between(lat, lon, targetLat, targetLon);
  131. if(d<targetDistance || open == true)
  132. {
  133. accessGranted();
  134. }
  135. else
  136. {
  137. lcdSerialPort.print(round(d/1000));
  138. lcdSerialPort.print("km");
  139. delay(5000);
  140. setX(15);
  141. setY(15);
  142. print("Powering down. Bye.");
  143. delay(5000);
  144. digitalWrite(offPin, HIGH);
  145. }
  146. Serial.print(d);
  147. Serial.print("Lat/Long(10^-5 deg): "); Serial.print(lat); Serial.print(", "); Serial.println(lon);
  148. Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms.");
  149.  
  150. feedgps(); // If we don't feed the gps during this long routine, we may drop characters and get checksum errors
  151. }
  152.  
  153. void accessGranted()
  154. {
  155. print("Access Granted!");
  156. unlockBox();
  157. print("Powering Off. Bye");
  158. delay(5000);
  159. digitalWrite(offPin, HIGH);
  160. }
  161.  
  162. void lockBox()
  163. {
  164. servo.write(90);
  165. }
  166. void unlockBox()
  167. {
  168. servo.write(185);
  169. }
  170.  
  171. bool feedgps()
  172. {
  173. while (nss.available())
  174. {
  175. if (gps.encode(nss.read()))
  176. return true;
  177. }
  178. return false;
  179. }
  180.  
  181. byte getCurrentWaypoint()
  182. {
  183. byte temp;
  184. temp = EEPROM.read(15);
  185. if(temp==255)
  186. {
  187. EEPROM.write(15,0);
  188. return 0;
  189. }
  190. return temp;
  191. }
  192.  
  193. float distance_between(float flat1, float flon1, float flat2, float flon2)
  194. {
  195. float dist_calc=0;
  196. float dist_calc2=0;
  197. float diflat=0;
  198. float diflon=0;
  199.  
  200. //I've to spplit all the calculation in several steps. If i try to do it in a single line the arduino will explode.
  201. diflat=radians(flat2-flat1);
  202. flat1=radians(flat1);
  203. flat2=radians(flat2);
  204. diflon=radians((flon2)-(flon1));
  205.  
  206. dist_calc = (sin(diflat/2.0)*sin(diflat/2.0));
  207. dist_calc2= cos(flat1);
  208. dist_calc2*=cos(flat2);
  209. dist_calc2*=sin(diflon/2.0);
  210. dist_calc2*=sin(diflon/2.0);
  211. dist_calc +=dist_calc2;
  212.  
  213. dist_calc=(2*atan2(sqrt(dist_calc),sqrt(1.0-dist_calc)));
  214.  
  215. dist_calc*=6371000.0; //Converting to meters
  216. //Serial.println(dist_calc);
  217. return dist_calc;
  218. }
  219.  
  220. void print(char *data){
  221. lcdSerialPort.print(data);
  222. }
  223.  
  224. void clearScreen(){
  225. lcdSerialPort.print(0x7C,BYTE);
  226. lcdSerialPort.print(0x00,BYTE);
  227. }
  228.  
  229. void demo(){
  230. lcdSerialPort.print(0x7C,BYTE);
  231. lcdSerialPort.print(0x04,BYTE);
  232. }
  233.  
  234. void toggleSplashScreen(){
  235. lcdSerialPort.print(0x7C,BYTE);
  236. lcdSerialPort.print(0x13,BYTE);
  237. }
  238.  
  239. void setBackgroundBrightness(byte value){
  240. lcdSerialPort.print(0x7C,BYTE);
  241. lcdSerialPort.print(0x02,BYTE);
  242. lcdSerialPort.print(value,BYTE);
  243. }
  244.  
  245. void setBaudRate(long value){
  246. // Get the internal reference for this baud rate
  247. char *ref = " ";
  248. if(value == 4800)
  249. ref = "1";
  250. else if(value == 9600)
  251. ref = "2";
  252. else if(value == 19200)
  253. ref = "3";
  254. else if(value == 38400)
  255. ref = "4";
  256. else if(value == 57600)
  257. ref = "5";
  258. else if(value == 115200)
  259. ref = "6";
  260. else
  261. return;
  262.  
  263. // Since it often rolls back to 115200, try setting it via 115200 first
  264. lcdSerialPort.begin(115200);
  265. lcdSerialPort.print(0x7C,BYTE);
  266. lcdSerialPort.print(0x07,BYTE);
  267. lcdSerialPort.print(ref);
  268.  
  269. // Now change the serial port to the desired rate, and set it again.
  270. lcdSerialPort.begin(value);
  271. lcdSerialPort.print(0x7C,BYTE);
  272. lcdSerialPort.print(0x07,BYTE);
  273. lcdSerialPort.print(ref);
  274. }
  275.  
  276. void setX(byte value){
  277. lcdSerialPort.print(0x7C,BYTE);
  278. lcdSerialPort.print(0x18,BYTE);
  279. lcdSerialPort.print(value,BYTE);
  280. }
  281.  
  282. void setY(byte value){
  283. lcdSerialPort.print(0x7C,BYTE);
  284. lcdSerialPort.print(0x19,BYTE);
  285. lcdSerialPort.print(value,BYTE);
  286. }
  287.  
  288. void setPixel(byte state){
  289. lcdSerialPort.print(0x50,BYTE);
  290. lcdSerialPort.print(0x40,BYTE);
  291. lcdSerialPort.print(state,BYTE);
  292. }
  293.  
  294. void drawLine(byte startX, byte startY, byte endX, byte endY, byte state){
  295. lcdSerialPort.print(0x7C,BYTE);
  296. lcdSerialPort.print(0x0C,BYTE);
  297. lcdSerialPort.print(startX,BYTE);
  298. lcdSerialPort.print(startY,BYTE);
  299. lcdSerialPort.print(endX,BYTE);
  300. lcdSerialPort.print(endY,BYTE);
  301. lcdSerialPort.print(state,BYTE);
  302. }
  303.  
  304. void drawCircle(byte startX, byte startY, byte radius, byte state){
  305. lcdSerialPort.print(0x7C,BYTE);
  306. lcdSerialPort.print(0x03,BYTE);
  307. lcdSerialPort.print(startX,BYTE);
  308. lcdSerialPort.print(startY,BYTE);
  309. lcdSerialPort.print(radius,BYTE);
  310. lcdSerialPort.print(state,BYTE);
  311. }
  312.  
  313. void drawBox(byte topLeftX, byte topLeftY, byte bottomRightX, byte bottomRightY, byte state){
  314. lcdSerialPort.print(0x7C,BYTE);
  315. lcdSerialPort.print(0x0F,BYTE);
  316. lcdSerialPort.print(topLeftX,BYTE);
  317. lcdSerialPort.print(topLeftY,BYTE);
  318. lcdSerialPort.print(bottomRightX,BYTE);
  319. lcdSerialPort.print(bottomRightY,BYTE);
  320. lcdSerialPort.print(state,BYTE);
  321. }
  322.  
  323. void eraseBox(byte topLeftX, byte topLeftY, byte bottomRightX, byte bottomRightY, byte state){
  324. lcdSerialPort.print(0x7C,BYTE);
  325. lcdSerialPort.print(0x05,BYTE);
  326. lcdSerialPort.print(topLeftX,BYTE);
  327. lcdSerialPort.print(topLeftY,BYTE);
  328. lcdSerialPort.print(bottomRightX,BYTE);
  329. lcdSerialPort.print(bottomRightY,BYTE);
  330. lcdSerialPort.print(state,BYTE);
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement