Skip to content

Commit a310a5c

Browse files
authored
Merge pull request #584 from 446564/nano-g2-gps-fix
nano g2 gps fixes
2 parents 6b2836e + 4d97bee commit a310a5c

File tree

4 files changed

+49
-43
lines changed

4 files changed

+49
-43
lines changed

variants/nano_g2_ultra/nano-g2.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ void NanoG2Ultra::begin()
3434
pinMode(EXT_NOTIFY_OUT, OUTPUT);
3535
digitalWrite(EXT_NOTIFY_OUT, LOW);
3636

37+
pinMode(GPS_EN, OUTPUT); // Initialize GPS power pin
38+
3739
Wire.begin();
3840
pinMode(SX126X_POWER_EN, OUTPUT);
3941
digitalWrite(SX126X_POWER_EN, HIGH);

variants/nano_g2_ultra/nano-g2.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#define BUTTON_PIN PIN_BUTTON1
2222
#define PIN_USER_BTN BUTTON_PIN
2323

24+
// GPS
25+
#define GPS_EN PIN_GPS_STANDBY
2426
// built-ins
2527
#define VBAT_MV_PER_LSB (0.73242188F) // 3.0V ADC range and 12-bit ADC resolution = 3000mV/4096
2628

variants/nano_g2_ultra/platformio.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ build_flags =
3737
-D MAX_CONTACTS=100
3838
-D MAX_GROUP_CHANNELS=8
3939
-D BLE_PIN_CODE=123456
40-
-D BLE_DEBUG_LOGGING=1
40+
; -D BLE_DEBUG_LOGGING=0
4141
-D OFFLINE_QUEUE_SIZE=256
4242
-D DISPLAY_CLASS=SH1106Display
4343
-D PIN_BUZZER=4

variants/nano_g2_ultra/target.cpp

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -42,45 +42,44 @@ void radio_set_tx_power(uint8_t dbm)
4242
radio.setOutputPower(dbm);
4343
}
4444

45-
void NanoG2UltraSensorManager::start_gps()
46-
{
47-
if (!gps_active)
48-
{
49-
MESH_DEBUG_PRINTLN("starting GPS");
50-
digitalWrite(PIN_GPS_STANDBY, HIGH);
45+
void NanoG2UltraSensorManager::start_gps() {
46+
MESH_DEBUG_PRINTLN("Starting GPS");
47+
if (!gps_active) {
48+
digitalWrite(PIN_GPS_STANDBY, HIGH); // Wake GPS from standby
49+
Serial1.setPins(PIN_GPS_TX, PIN_GPS_RX);
50+
Serial1.begin(9600);
51+
MESH_DEBUG_PRINTLN("Waiting for gps to power up");
52+
delay(1000);
5153
gps_active = true;
5254
}
55+
_location->begin();
5356
}
5457

55-
void NanoG2UltraSensorManager::stop_gps()
56-
{
57-
if (gps_active)
58-
{
59-
MESH_DEBUG_PRINTLN("stopping GPS");
60-
digitalWrite(PIN_GPS_STANDBY, LOW);
58+
void NanoG2UltraSensorManager::stop_gps() {
59+
MESH_DEBUG_PRINTLN("Stopping GPS");
60+
if (gps_active) {
61+
digitalWrite(PIN_GPS_STANDBY, LOW); // sleep GPS
6162
gps_active = false;
6263
}
64+
_location->stop();
6365
}
6466

6567
bool NanoG2UltraSensorManager::begin()
6668
{
67-
Serial1.setPins(PIN_GPS_TX, PIN_GPS_RX); // be sure to tx into rx and rx into tx
68-
Serial1.begin(115200);
69-
70-
pinMode(PIN_GPS_STANDBY, OUTPUT);
7169
digitalWrite(PIN_GPS_STANDBY, HIGH); // Wake GPS from standby
72-
delay(500);
73-
74-
// We'll consider GPS detected if we see any data on Serial1
75-
if (Serial1.available() > 0)
76-
{
77-
MESH_DEBUG_PRINTLN("GPS detected");
78-
}
79-
else
80-
{
81-
MESH_DEBUG_PRINTLN("No GPS detected");
70+
Serial1.setPins(PIN_GPS_TX, PIN_GPS_RX);
71+
Serial1.begin(9600);
72+
MESH_DEBUG_PRINTLN("Checking GPS switch state");
73+
delay(1000);
74+
75+
// Check initial switch state to determine if GPS should be active
76+
if (Serial1.available() > 0) {
77+
MESH_DEBUG_PRINTLN("GPS was on at boot, GPS enabled");
78+
start_gps();
79+
} else {
80+
MESH_DEBUG_PRINTLN("GPS was not on at boot, GPS disabled");
8281
}
83-
digitalWrite(GPS_EN, LOW); // Put GPS back into standby mode
82+
8483
return true;
8584
}
8685

@@ -96,17 +95,24 @@ bool NanoG2UltraSensorManager::querySensors(uint8_t requester_permissions, Cayen
9695
void NanoG2UltraSensorManager::loop()
9796
{
9897
static long next_gps_update = 0;
98+
99+
if (!gps_active) {
100+
return; // GPS is not active, skip further processing
101+
}
102+
99103
_location->loop();
100-
if (millis() > next_gps_update && gps_active) // don't bother if gps position is not enabled
101-
{
102-
if (_location->isValid())
103-
{
104-
node_lat = ((double)_location->getLatitude()) / 1000000.;
105-
node_lon = ((double)_location->getLongitude()) / 1000000.;
104+
105+
if (millis() > next_gps_update) {
106+
if (_location->isValid()) {
107+
node_lat = ((double)_location->getLatitude())/1000000.;
108+
node_lon = ((double)_location->getLongitude())/1000000.;
106109
node_altitude = ((double)_location->getAltitude()) / 1000.0;
107-
MESH_DEBUG_PRINTLN("lat %f lon %f", node_lat, node_lon);
110+
MESH_DEBUG_PRINTLN("VALID location: lat %f lon %f", node_lat, node_lon);
111+
} else {
112+
MESH_DEBUG_PRINTLN("INVALID location, waiting for fix");
108113
}
109-
next_gps_update = millis() + (1000 * 60); // after initial update, only check every minute TODO: should be configurable
114+
MESH_DEBUG_PRINTLN("GPS satellites: %d", _location->satellitesCount());
115+
next_gps_update = millis() + 1000;
110116
}
111117
}
112118

@@ -128,14 +134,10 @@ const char *NanoG2UltraSensorManager::getSettingValue(int i) const
128134

129135
bool NanoG2UltraSensorManager::setSettingValue(const char *name, const char *value)
130136
{
131-
if (strcmp(name, "gps") == 0)
132-
{
133-
if (strcmp(value, "0") == 0)
134-
{
137+
if (strcmp(name, "gps") == 0) {
138+
if (strcmp(value, "0") == 0) {
135139
stop_gps();
136-
}
137-
else
138-
{
140+
} else {
139141
start_gps();
140142
}
141143
return true;

0 commit comments

Comments
 (0)