Browse Source

Cleanup example code

master
Lukas Bachschwell 1 year ago
parent
commit
2bb5001fe4
2 changed files with 145 additions and 36 deletions
  1. 20
    2
      examples/BasicMoves/BasicMoves.ino
  2. 125
    34
      examples/WebInterface/WebInterface.ino

+ 20
- 2
examples/BasicMoves/BasicMoves.ino View File

@@ -1,7 +1,7 @@
1 1
 // Example of the LARS Movement library
2 2
 #include <LARS.h>
3 3
 
4
-LARS robot; // using default ESP32 pinning values for constructor
4
+LARS robot;
5 5
 
6 6
 boolean walk_forward = false;
7 7
 boolean walk_backward = false;
@@ -19,7 +19,25 @@ boolean PushUp = false;
19 19
 void setup() {
20 20
   Serial.begin(115200);
21 21
 
22
-  robot.init();
22
+  /*
23
+  How to wire:
24
+
25
+  init(FRH, FLH, BRH, BLH, FRL, FLL, BRL, BLL)
26
+
27
+    Leg        Hip    Face    Hip         Leg
28
+     __________ __________ _________________
29
+    |(FLL)_____)(FLH)      (FRH)(______(FRL)|
30
+    |__|       |left FRONT right|        |__|
31
+               |                |
32
+               |                |
33
+               |                |
34
+     _________ |                | __________
35
+    |(BLL)_____)(BLH)______(BRH)(______(BRL)|
36
+    |__|                                 |__|
37
+
38
+  */
39
+
40
+  robot.init(26, 25, 17, 16, 27, 5, 23, 13); // Calling init with default values
23 41
   delay(500);
24 42
 }
25 43
 

+ 125
- 34
examples/WebInterface/WebInterface.ino View File

@@ -1,9 +1,23 @@
1
-// Example of the LARS Movement library using the ESPUI Webinferface by Lukas Bachschwell
1
+// Example of the LARS Movement library using the ESPUI Webinferface and the SimleExpressions Library by Lukas Bachschwell
2
+#include <SimpleExpressions.h>
2 3
 #include <WiFi.h>
3 4
 #include <ESPUI.h>
4 5
 #include <LARS.h>
5 6
 
6
-LARS robot; // using default ESP32 pinning values for constructor
7
+#define ledDataPin 27 // cannot use
8
+#define beeperPin 17 // cannot use
9
+
10
+#define echoPin 5 // cannot use
11
+#define triggerPin 23 //cannot use
12
+
13
+#define checkTime 1000
14
+#define warningDistance 20
15
+long oldTime = 0;
16
+bool warning = false;
17
+
18
+const char* ssid = "Lars";
19
+
20
+LARS robot;
7 21
 
8 22
 boolean walk_forward = false;
9 23
 boolean walk_backward = false;
@@ -21,8 +35,8 @@ boolean PushUp = false;
21 35
 void setup() {
22 36
   Serial.begin(115200);
23 37
   WiFi.mode(WIFI_AP);
24
-  WiFi.setHostname("Lars");
25
-  WiFi.softAP("Lars");
38
+  WiFi.setHostname(ssid);
39
+  WiFi.softAP(ssid);
26 40
   Serial.println("");
27 41
   Serial.print("IP address: ");
28 42
   Serial.println(WiFi.softAPIP());
@@ -40,25 +54,73 @@ void setup() {
40 54
 
41 55
   ESPUI.begin("Crabby Control");
42 56
 
43
-  robot.init();
57
+  SimpleExpressions.init(ledDataPin, beeperPin);
58
+  SimpleExpressions.clearMouth();
59
+
60
+  SimpleExpressions.writeMouth("happySmall", 0, 0, 80);
61
+  SimpleExpressions.playSound(S_SUPER_HAPPY);
62
+  SimpleExpressions.writeMouth("happyFull", 0, 60, 100);
63
+
64
+
65
+
66
+/*
67
+How to wire:
68
+
69
+init(FRH, FLH, BRH, BLH, FRL, FLL, BRL, BLL)
70
+
71
+  Leg        Hip    Face    Hip         Leg
72
+   __________ __________ _________________
73
+  |(FLL)_____)(FLH)      (FRH)(______(FRL)|
74
+  |__|       |left FRONT right|        |__|
75
+             |                |
76
+             |                |
77
+             |                |
78
+   _________ |                | __________
79
+  |(BLL)_____)(BLH)______(BRH)(______(BRL)|
80
+  |__|                                 |__|
81
+
82
+*/
83
+
84
+  robot.init(26, 25, 17, 16, 27, 5, 23, 13); // Calling init with default values
44 85
   delay(1000);
45 86
 }
46 87
 
88
+void loop() {
89
+  if (millis() - oldTime > checkTime) {
90
+    checkDistance();
91
+    oldTime = millis();
92
+  }
93
+  
94
+  if (walk_forward) robot.walk(0);
95
+  else if (walk_backward) robot.walk();
96
+  else if (hello) robot.hello();
97
+  else if (turn_left) robot.turnL(1, 550);
98
+  else if (turn_right) robot.turnR(1, 550);
99
+  else if (moonwalk) robot.moonwalk(1, 5000);
100
+  else if (audience) robot.wave(1);
101
+  else if (dance) robot.dance(1, 600);
102
+  else if (omni) robot.omniWalk(1, 600, true, 1);
103
+  else if (omni_left) robot.omniWalk(1, 600, false, 1);
104
+  else if (PushUp) robot.pushUp(1, 600);
105
+  else if (upDown) robot.upDown(1, 5000);
106
+  else robot.home();
107
+
108
+}
109
+
110
+// UI Callbacks
111
+
47 112
 void audienceWaveButton(Control c, int type) {
48 113
   if (type == B_DOWN) {
49 114
     audience = true;
50
-  }
51
-  else {
115
+  } else {
52 116
     audience = false;
53 117
   }
54 118
 }
55 119
 
56
-
57 120
 void moonWalkButton(Control c, int type) {
58 121
   if (type == B_DOWN) {
59 122
     moonwalk = true;
60
-  }
61
-  else {
123
+  } else {
62 124
     moonwalk = false;
63 125
   }
64 126
 }
@@ -66,32 +128,31 @@ void moonWalkButton(Control c, int type) {
66 128
 void danceButton(Control c, int type) {
67 129
   if (type == B_DOWN) {
68 130
     dance = true;
69
-  }
70
-  else {
131
+  } else {
71 132
     dance = false;
72 133
   }
73 134
 }
135
+
74 136
 void pushUpButton(Control c, int type) {
75 137
   if (type == B_DOWN) {
76 138
     PushUp = true;
77
-  }
78
-  else {
139
+  } else {
79 140
     PushUp = false;
80 141
   }
81 142
 }
143
+
82 144
 void upDownButton(Control c, int type) {
83 145
   if (type == B_DOWN) {
84 146
     upDown = true;
85
-  }
86
-  else {
147
+  } else {
87 148
     upDown = false;
88 149
   }
89 150
 }
151
+
90 152
 void slowTurnRight(Control c, int type) {
91 153
   if (type == B_DOWN) {
92 154
     omni = true;
93
-  }
94
-  else {
155
+  } else {
95 156
     omni = false;
96 157
   }
97 158
 }
@@ -99,8 +160,7 @@ void slowTurnRight(Control c, int type) {
99 160
 void slowTurnLeft(Control c, int type) {
100 161
   if (type == B_DOWN) {
101 162
     omni_left = true;
102
-  }
103
-  else {
163
+  } else {
104 164
     omni_left = false;
105 165
   }
106 166
 }
@@ -157,20 +217,51 @@ void wavePad(Control c, int value) {
157 217
   }
158 218
 }
159 219
 
160
-void loop() {
220
+// Ultrasonic Sensor
221
+
222
+void checkDistance() {
223
+  int d = distance(triggerPin, echoPin);
224
+  ESPUI.print("Distance", String(d));
225
+  if (d < warningDistance) {
226
+    delay(100);
227
+    d = distance(triggerPin, echoPin);
228
+    if (d < warningDistance) {
229
+      if(!warning) {
230
+        SimpleExpressions.writeMouth("sadFull", 100, 0, 0);
231
+        SimpleExpressions.writeMouth("sadFull", 100, 0, 0); // only twice please
232
+        }
233
+      SimpleExpressions.playSound(S_CONFUSED);
234
+      warning = true;
235
+    }
236
+  } else {
237
+    if (warning) {
238
+      SimpleExpressions.writeMouth("happyFull", 0, 60, 100);
239
+      SimpleExpressions.writeMouth("happyFull", 0, 60, 100); // only twice please
240
+      warning = false;
241
+    }
242
+  }
243
+}
244
+
161 245
 
162
-  if (walk_forward) robot.walk(0);
163
-  else if (walk_backward) robot.walk();
164
-  else if (hello) robot.hello();
165
-  else if (turn_left) robot.turnL(1, 550);
166
-  else if (turn_right) robot.turnR(1, 550);
167
-  else if (moonwalk) robot.moonwalk(1, 5000);
168
-  else if (audience) robot.wave(1);
169
-  else if (dance) robot.dance(1, 600);
170
-  else if (omni) robot.omniWalk(1, 600, true, 1);
171
-  else if (omni_left) robot.omniWalk(1, 600, false, 1);
172
-  else if (PushUp) robot.pushUp(1, 600);
173
-  else if (upDown) robot.upDown(1, 5000);
174
-  else robot.home();
175 246
 
247
+long US_init(int trigger_pin, int echo_pin)
248
+{
249
+  digitalWrite(trigger_pin, LOW);
250
+  delayMicroseconds(2);
251
+  digitalWrite(trigger_pin, HIGH);
252
+  delayMicroseconds(10);
253
+  digitalWrite(trigger_pin, LOW);
254
+
255
+  return pulseIn(echo_pin, HIGH, 100000);
256
+}
257
+
258
+long distance(int trigger_pin, int echo_pin)
259
+{
260
+  long microseconds = US_init(trigger_pin, echo_pin);
261
+  long distance;
262
+  distance = microseconds / 29 / 2;
263
+  if (distance == 0) {
264
+    distance = 999;
265
+  }
266
+  return distance;
176 267
 }

Loading…
Cancel
Save