-
Notifications
You must be signed in to change notification settings - Fork 1.8k
rangefinder gy-us42 added #5698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| /* | ||
| * This file is part of INAV. | ||
| * | ||
| * INAV is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * INAV is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with INAV. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| #include <stdbool.h> | ||
| #include <stdint.h> | ||
|
|
||
| #include "platform.h" | ||
|
|
||
| #if defined(USE_RANGEFINDER) && defined(USE_RANGEFINDER_US42) | ||
|
|
||
| #include "build/build_config.h" | ||
|
|
||
| #include "drivers/time.h" | ||
| #include "drivers/bus_i2c.h" | ||
|
|
||
| #include "drivers/rangefinder/rangefinder.h" | ||
| #include "drivers/rangefinder/rangefinder_us42.h" | ||
|
|
||
| #include "build/debug.h" | ||
|
|
||
| #define US42_MAX_RANGE_CM 500 // vcc=3.3v -> 500cm; vcc=5v -> 700cm | ||
| #define US42_DETECTION_CONE_DECIDEGREES 250 | ||
| #define US42_DETECTION_CONE_EXTENDED_DECIDEGREES 300 | ||
| #define US42_MIN_PROBE_INTERVAL 50 | ||
|
|
||
| #define US42_I2C_ADDRESS 0x70 | ||
| #define US42_I2C_REGISTRY_BASE 0x00 | ||
| #define US42_I2C_REGISTRY_PROBE 0x51 | ||
| #define US42_I2C_REGISTRY_STATUS_OK 0x00 | ||
| #define US42_I2C_REGISTRY_DISTANCE_HIGH 0x00 | ||
| #define US42_I2C_REGISTRY_DISTANCE_LOW 0x01 | ||
|
|
||
| volatile int32_t us42MeasurementCm = RANGEFINDER_OUT_OF_RANGE; | ||
| static int16_t minimumReadingIntervalMs = US42_MIN_PROBE_INTERVAL; | ||
| static uint32_t timeOfLastMeasurementMs; | ||
| uint8_t nullProbeCommandValue[0]; | ||
| static bool isUs42Responding = false; | ||
|
|
||
| static void us42Init(rangefinderDev_t *rangefinder) | ||
| { | ||
| busWriteBuf(rangefinder->busDev, US42_I2C_REGISTRY_PROBE, nullProbeCommandValue, 0); | ||
| } | ||
|
|
||
| void us42Update(rangefinderDev_t *rangefinder) | ||
| { | ||
| uint8_t data[2]; | ||
| isUs42Responding = busReadBuf(rangefinder->busDev, US42_I2C_REGISTRY_PROBE, data, 2); | ||
|
|
||
| if (isUs42Responding) { | ||
| us42MeasurementCm = (int32_t)data[0] << 8 | (int32_t)data[1]; | ||
|
|
||
| if (us42MeasurementCm > US42_MAX_RANGE_CM) { | ||
| us42MeasurementCm = RANGEFINDER_OUT_OF_RANGE; | ||
| } | ||
|
|
||
| } else { | ||
| us42MeasurementCm = RANGEFINDER_HARDWARE_FAILURE; | ||
| } | ||
|
|
||
| const timeMs_t timeNowMs = millis(); | ||
| if (timeNowMs > timeOfLastMeasurementMs + minimumReadingIntervalMs) { | ||
| // measurement repeat interval should be greater than minimumReadingIntervalMs | ||
| // to avoid interference between connective measurements. | ||
| timeOfLastMeasurementMs = timeNowMs; | ||
| busWriteBuf(rangefinder->busDev, US42_I2C_REGISTRY_PROBE, nullProbeCommandValue, 0); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get the distance that was measured by the last pulse, in centimeters. | ||
| */ | ||
| int32_t us42GetDistance(rangefinderDev_t *rangefinder) | ||
| { | ||
| UNUSED(rangefinder); | ||
| return us42MeasurementCm; | ||
| } | ||
|
|
||
| static bool deviceDetect(busDevice_t * busDev) | ||
| { | ||
| for (int retry = 0; retry < 5; retry++) { | ||
| uint8_t inquiryResult; | ||
|
|
||
| delay(150); | ||
|
|
||
| bool ack = busRead(busDev, US42_I2C_REGISTRY_BASE, &inquiryResult); | ||
| if (ack && inquiryResult == US42_I2C_REGISTRY_STATUS_OK) { | ||
| return true; | ||
| } | ||
| }; | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| bool us42Detect(rangefinderDev_t *rangefinder) | ||
| { | ||
| rangefinder->busDev = busDeviceInit(BUSTYPE_I2C, DEVHW_US42, 0, OWNER_RANGEFINDER); | ||
| if (rangefinder->busDev == NULL) { | ||
| return false; | ||
| } | ||
|
|
||
| if (!deviceDetect(rangefinder->busDev)) { | ||
| busDeviceDeInit(rangefinder->busDev); | ||
| return false; | ||
| } | ||
|
|
||
| rangefinder->delayMs = RANGEFINDER_US42_TASK_PERIOD_MS; | ||
| rangefinder->maxRangeCm = US42_MAX_RANGE_CM; | ||
| rangefinder->detectionConeDeciDegrees = US42_DETECTION_CONE_DECIDEGREES; | ||
| rangefinder->detectionConeExtendedDeciDegrees = US42_DETECTION_CONE_EXTENDED_DECIDEGREES; | ||
|
|
||
| rangefinder->init = &us42Init; | ||
| rangefinder->update = &us42Update; | ||
| rangefinder->read = &us42GetDistance; | ||
|
|
||
| return true; | ||
| } | ||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* | ||
| * This file is part of INAV. | ||
| * | ||
| * INAV is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * INAV is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with INAV. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #define RANGEFINDER_US42_TASK_PERIOD_MS 100 | ||
|
|
||
| bool us42Detect(rangefinderDev_t *dev); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
STDPeriph I2C driver probably needs patching to allow null writes as well. Can you please check?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need to patch "driver/bus_busdev_i2c.c" and "driver/bus_i2c_soft.c" because they are using for-loops based on paramater "len" [... for (i = 0; i < len; i++) { ...]. so it should work... but how can i test it? any other i2c-drivers out there?