Advertisement
microrobotics

TFMini I2C

Mar 11th, 2024
1,347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Arduino.h>
  2. #include <Wire.h>
  3.  
  4. const uint8_t SENSOR_ADDRESS = 0x10; // TFMini I2C Address
  5. const uint8_t DISTANCE_DATA_LENGTH = 7; // Data length for distance data request
  6. const uint8_t MAX_RETRY_COUNT = 3; // Maximum number of retries for reading data
  7. const uint16_t UNRELIABLE_DATA = 0xFFFF; // Indicating unreliable data
  8.  
  9. struct SensorData {
  10.   uint16_t distance;  // Measured distance
  11.   uint16_t strength;  // Signal strength
  12.   uint8_t rangeType;  // Range scale: 0 (short), 3 (intermediate), 7 (long)
  13.   bool isValid;       // Flag to indicate if the data is valid
  14. };
  15.  
  16. unsigned long readFrequencyMillis = 50; // Default read frequency: 50 ms
  17.  
  18. void setup() {
  19.   Wire.begin();
  20.   Serial.begin(115200);
  21.   Serial.println("TFMini I2C Test");
  22. }
  23.  
  24. void loop() {
  25.   static unsigned long lastReadTime = 0; // Tracks the last read time
  26.   unsigned long currentTime = millis(); // Current time
  27.  
  28.   if (currentTime - lastReadTime >= readFrequencyMillis) {
  29.     SensorData data = tryReadSensorData(SENSOR_ADDRESS, MAX_RETRY_COUNT);
  30.  
  31.     if (data.isValid && data.distance != UNRELIABLE_DATA) {
  32.       Serial.print("\tDist[");
  33.       Serial.print(data.distance);
  34.       Serial.print("]\tStrength[");
  35.       Serial.print(data.strength);
  36.       Serial.print("]\tMode[");
  37.       Serial.print(data.rangeType);
  38.       Serial.println("]");
  39.     }
  40.  
  41.     lastReadTime = currentTime; // Update the last read time
  42.   }
  43. }
  44.  
  45. void setReadFrequency(unsigned long frequencyMillis) {
  46.   readFrequencyMillis = frequencyMillis;
  47. }
  48.  
  49. SensorData tryReadSensorData(uint8_t deviceAddress, uint8_t retries) {
  50.   while (retries--) {
  51.     SensorData data = readSensorData(deviceAddress);
  52.     if (data.isValid) {
  53.       return data; // Valid data received
  54.     }
  55.     delay(10); // Wait a bit before retrying
  56.   }
  57.   return SensorData{0, 0, 0, false}; // Return invalid data after exhausting retries
  58. }
  59.  
  60. SensorData readSensorData(uint8_t deviceAddress) {
  61.   SensorData data = {0, 0, 0, false};
  62.  
  63.   Wire.beginTransmission(deviceAddress);
  64.   Wire.write(0x01); // MSB
  65.   Wire.write(0x02); // LSB
  66.   Wire.write(DISTANCE_DATA_LENGTH); // Request distance data
  67.   if (Wire.endTransmission(false) != 0) {
  68.     return data; // Sensor did not acknowledge
  69.   }
  70.  
  71.   Wire.requestFrom(deviceAddress, DISTANCE_DATA_LENGTH);
  72.   if (Wire.available() < DISTANCE_DATA_LENGTH) {
  73.     return data; // Not enough data available
  74.   }
  75.  
  76.   for (uint8_t i = 0; i < DISTANCE_DATA_LENGTH; i++) {
  77.     uint8_t incoming = Wire.read();
  78.     switch (i) {
  79.       case 0:
  80.         data.isValid = (incoming == 0x01);
  81.         break;
  82.       case 2:
  83.         data.distance = incoming; // LSB of the distance
  84.         break;
  85.       case 3:
  86.         data.distance |= incoming << 8; // MSB of the distance
  87.         if (data.distance == UNRELIABLE_DATA) {
  88.           data.isValid = false;
  89.         }
  90.         break;
  91.       case 4:
  92.         data.strength = incoming; // LSB of signal strength
  93.         break;
  94.       case 5:
  95.         data.strength |= incoming << 8; // MSB of signal strength
  96.         break;
  97.       case 6:
  98.         data.rangeType = incoming; // Range scale
  99.         break;
  100.     }
  101.   }
  102.   return data;
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement