Problem with robodog project

hi there

I'm building a robodog which have a car body, I used a servo to move it's tail, and easyvr shield to listen and execute voice comands, and motor shield to move the 4wd chassis, but whenever I say start the servo get stuck at the last rotation and starts vibrating, I tried to increase or decrease the times the servo move but I get the same problem each time, can anyone help me?  

  1 #include <Servo.h> 
  2  
  3 Servo myservo;  // create servo object to control a servo 
  4                 // twelve servo objects can be created on most boards
  5  
  6 int pos = 0;    // variable to store the servo position 
  7 
  8 #include <AFMotor.h>
  9 
 10 uint8_t i;
 11 
 12 AF_DCMotor motor1(1);
 13 AF_DCMotor motor2(2);
 14 AF_DCMotor motor3(3);
 15 AF_DCMotor motor4(4);
 16 
 17 #if defined(ARDUINO) && ARDUINO >= 100
 18 
 19   #include "Arduino.h"
 20 
 21   #include "Platform.h"
 22 
 23   #include "SoftwareSerial.h"
 24 
 25 
 26   SoftwareSerial port(12,13);
 27 #endif
 28 
 29 #include "EasyVR.h"
 30 
 31 EasyVR easyvr(port);
 32 
 33 //Groups and Commands
 34 enum Groups
 35 {
 36   GROUP_0  = 0,
 37   GROUP_1  = 1,
 38   GROUP_2  = 2,
 39 };
 40 
 41 enum Group0 
 42 {
 43   G0_START = 0,
 44 };
 45 
 46 enum Group1 
 47 {
 48   G1_RIGHT = 0,
 49   G1_LEFT = 1,
 50   G1_BACK = 2,
 51   G1_GO = 3,
 52   G1_PLAY_DEAD = 4,
 53 };
 54 
 55 enum Group2 
 56 {
 57   G2_WAKE_UP = 0,
 58 };
 59 
 60 
 61 EasyVRBridge bridge;
 62 
 63 int8_t group, idx;
 64 
 65 
 66 void setup()
 67 {
 68    myservo.attach(9);
 69 #ifndef CDC_ENABLED
 70   // bridge mode?
 71   if (bridge.check())
 72   {
 73     cli();
 74     bridge.loop(0, 1, 12, 13);
 75   }
 76   // run normally
 77   Serial.begin(9600);
 78   Serial.println("Bridge not started!");
 79 #else
 80   // bridge mode?
 81   if (bridge.check())
 82   {
 83     port.begin(9600);
 84     bridge.loop(port);
 85   }
 86   Serial.println("Bridge connection aborted!");
 87 #endif
 88   port.begin(9600);
 89 
 90   while (!easyvr.detect())
 91   {
 92     Serial.println("EasyVR not detected!");
 93     delay(1000);
 94   }
 95 
 96   easyvr.setPinOutput(EasyVR::IO1, LOW);
 97   Serial.println("EasyVR detected!");
 98   easyvr.setTimeout(5);
 99   easyvr.setLanguage(0);
100 
101   group = EasyVR::TRIGGER; //<-- start group (customize)
102     Serial.println("Motor test!");
103 
104   // turn on motor
105   motor1.setSpeed(200);
106   motor2.setSpeed(200);
107   motor3.setSpeed(200);
108   motor4.setSpeed(200);
109  
110   motor1.run(RELEASE);
111   motor2.run(RELEASE);
112   motor3.run(RELEASE);
113   motor4.run(RELEASE);
114 
115 }
116 
117 void action();
118 
119 void loop()
120 {
121   easyvr.setPinOutput(EasyVR::IO1, HIGH); // LED on (listening)
122 
123   Serial.print("Say a command in Group ");
124   Serial.println(group);
125   easyvr.recognizeCommand(group);
126 
127   do
128   {
129     // can do some processing while waiting for a spoken command
130   }
131   while (!easyvr.hasFinished());
132   
133   easyvr.setPinOutput(EasyVR::IO1, LOW); // LED off
134 
135   idx = easyvr.getWord();
136   if (idx >= 0)
137   {
138     // built-in trigger (ROBOT)
139     // group = GROUP_X; <-- jump to another group X
140     return;
141   }
142   idx = easyvr.getCommand();
143   if (idx >= 0)
144   {
145     // print debug message
146     uint8_t train = 0;
147     char name[32];
148     Serial.print("Command: ");
149     Serial.print(idx);
150     if (easyvr.dumpCommand(group, idx, name, train))
151     {
152       Serial.print(" = ");
153       Serial.println(name);
154     }
155     else
156       Serial.println();
157     easyvr.playSound(0, EasyVR::VOL_FULL);
158     // perform some action
159     action();
160   }
161   else // errors or timeout
162   {
163     if (easyvr.isTimeout())
164       Serial.println("Timed out, try again...");
165     int16_t err = easyvr.getError();
166     if (err >= 0)
167     {
168       Serial.print("Error ");
169       Serial.println(err, HEX);
170     }
171   }
172 }
173 
174 void action()
175 {
176     switch (group)
177     {
178     case GROUP_0:
179       switch (idx)
180       {
181       case G0_START:
182         servoo(2);
183         delay(100);
184         group = GROUP_1;// <-- or jump to another group X for composite commands
185         break;
186       }
187       break;
188     case GROUP_1:
189       switch (idx)
190       {
191       case G1_RIGHT:
192         stopp();
193         right(255);
194         // group = GROUP_X; <-- or jump to another group X for composite commands
195         break;
196       case G1_LEFT:
197         stopp();
198         left(255);
199         // group = GROUP_X; <-- or jump to another group X for composite commands
200         break;
201       case G1_BACK:
202         stopp();
203         backward(255);
204         // group = GROUP_X; <-- or jump to another group X for composite commands
205         break;
206       case G1_GO:
207         stopp();
208         forward(255);
209         // group = GROUP_X; <-- or jump to another group X for composite commands
210         break;
211       case G1_PLAY_DEAD:
212         stopp();
213         group = GROUP_2;// <-- or jump to another group X for composite commands
214         break;
215       }
216       break;
217     case GROUP_2:
218       switch (idx)
219       {
220       case G2_WAKE_UP:
221         // write your action code here
222         group = GROUP_1; //<-- or jump to another group X for composite commands
223         break;
224       }
225       break;
226     }
227 }
228 void forward(int sped)
229 {
230   motor1.run(FORWARD);
231   motor2.run(FORWARD);
232   motor3.run(FORWARD);
233   motor4.run(FORWARD);
234   for (i=0; i<sped; i++) 
235   {
236     motor1.setSpeed(i);
237     motor2.setSpeed(i);
238     motor3.setSpeed(i);
239     motor4.setSpeed(i); 
240     delay(10);
241   } 
242  }
243  void backward(int sped)
244 {
245   motor1.run(BACKWARD);
246   motor2.run(BACKWARD);
247   motor3.run(BACKWARD);
248   motor4.run(BACKWARD);
249   for (i=0; i<sped; i++) 
250   {
251     motor1.setSpeed(i);
252     motor2.setSpeed(i);
253     motor3.setSpeed(i);
254     motor4.setSpeed(i); 
255     delay(10);
256   }
257 }
258  void left(int sped)
259 {
260   motor1.run(BACKWARD);
261   motor2.run(FORWARD);
262   motor3.run(FORWARD);
263   motor4.run(BACKWARD);  
264   for (i=0; i<sped; i++) 
265   {
266     motor1.setSpeed(i);
267     motor2.setSpeed(i);
268     motor3.setSpeed(i);
269     motor4.setSpeed(i); 
270     delay(10);
271   }
272  }
273  void right(int sped)
274 {
275   motor1.run(FORWARD);
276   motor2.run(BACKWARD);
277   motor3.run(BACKWARD);
278   motor4.run(FORWARD);
279   for (i=0; i<sped; i++) 
280   {
281     motor1.setSpeed(i);
282     motor2.setSpeed(i);
283     motor3.setSpeed(i);
284     motor4.setSpeed(i); 
285     delay(10);
286   }
287  }
288  void stopp()
289 {
290   motor1.run(RELEASE);
291   motor2.run(RELEASE);
292   motor3.run(RELEASE);
293   motor4.run(RELEASE);  
294  }
295 void servoo(int num)
296 {
297   for(int i=0; i<num; i++)
298   {
299     for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees 
300     {                                  // in steps of 1 degree 
301       myservo.write(pos);              // tell servo to go to position in variable 'pos' 
302       delay(2);                       // waits 15ms for the servo to reach the position 
303     } 
304     for(pos = 180; pos>=0; pos-=1)     // goes from 180 degrees to 0 degrees 
305     {                                
306       myservo.write(pos);              // tell servo to go to position in variable 'pos' 
307       delay(2);                       // waits 15ms for the servo to reach the position 
308     } 
309   }
310 } 

syntax highlighted by Code2HTML, v. 0.9.1

Could you do two things for this post?

Somehow you have pasted your code twice. Could you read https://www.robotshop.com/letsmakerobots/node/33446 and then post it once in a more readable format? Even after going through and killing lots of excess whitespace there are still 250 +/- lines. (The two things I was hoping for are getting rid of one of the copies of your code, and, posting it in a more readable way.)

To answer your question, have you tried not asking the servo to go to 180 or 0? It is my understanding that most servos do not have a full 180 degrees of travel. The vibration may be you asking the servo to travel to a point that is outside of its ability.

PS: I did notice you really did try to get the code to post properly with the tags. They just don’t work here.

another problem came up

i tried to make it move from 30 to 150 and it solved the problem of the servo, but now the eazyvr shield stopped recognizing any command and giving me Error 0, Error 7 and Error 11

 

update: I downloaded the manual and identified the problem and solved it, thanks for your help i really appreciate it 

If you don’t mind some constructive criticism and/or

some suggestions. Take the criticism with a grain or two of salt for what it is worth. :slight_smile:

Might I suggest you enclose ALL blocks of code after if statements in {}, single line or not?

From the looks of the code, a fast move for instance left(200) would take longer than a slow move left(50), because of the delay(10) in the loop that progressively sets the speed faster and faster. left(200) would take 1 1/2 seconds longer to complete simply due to the delay(10). Is there a reason to avoid just saying motor1(2,3,4).setSpeed(sped); ? If you feel you need a delay (I would agree), why not just send a delay with the speed requested? To that end I slapped something together that may or may not save some lines of code

 

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
void roboMove(int left, int right, int dlay) {
if (left > 0) AND (right > 0) {
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
if (left > 0) AND (right =< 0) {
motor1.run(BACKWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(BACKWARD);
}
if (left =< 0) AND (right > 0) {
motor1.run(FORWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(FORWARD);
}
if (left < 0) AND (right < 0) {
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
}
if (left = 0) AND (right = 0) {
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
left = map(abs(left), 0, 100, 0, 255);
right = map(abs(right), 0, 100, 0, 255);
motor1.setSpeed(left);
motor2.setSpeed(left);
motor3.setSpeed(right);
motor4.setSpeed(right);
delay(dlay);
}

 

Fair warning, I have not tested this code in the slightest.

If you decide against trying the above code, at least consider calling the stopp() function in your setup() in place of all the motor1.run(RELEASE); lines. You have already typed it why type it again? :slight_smile: If you don’t use the code I pasted, at least also consider breaking out the .setSpeed() in another function and call it when you need it,  like in setup() and in each of the direction calls.