Bonjour,

Je reconstruis sur une carte BotBoarduino (Duemilanove) un ancien projet qui tournait sur BasicStamp il y a une quinzaine d'années. Il s'agit du guidage GPS d'un bateau modèle réduit.
Les composants utilisés sont énumérés dans le programme.
Je suis bloqué par un problème d'interférence entre le fonctionnement du GPS Shield et la rotation d'un servo qui se met à battre au rythme de la réception des données NMEA.
Si je désactive ss.begin, le servo reste bien à la position souhaitée.
Merci pour votre aide.

Mon programme ci-dessous :
Code ino : Sélectionner tout - Visualiser dans une fenêtre à part
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
******************************************************************************************************
   Position acquisition with GPS Shield https://www.elecrow.com/gps-shield-with-antenna-p-696.html
   Use of a TinyGPS++ (TinyGPSPlus) object.
   It requires the use of SoftwareSerial, and assumes that you have a
   9600-baud serial GPS device hooked up on pins 6(rx) and 7(tx)(Arduino).
 
   Bearing with Compass GY-282 (HMC5983)  https://wiki.eprolabs.com/index.php?title=GY-282_HMC5983_Module
 
 
   Acquisition de position par Shield GPS https://www.elecrow.com/gps-shield-with-antenna-p-696.html
   Utilise TinyGPS++ (TinyGPSPlus)et SoftwareSerial
   Nécessite l'utilisation d'un GPS série à 9600 bauds raccordé aux pins 6(rx) et 7(tx)de l'Arduino.
 
   Orientation par Boussole GY-282 (HMC5983)  https://wiki.eprolabs.com/index.php?title=GY-282_HMC5983_Module
 
   Carte Botboarduino (basée sur l'Arduino Duemilanove) https://www.robotshop.com/media/files/pdf2/botboarduinomanual.pdf
 
                    Thierry M.
                    14 août 2020
 
 
******************************************************************************************************/
 
// Pas de bug, mais
 
//    NON TERMINÉ (le servo balaye en permanence !!! interférence GPS et servo)
 
 
 
 
/******************************************************************************************************
   GPS Shield
******************************************************************************************************/
 
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
 
static const double WP_LAT = 44.078562, WP_LON = 6.174634; // WayPoint, Destination
static const uint32_t GPSBaud = 9600; // Vitesse transfert  GPS ==> Carte
static const int RXPin = 6, TXPin = 7; // Rx-Tx de l'Arduino (pas du GPS)
SoftwareSerial ss(RXPin, TXPin);
TinyGPSPlus gps; // The TinyGPS++ object
 
unsigned long distancemToWP;
double capToWP;
 
void gps_pc_monit()  // Ecriture texte sur Moniteur PC
{
  Serial.println(F("Sats Latitude   Longitude     Date      Heure   Alt     Route Vitesse Card Distance   Cap    Dir  Chars Sentences Checksum"));
  Serial.println(F("      (deg)      (deg)                          (m)     --- from GPS ----   du WP   vers WP  ----  RX    RX       Fail"));
  Serial.println(F("---------------------------------------------------------------------------------------------------------------------------"));
}
 
 
void gps_data() // Envoi des données GPS vers Moniteur PC
{
 
 
 
  printInt(gps.satellites.value(), gps.satellites.isValid(), 5);
  printFloat(gps.location.lat(), gps.location.isValid(), 11, 6);
  printFloat(gps.location.lng(), gps.location.isValid(), 12, 6);
  printDateTime(gps.date, gps.time);
  printFloat(gps.altitude.meters(), gps.altitude.isValid(), 9, 2);
  printFloat(gps.course.deg(), gps.course.isValid(), 7, 2);
  printFloat(gps.speed.kmph(), gps.speed.isValid(), 6, 2);
  printStr(gps.course.isValid() ? TinyGPSPlus::cardinal(gps.course.value()) : "*** ", 6);
 
 
 
  distancemToWP =
    (unsigned long)TinyGPSPlus::distanceBetween(
      gps.location.lat(),
      gps.location.lng(),
      WP_LAT,
      WP_LON);
 
 
  printInt(distancemToWP, gps.location.isValid(), 9);
 
 
  capToWP =
    TinyGPSPlus::courseTo(
      gps.location.lat(),
      gps.location.lng(),
      WP_LAT,
      WP_LON);
  printFloat(capToWP, gps.location.isValid(), 8, 2);
 
  const char *cardinalToWP = TinyGPSPlus::cardinal(capToWP);
  printStr(gps.location.isValid() ? cardinalToWP : "*** ", 6);
  printInt(gps.charsProcessed(), true, 7);
  printInt(gps.sentencesWithFix(), true, 10);
  printInt(gps.failedChecksum(), true, 9);
  Serial.println();
 
}
 
 
static void smartDelay(unsigned long ms)
{
  unsigned long start = millis();
  do
  {
    while (ss.available())
      gps.encode(ss.read());
  } while (millis() - start < ms);
}
 
 
static void printFloat(float val, bool valid, int len, int prec)
{
  if (!valid)
  {
    while (len-- > 1)
      Serial.print('*');
    Serial.print(' ');
  }
  else
  {
    Serial.print(val, prec);
    int vi = abs((int)val);
    int flen = prec + (val < 0.0 ? 2 : 1); // . and -
    flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
    for (int i = flen; i < len; ++i)
      Serial.print(' ');
  }
  smartDelay(0);
}
 
 
static void printInt(unsigned long val, bool valid, int len)
{
  char sz[32] = "*****************";
  if (valid)
    sprintf(sz, "%ld", val);
  sz[len] = 0;
  for (int i = strlen(sz); i < len; ++i)
    sz[i] = ' ';
  if (len > 0)
    sz[len - 1] = ' ';
  Serial.print(sz);
  smartDelay(0);
}
 
 
 
static void printDateTime(TinyGPSDate &d, TinyGPSTime &t)
{
  if (!d.isValid())
  {
    Serial.print(F("********** "));
  }
  else
  {
    char sz[32];
    sprintf(sz, "%02d/%02d/%02d ", d.month(), d.day(), d.year());
    Serial.print(sz);
  }
 
  if (!t.isValid())
  {
    Serial.print(F("******** "));
  }
  else
  {
    char sz[32];
    sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second());
    Serial.print(sz);
  }
 
 
}
 
 
 
static void printStr(const char *str, int len)
{
  int slen = strlen(str);
  for (int i = 0; i < len; ++i)
    Serial.print(i < slen ? str[i] : ' ');
  smartDelay(0);
}
 
 
void gps_valid() // Informe de l'état du GPS
{
 
  if (millis() > 5000 && gps.charsProcessed() < 10)
    Serial.println(F("No GPS data received: check wiring"));
 
  if (gps.location.isValid())
  {
    digitalWrite(8, LOW);    // turn the LED on by making the voltage LOW
  }
}
 
 
void wp_prox()  // Allume led interne orange si WayPoint proche
{
  if (distancemToWP < 8)
  {
    digitalWrite(9, LOW);    // turn the LED on by making the voltage LOW
  }
}
 
 
void init_led()  // Initialise les Leds internes
{
  pinMode(8, OUTPUT); // initialize digital pin LED_VERTE as an output
  digitalWrite(8, HIGH);    // turn the LED off by making the voltage HIGH
 
  pinMode(9, OUTPUT); // initialize digital pin LED_JAUNE as an output.
  digitalWrite(9, HIGH);    // turn the LED off by making the voltage HIGH
 
 
  digitalWrite(LED_BUILTIN, LOW); // Eteint Led interne
}
 
/******************************************************************************************************
   Boussole GY-282
******************************************************************************************************/
 
#include <Wire.h> //I2C Arduino Library
#define addr 0x1E //I2C Address for The HMC5883
 
int compassCorrection = 0; // set to 0 (not used)
 
 
void init_boussole()  // Début de transfert (I2C) Boussole ==> Carte
{
  Wire.begin();
  Wire.beginTransmission(addr); //start talking
  Wire.write(0x02); // Set the Register
  Wire.write(0x00); // Tell the HMC5883 to Continuously Measure
  Wire.endTransmission();
}
 
int x, y, z; //triple axis data
 
void data_boussole ()
{
 
  //Tell the HMC what regist to begin writing data into
  Wire.beginTransmission(addr);
  Wire.write(0x03); //start with register 3.
  Wire.endTransmission();
 
  //Read the data.. 2 bytes for each axis.. 6 total bytes
  Wire.requestFrom(addr, 6);
  if (6 <= Wire.available()) {
    x = Wire.read() << 8; //MSB  x
    x |= Wire.read(); //LSB  x
    z = Wire.read() << 8; //MSB  z
    z |= Wire.read(); //LSB z
    y = Wire.read() << 8; //MSB y
    y |= Wire.read(); //LSB y}
  }
}
 
float routeDegrees;
 
void calc_route ()
{
  // Calcul de la route
  float route = atan2(x, y);
 
  // Correct for when signs are reversed.
  if (route < 0)
    route += 2 * PI;
 
  // Check for wrap due to addition of declination.
  if (route > 2 * PI)
    route -= 2 * PI;
 
  // Convert radians to degrees for readability.
  routeDegrees = route * 180 / M_PI;
 
  int routeDegreesInteger = routeDegrees;
  // not used here, since correction is set to 0
  if ((routeDegreesInteger - compassCorrection) >= 0)
  {
    routeDegreesInteger = routeDegreesInteger  - compassCorrection;
  }
  else
  {
    routeDegreesInteger = routeDegreesInteger - compassCorrection + 360;
  }
 
  Serial.print("Route (degres): "); Serial.println(routeDegrees);
 
}
 
 
/******************************************************************************************************
   Commande Servo Direction
******************************************************************************************************/
 
 
#include <Servo.h>
 
Servo myservo; // création de l'objet myservo
 
int pos; // variable to store the servo position
 
void init_servo ()
{
  // Fixe le servo sur pin 13
  myservo.attach(13);
  // Initialise position servo à 90°
  pos = 170;
 
}
 
void pos_servo ()
{
  myservo.write(pos);
  delay(15);
}
 
 
 
/*******************************************************************************
                                 SETUP
********************************************************************************/
 
void setup()
{
  init_led();
  Serial.begin(9600); // Début de transfert (Vitesse)  Carte ==> PC
  ss.begin(GPSBaud); // Début de transfert (Vitesse)  GPS ==> Carte
  gps_pc_monit();
  init_boussole();
  init_servo (); // !!!!!!!!!  pour le moment  -  interfère avec ss.begin  !!!!!!!!!!
 
}
 
 
 
/*******************************************************************************
                                 LOOP
********************************************************************************/
 
void loop()
{
  gps_data();
 
  smartDelay(1000);
 
  gps_valid();
  wp_prox();
 
  data_boussole ();
 
  calc_route ();
 
  pos_servo ();
 
}