[关闭]
@rg070836rg 2017-05-09T10:18:31.000000Z 字数 7568 阅读 1165

同步文档

同步


http://www.linuxidc.com/Linux/2016-05/131096.htm

  1. { "extensionName": "Demo2",
  2. "description": "A Demo Extension for Arduino",
  3. "version": "1.1",
  4. "author": "Seeed Studio(support@seed.com)",
  5. "homepage": "seeed.com",
  6. "sort":0,
  7. "javascriptURL":"js/demo.js",
  8. "firmware":"1.0",
  9. "extensionPort":0,
  10. "blockSpecs": [
  11. ["h","Demo Program","runArduino"],
  12. [
  13. "w",
  14. "digitalWrite( %n , %d.digital )",
  15. "digitalWrite",
  16. "13",
  17. "HIGH",
  18. {
  19. "setup":"pinMode({0},OUTPUT); \n",
  20. "inc":"",
  21. "def":"",
  22. "work":"digitalWrite({0},{1});\n",
  23. "loop":""
  24. }
  25. ],
  26. [
  27. "w",
  28. "blink",
  29. "blink",
  30. {
  31. "setup":"",
  32. "inc":"#include \"demo.h\"",
  33. "def":"DemoClass demo; \n",
  34. "work":"demo.blink(); \n",
  35. "loop":""
  36. }
  37. ]
  38. ],
  39. "menus": {
  40. "digital":["HIGH","LOW"]
  41. },
  42. "values":{
  43. "HIGH":1,
  44. "LOW":0
  45. },
  46. "translators":{
  47. "zh_CN":{
  48. "Demo Program":"演示程序",
  49. "HIGH":"高电平",
  50. "LOW":"低电平",
  51. "digitalWrite( %n , %d.digital )":"数字口输出( %n ,%d.digital )",
  52. "blink":"闪烁"
  53. }
  54. }
  55. }
  1. /*
  2. This program is for this robot http://www.thingiverse.com/thing:1912377
  3. */
  4. #include <Arduino.h>
  5. #include <Servo.h>
  6. #include "Oscillator.h"
  7. #include <SoftwareSerial.h>
  8. //comment out below line if you don't use HC_SR04 sensor
  9. #define __HC_SR04__
  10. #ifdef __HC_SR04__
  11. #define HC_SR04_TRIGGER_PIN 10
  12. #define HC_SR04_ECHO_PIN 11
  13. #define MIN_DISTANCE 10
  14. #define MAX_DISTANCE MIN_DISTANCE + 10
  15. #endif
  16. #define SERVO_START_PIN 2
  17. #define TIMEOUT 1000
  18. #define FORWARD 'f'
  19. #define LEFT 'l'
  20. #define STAND 's'
  21. #define RIGHT 'r'
  22. #define BACKWARD 'b'
  23. #define RIGHT_FRONT 'c'
  24. // servo ID
  25. #define FRONT_RIGHT_0 0
  26. #define FRONT_RIGHT_1 1
  27. #define BACK_RIGHT_0 2
  28. #define BACK_RIGHT_1 3
  29. #define BACK_LEFT_0 4
  30. #define BACK_LEFT_1 5
  31. #define FRONT_LEFT_0 6
  32. #define FRONT_LEFT_1 7
  33. typedef struct {
  34. int index;
  35. int degree;
  36. int delay;
  37. } angle_t;
  38. angle_t *forward = (angle_t []) {
  39. {FRONT_RIGHT_0, 140, 0},
  40. {FRONT_RIGHT_1, 60, 0},
  41. {BACK_RIGHT_0, 40, 0},
  42. {BACK_RIGHT_1, 90, 0},
  43. {BACK_LEFT_0, 150, 0},
  44. {BACK_LEFT_1, 60, 0},
  45. {FRONT_LEFT_0, 55, 0},
  46. {FRONT_LEFT_1, 90, 200},
  47. {BACK_LEFT_0, 80, 0},
  48. {FRONT_LEFT_0, 100, 200},
  49. {FRONT_RIGHT_1, 90, 0},
  50. {BACK_RIGHT_1, 120, 0},
  51. {BACK_LEFT_1, 110, 0},
  52. {FRONT_LEFT_1, 120, 200},
  53. {FRONT_RIGHT_0, 80, 0},
  54. {BACK_RIGHT_0, 100, 0},
  55. {BACK_RIGHT_1, 90, 0},
  56. {BACK_LEFT_0, 125, 0},
  57. {FRONT_LEFT_0, 55, 200},
  58. { -1, -1, -1}
  59. };
  60. angle_t *backward = (angle_t []) {
  61. {BACK_LEFT_0, 140, 0},
  62. {BACK_LEFT_1, 60, 0},
  63. {FRONT_LEFT_0, 40, 0},
  64. {FRONT_LEFT_1, 90, 0},
  65. {FRONT_RIGHT_0, 150, 0},
  66. {FRONT_RIGHT_1, 60, 0},
  67. {BACK_RIGHT_0, 55, 0},
  68. {BACK_RIGHT_1, 90, 200},
  69. {FRONT_RIGHT_0, 80, 0},
  70. {BACK_RIGHT_0, 100, 200},
  71. {BACK_LEFT_1, 90, 0},
  72. {FRONT_LEFT_1, 120, 0},
  73. {FRONT_RIGHT_1, 110, 0},
  74. {BACK_RIGHT_1, 120, 200},
  75. {BACK_LEFT_0, 80, 0},
  76. {FRONT_LEFT_0, 100, 0},
  77. {FRONT_LEFT_1, 90, 0},
  78. {FRONT_RIGHT_0, 125, 0},
  79. {BACK_RIGHT_0, 55, 200},
  80. { -1, -1, -1}
  81. };
  82. #define LEG_UP 50
  83. #define LEG_DN 90
  84. #define LEG_CLOSE 50 // move inward
  85. #define LEG_OPEN 150 // move outward
  86. #define LEG_TURN 125
  87. #define INV(x) (180-x)
  88. angle_t *right = (angle_t []) {
  89. {FRONT_RIGHT_1, LEG_UP, 0},
  90. {BACK_LEFT_1, LEG_UP, 100},
  91. {FRONT_RIGHT_0, LEG_CLOSE, 0},
  92. {BACK_LEFT_0, LEG_CLOSE, 100},
  93. //
  94. {FRONT_RIGHT_1, LEG_DN, 0},
  95. {BACK_LEFT_1, LEG_DN, 100},
  96. {BACK_RIGHT_1, LEG_OPEN, 0},
  97. {FRONT_LEFT_1, LEG_OPEN, 100},
  98. //
  99. {FRONT_RIGHT_0, LEG_TURN, 0},
  100. {BACK_LEFT_0, LEG_TURN, 100},
  101. {BACK_RIGHT_1, LEG_DN, 0},
  102. {FRONT_LEFT_1, LEG_DN, 100},
  103. { -1, -1, -1}
  104. };
  105. angle_t *left = (angle_t []) {
  106. {FRONT_LEFT_1, INV(LEG_UP), 0},
  107. {BACK_RIGHT_1, INV(LEG_UP), 100},
  108. {FRONT_LEFT_0, INV(LEG_CLOSE), 0},
  109. {BACK_RIGHT_0, INV(LEG_CLOSE), 100},
  110. //
  111. {FRONT_LEFT_1, INV(LEG_DN), 0},
  112. {BACK_RIGHT_1, INV(LEG_DN), 100},
  113. {BACK_LEFT_1, INV(LEG_OPEN), 0},
  114. {FRONT_RIGHT_1, INV(LEG_OPEN), 100},
  115. //
  116. {FRONT_LEFT_0, INV(LEG_TURN), 0},
  117. {BACK_RIGHT_0, INV(LEG_TURN), 100},
  118. {BACK_LEFT_1, INV(LEG_DN), 0},
  119. {FRONT_RIGHT_1, INV(LEG_DN), 100},
  120. { -1, -1, -1}
  121. };
  122. angle_t *stand = (angle_t []) {
  123. {FRONT_RIGHT_0, 125, 0},
  124. {FRONT_RIGHT_1, 90, 0},
  125. {BACK_RIGHT_0, 55, 0},
  126. {BACK_RIGHT_1, 90, 0},
  127. {BACK_LEFT_0, 125, 0},
  128. {BACK_LEFT_1, 90, 0},
  129. {FRONT_LEFT_0, 55, 0},
  130. {FRONT_LEFT_1, 90, 150},
  131. { -1, -1, -1}
  132. };
  133. char movements[] = {FORWARD, LEFT, STAND, RIGHT, BACKWARD};
  134. Oscillator servos[8];
  135. unsigned long cur_time, prev_time;
  136. char action;
  137. char prev_action;
  138. boolean auto_mode = false;
  139. SoftwareSerial mySerial(12, 13); // RX, TX
  140. void locomotion(angle_t angles[]);
  141. void setup() {
  142. int i;
  143. for (i = 0; i < 8; i++) {
  144. servos[i].attach(i + SERVO_START_PIN);
  145. }
  146. Serial.begin(57600);
  147. Serial.println("Shit Type AT commands!");
  148. // The HC-06 defaults to 9600 according to the datasheet.
  149. mySerial.begin(9600);
  150. #ifdef __HC_SR04__
  151. pinMode(HC_SR04_TRIGGER_PIN, OUTPUT);
  152. pinMode(HC_SR04_ECHO_PIN, INPUT);
  153. #endif
  154. //
  155. // pinMode(MODE_PIN, INPUT);
  156. // auto_mode = digitalRead(MODE_PIN);
  157. if (auto_mode) {
  158. analogWrite(A0, 0);
  159. prev_action = action = FORWARD;
  160. } else {
  161. prev_action = action = STAND;
  162. analogWrite(A0, 128);
  163. }
  164. //
  165. locomotion(stand);
  166. randomSeed(analogRead(A7));
  167. cur_time = prev_time = millis();
  168. delay(3000);
  169. }
  170. void checkAutoMode() {
  171. if (mySerial.available () > 0) {
  172. action = mySerial.read ();
  173. if (action==RIGHT_FRONT) {
  174. auto_mode = !auto_mode;
  175. }
  176. }
  177. }
  178. void do_action() {
  179. long receivedNumber = 0;
  180. boolean negative = false;
  181. byte channel = 0;
  182. switch (action) {
  183. case '>':
  184. // if (negative)
  185. // do(-receivedNumber, channel);
  186. // else
  187. // do(receivedNumber, channel);
  188. break;
  189. case '<': // fall through to start a new number
  190. receivedNumber = 0;
  191. negative = false;
  192. channel = 0;
  193. break;
  194. case GO:
  195. #ifdef __HC_SR04__
  196. action = detect_obstacle(action);
  197. if (action == BACKWARD) {
  198. goto __backward;
  199. } else if (action == STAND) {
  200. goto __stand;
  201. }
  202. #endif
  203. case FORWARD:
  204. if (action != prev_action) {
  205. prev_action = action;
  206. locomotion(stand);
  207. }
  208. locomotion(forward);
  209. break;
  210. case BACKWARD: // Go BACK
  211. __backward:
  212. if (action != prev_action) {
  213. prev_action = action;
  214. locomotion(stand);
  215. }
  216. locomotion(backward);
  217. break;
  218. case RIGHT:
  219. if (action != prev_action) {
  220. prev_action = action;
  221. locomotion(stand);
  222. }
  223. locomotion(right);
  224. break;
  225. case RIGHT_FRONT:
  226. auto_mode = !auto_mode;
  227. // locomotion(stand);
  228. // locomotion(right);
  229. // locomotion(stand);
  230. // locomotion(forward);
  231. // locomotion(stand);
  232. break;
  233. case RIGHT_BACK:
  234. locomotion(stand);
  235. locomotion(right);
  236. locomotion(stand);
  237. locomotion(backward);
  238. locomotion(stand);
  239. break;
  240. case LEFT:
  241. if (action != prev_action) {
  242. prev_action = action;
  243. locomotion(stand);
  244. }
  245. locomotion(left);
  246. break;
  247. case LEFT_FRONT:
  248. Serial.print("auto_mode=");
  249. Serial.println(auto_mode ? "true" : "false");
  250. // locomotion(stand);
  251. // locomotion(left);
  252. // locomotion(stand);
  253. // locomotion(forward);
  254. // locomotion(stand);
  255. break;
  256. case LEFT_BACK:
  257. locomotion(stand);
  258. locomotion(left);
  259. locomotion(stand);
  260. locomotion(backward);
  261. locomotion(stand);
  262. break;
  263. case STAND:
  264. __stand:
  265. locomotion(stand);
  266. break;
  267. case 'x':
  268. channel = 1;
  269. break;
  270. case 'y':
  271. channel = 2;
  272. break;
  273. case '0' ... '9':
  274. receivedNumber *= 10;
  275. receivedNumber += action - '0';
  276. break;
  277. case '-':
  278. negative = true;
  279. break;
  280. } // end of switch
  281. }
  282. void loop() {
  283. //locomotion(forward);return;
  284. if (auto_mode) {
  285. checkAutoMode();
  286. cur_time = millis();
  287. if (cur_time - prev_time >= TIMEOUT) {
  288. prev_time = cur_time;
  289. checkAutoMode ();
  290. action = movements[(int)random(0, 5)];
  291. #ifdef __HC_SR04__
  292. action = detect_obstacle(action);
  293. #endif
  294. do_action();
  295. }
  296. } else {
  297. if (mySerial.available () > 0) {
  298. action = mySerial.read ();
  299. Serial.println(action);
  300. do_action();
  301. locomotion(stand);
  302. }
  303. }
  304. }
  305. void locomotion(angle_t angles[]) {
  306. int i = 0;
  307. while (1) {
  308. if (angles[i].degree == -1 ||
  309. angles[i].index == -1 ||
  310. angles[i].delay == -1) {
  311. break;
  312. }
  313. servos[angles[i].index].SetPosition(angles[i].degree);
  314. if (angles[i].delay > 0) delay(angles[i].delay);
  315. i++;
  316. }
  317. //delay(1000);
  318. }
  319. #ifdef __HC_SR04__
  320. char detect_obstacle(char action) {
  321. char i = action;
  322. long cm = distance_measure();
  323. if (cm < MIN_DISTANCE) {
  324. i = STAND;
  325. } else if (cm >= MIN_DISTANCE && cm <= MAX_DISTANCE) {
  326. i = BACKWARD;
  327. }
  328. return i;
  329. }
  330. long distance_measure()
  331. {
  332. // establish variables for duration of the ping,
  333. // and the distance result in inches and centimeters:
  334. long duration, cm;
  335. // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  336. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  337. digitalWrite(HC_SR04_TRIGGER_PIN, LOW);
  338. delayMicroseconds(2);
  339. digitalWrite(HC_SR04_TRIGGER_PIN, HIGH);
  340. delayMicroseconds(5);
  341. digitalWrite(HC_SR04_TRIGGER_PIN, LOW);
  342. // The same pin is used to read the signal from the PING))): a HIGH
  343. // pulse whose duration is the time (in microseconds) from the sending
  344. // of the ping to the reception of its echo off of an object.
  345. duration = pulseIn(HC_SR04_ECHO_PIN, HIGH);
  346. // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  347. // The ping travels out and back, so to find the distance of the
  348. // object we take half of the distance travelled.
  349. cm = duration / 29 / 2;
  350. //Serial.print(cm);
  351. //Serial.println("cm");
  352. // delay(100);
  353. return cm;
  354. }
  355. #endif
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注