|
41 | 41 | SFE_UBLOX_GPS::SFE_UBLOX_GPS(void)
|
42 | 42 | {
|
43 | 43 | // Constructor
|
| 44 | + currentGeofenceParams.numFences = 0; // Zero the number of geofences currently in use |
44 | 45 | }
|
45 | 46 |
|
46 | 47 | //Initialize the Serial port
|
@@ -1638,6 +1639,156 @@ boolean SFE_UBLOX_GPS::setAutoPVT(boolean enable, boolean implicitUpdate, uint16
|
1638 | 1639 | return ok;
|
1639 | 1640 | }
|
1640 | 1641 |
|
| 1642 | +//Add a new geofence using UBX-CFG-GEOFENCE |
| 1643 | +boolean SFE_UBLOX_GPS::addGeofence(int32_t latitude, int32_t longitude, uint32_t radius, byte confidence, byte pinPolarity, byte pin, uint16_t maxWait) |
| 1644 | +{ |
| 1645 | + if (currentGeofenceParams.numFences >= 4) return(false); // Quit if we already have four geofences defined |
| 1646 | + |
| 1647 | + // Store the new geofence parameters |
| 1648 | + currentGeofenceParams.lats[currentGeofenceParams.numFences] = latitude; |
| 1649 | + currentGeofenceParams.longs[currentGeofenceParams.numFences] = longitude; |
| 1650 | + currentGeofenceParams.rads[currentGeofenceParams.numFences] = radius; |
| 1651 | + currentGeofenceParams.numFences = currentGeofenceParams.numFences + 1; // Increment the number of fences |
| 1652 | + |
| 1653 | + packetCfg.cls = UBX_CLASS_CFG; |
| 1654 | + packetCfg.id = UBX_CFG_GEOFENCE; |
| 1655 | + packetCfg.len = (currentGeofenceParams.numFences * 12) + 8; |
| 1656 | + packetCfg.startingSpot = 0; |
| 1657 | + |
| 1658 | + payloadCfg[0] = 0; // Message version = 0x00 |
| 1659 | + payloadCfg[1] = currentGeofenceParams.numFences; // numFences |
| 1660 | + payloadCfg[2] = confidence; // confLvl = Confidence level 0-4 (none, 68%, 95%, 99.7%, 99.99%) |
| 1661 | + payloadCfg[3] = 0; // reserved1 |
| 1662 | + if (pin > 0) |
| 1663 | + { |
| 1664 | + payloadCfg[4] = 1; // enable PIO combined fence state |
| 1665 | + } |
| 1666 | + else |
| 1667 | + { |
| 1668 | + payloadCfg[4] = 0; // disable PIO combined fence state |
| 1669 | + } |
| 1670 | + payloadCfg[5] = pinPolarity; // PIO pin polarity (0 = low means inside, 1 = low means outside (or unknown)) |
| 1671 | + payloadCfg[6] = pin; // PIO pin |
| 1672 | + payloadCfg[7] = 0; //reserved2 |
| 1673 | + payloadCfg[8] = currentGeofenceParams.lats[0] & 0xFF; |
| 1674 | + payloadCfg[9] = currentGeofenceParams.lats[0] >> 8; |
| 1675 | + payloadCfg[10] = currentGeofenceParams.lats[0] >> 16; |
| 1676 | + payloadCfg[11] = currentGeofenceParams.lats[0] >> 24; |
| 1677 | + payloadCfg[12] = currentGeofenceParams.longs[0] & 0xFF; |
| 1678 | + payloadCfg[13] = currentGeofenceParams.longs[0] >> 8; |
| 1679 | + payloadCfg[14] = currentGeofenceParams.longs[0] >> 16; |
| 1680 | + payloadCfg[15] = currentGeofenceParams.longs[0] >> 24; |
| 1681 | + payloadCfg[16] = currentGeofenceParams.rads[0] & 0xFF; |
| 1682 | + payloadCfg[17] = currentGeofenceParams.rads[0] >> 8; |
| 1683 | + payloadCfg[18] = currentGeofenceParams.rads[0] >> 16; |
| 1684 | + payloadCfg[19] = currentGeofenceParams.rads[0] >> 24; |
| 1685 | + if (currentGeofenceParams.numFences >= 2) { |
| 1686 | + payloadCfg[20] = currentGeofenceParams.lats[1] & 0xFF; |
| 1687 | + payloadCfg[21] = currentGeofenceParams.lats[1] >> 8; |
| 1688 | + payloadCfg[22] = currentGeofenceParams.lats[1] >> 16; |
| 1689 | + payloadCfg[23] = currentGeofenceParams.lats[1] >> 24; |
| 1690 | + payloadCfg[24] = currentGeofenceParams.longs[1] & 0xFF; |
| 1691 | + payloadCfg[25] = currentGeofenceParams.longs[1] >> 8; |
| 1692 | + payloadCfg[26] = currentGeofenceParams.longs[1] >> 16; |
| 1693 | + payloadCfg[27] = currentGeofenceParams.longs[1] >> 24; |
| 1694 | + payloadCfg[28] = currentGeofenceParams.rads[1] & 0xFF; |
| 1695 | + payloadCfg[29] = currentGeofenceParams.rads[1] >> 8; |
| 1696 | + payloadCfg[30] = currentGeofenceParams.rads[1] >> 16; |
| 1697 | + payloadCfg[31] = currentGeofenceParams.rads[1] >> 24; |
| 1698 | + } |
| 1699 | + if (currentGeofenceParams.numFences >= 3) { |
| 1700 | + payloadCfg[32] = currentGeofenceParams.lats[2] & 0xFF; |
| 1701 | + payloadCfg[33] = currentGeofenceParams.lats[2] >> 8; |
| 1702 | + payloadCfg[34] = currentGeofenceParams.lats[2] >> 16; |
| 1703 | + payloadCfg[35] = currentGeofenceParams.lats[2] >> 24; |
| 1704 | + payloadCfg[36] = currentGeofenceParams.longs[2] & 0xFF; |
| 1705 | + payloadCfg[37] = currentGeofenceParams.longs[2] >> 8; |
| 1706 | + payloadCfg[38] = currentGeofenceParams.longs[2] >> 16; |
| 1707 | + payloadCfg[39] = currentGeofenceParams.longs[2] >> 24; |
| 1708 | + payloadCfg[40] = currentGeofenceParams.rads[2] & 0xFF; |
| 1709 | + payloadCfg[41] = currentGeofenceParams.rads[2] >> 8; |
| 1710 | + payloadCfg[42] = currentGeofenceParams.rads[2] >> 16; |
| 1711 | + payloadCfg[43] = currentGeofenceParams.rads[2] >> 24; |
| 1712 | + } |
| 1713 | + if (currentGeofenceParams.numFences >= 4) { |
| 1714 | + payloadCfg[44] = currentGeofenceParams.lats[3] & 0xFF; |
| 1715 | + payloadCfg[45] = currentGeofenceParams.lats[3] >> 8; |
| 1716 | + payloadCfg[46] = currentGeofenceParams.lats[3] >> 16; |
| 1717 | + payloadCfg[47] = currentGeofenceParams.lats[3] >> 24; |
| 1718 | + payloadCfg[48] = currentGeofenceParams.longs[3] & 0xFF; |
| 1719 | + payloadCfg[49] = currentGeofenceParams.longs[3] >> 8; |
| 1720 | + payloadCfg[50] = currentGeofenceParams.longs[3] >> 16; |
| 1721 | + payloadCfg[51] = currentGeofenceParams.longs[3] >> 24; |
| 1722 | + payloadCfg[52] = currentGeofenceParams.rads[3] & 0xFF; |
| 1723 | + payloadCfg[53] = currentGeofenceParams.rads[3] >> 8; |
| 1724 | + payloadCfg[54] = currentGeofenceParams.rads[3] >> 16; |
| 1725 | + payloadCfg[55] = currentGeofenceParams.rads[3] >> 24; |
| 1726 | + } |
| 1727 | + return (sendCommand(packetCfg, maxWait)); //Wait for ack |
| 1728 | +} |
| 1729 | + |
| 1730 | +//Clear all geofences using UBX-CFG-GEOFENCE |
| 1731 | +boolean SFE_UBLOX_GPS::clearGeofences(uint16_t maxWait) |
| 1732 | +{ |
| 1733 | + packetCfg.cls = UBX_CLASS_CFG; |
| 1734 | + packetCfg.id = UBX_CFG_GEOFENCE; |
| 1735 | + packetCfg.len = 8; |
| 1736 | + packetCfg.startingSpot = 0; |
| 1737 | + |
| 1738 | + payloadCfg[0] = 0; // Message version = 0x00 |
| 1739 | + payloadCfg[1] = 0; // numFences |
| 1740 | + payloadCfg[2] = 0; // confLvl |
| 1741 | + payloadCfg[3] = 0; // reserved1 |
| 1742 | + payloadCfg[4] = 0; // disable PIO combined fence state |
| 1743 | + payloadCfg[5] = 0; // PIO pin polarity (0 = low means inside, 1 = low means outside (or unknown)) |
| 1744 | + payloadCfg[6] = 0; // PIO pin |
| 1745 | + payloadCfg[7] = 0; //reserved2 |
| 1746 | + |
| 1747 | + currentGeofenceParams.numFences = 0; // Zero the number of geofences currently in use |
| 1748 | + |
| 1749 | + return (sendCommand(packetCfg, maxWait)); //Wait for ack |
| 1750 | +} |
| 1751 | + |
| 1752 | +//Clear the antenna control settings using UBX-CFG-ANT |
| 1753 | +//This function is hopefully redundant but may be needed to release |
| 1754 | +//any PIO pins pre-allocated for antenna functions |
| 1755 | +boolean SFE_UBLOX_GPS::clearAntPIO(uint16_t maxWait) |
| 1756 | +{ |
| 1757 | + packetCfg.cls = UBX_CLASS_CFG; |
| 1758 | + packetCfg.id = UBX_CFG_ANT; |
| 1759 | + packetCfg.len = 4; |
| 1760 | + packetCfg.startingSpot = 0; |
| 1761 | + |
| 1762 | + payloadCfg[0] = 0x10; // Antenna flag mask: set the recovery bit |
| 1763 | + payloadCfg[1] = 0; |
| 1764 | + payloadCfg[2] = 0xFF; // Antenna pin configuration: set pinSwitch and pinSCD to 31 |
| 1765 | + payloadCfg[3] = 0xFF; // Antenna pin configuration: set pinOCD to 31, set reconfig bit |
| 1766 | + |
| 1767 | + return (sendCommand(packetCfg, maxWait)); //Wait for ack |
| 1768 | +} |
| 1769 | + |
| 1770 | +//Returns the combined geofence state using UBX-NAV-GEOFENCE |
| 1771 | +boolean SFE_UBLOX_GPS::getGeofenceState(geofenceState ¤tGeofenceState, uint16_t maxWait) |
| 1772 | +{ |
| 1773 | + packetCfg.cls = UBX_CLASS_NAV; |
| 1774 | + packetCfg.id = UBX_NAV_GEOFENCE; |
| 1775 | + packetCfg.len = 0; |
| 1776 | + packetCfg.startingSpot = 0; |
| 1777 | + |
| 1778 | + if (sendCommand(packetCfg, maxWait) == false) //Ask module for the geofence status. Loads into payloadCfg. |
| 1779 | + return (false); |
| 1780 | + |
| 1781 | + currentGeofenceState.status = payloadCfg[5]; // Extract the status |
| 1782 | + currentGeofenceState.numFences = payloadCfg[6]; // Extract the number of geofences |
| 1783 | + currentGeofenceState.combState = payloadCfg[7]; // Extract the combined state of all geofences |
| 1784 | + if (currentGeofenceState.numFences > 0) currentGeofenceState.states[0] = payloadCfg[8]; // Extract geofence 1 state |
| 1785 | + if (currentGeofenceState.numFences > 1) currentGeofenceState.states[1] = payloadCfg[10]; // Extract geofence 2 state |
| 1786 | + if (currentGeofenceState.numFences > 2) currentGeofenceState.states[2] = payloadCfg[12]; // Extract geofence 3 state |
| 1787 | + if (currentGeofenceState.numFences > 3) currentGeofenceState.states[3] = payloadCfg[14]; // Extract geofence 4 state |
| 1788 | + |
| 1789 | + return(true); |
| 1790 | +} |
| 1791 | + |
1641 | 1792 | //Given a spot in the payload array, extract four bytes and build a long
|
1642 | 1793 | uint32_t SFE_UBLOX_GPS::extractLong(uint8_t spotToStart)
|
1643 | 1794 | {
|
|
0 commit comments