Line data Source code
1 : #include "SIM_config.h" 2 : 3 : #if AP_SIM_TEMPERATURE_SHT3X_ENABLED 4 : 5 : #include "SIM_I2CDevice.h" 6 : 7 : /* 8 : Simulator for the SHT3X temperature sensor 9 : 10 : ./Tools/autotest/sim_vehicle.py --gdb --debug -v ArduSub -A --speedup=1 11 : 12 : param set TEMP1_TYPE 8 13 : param set TEMP1_ADDR 0x44 14 : 15 : */ 16 : 17 : namespace SITL { 18 : 19 : class SHT3x : public I2CDevice 20 : { 21 : public: 22 : 23 : SHT3x(); 24 : 25 : void update(const class Aircraft &aircraft) override; 26 : 27 : int rdwr(I2C::i2c_rdwr_ioctl_data *&data) override; 28 : 29 : private: 30 : 31 : // should be a call on aircraft: 32 : float last_temperature = -1000.0f; 33 : 34 : enum class State { 35 : UNKNOWN = 22, 36 : RESET = 23, 37 : CLEAR_STATUS = 24, 38 : IDLE = 25, 39 : MEASURING = 26, 40 : MEASURED = 27, 41 : } state = State::RESET; 42 : 43 : uint32_t state_start_time_ms; 44 : 45 3714 : void set_state(State new_state) { 46 3714 : state = new_state; 47 3714 : state_start_time_ms = AP_HAL::millis(); 48 3714 : } 49 28073 : uint32_t time_in_state_ms() const { 50 28073 : return AP_HAL::millis() - state_start_time_ms; 51 : } 52 : 53 : float get_sim_temperature() const; 54 : 55 : float temperature_for_adc(uint32_t adc) const; 56 : uint32_t calculate_adc(float temperature) const; 57 : uint32_t adc; 58 : 59 : void pack_reading(SITL::I2C::i2c_msg &msg); 60 : 61 : enum class Command { 62 : RESET = 0x30A2, 63 : READ_STATUS = 0xF32D, 64 : CLEAR_STATUS = 0x3041, 65 : MEASURE = 0x2C06, // clock-stretching-enabled, high-repatability 66 : READ_SN = 0x3780, 67 : }; 68 : 69 : union { 70 : struct { 71 : uint8_t checksum_status : 1; 72 : uint8_t command_status : 1; 73 : uint8_t reserved1 : 2; 74 : uint8_t reset_detected : 1; 75 : uint8_t reserved2 : 5; 76 : uint8_t T_tracking_alert : 1; 77 : uint8_t RH_tracking_alert : 1; 78 : uint8_t reserved3 : 1; 79 : uint8_t header_status : 1; 80 : uint8_t reserved4 : 1; 81 : uint8_t alert_pending : 1; 82 : }; 83 : uint16_t value; 84 : } status; 85 : 86 : struct { 87 : float temperature; 88 : float humidity; 89 : } measurement; 90 : 91 : void reset(); 92 : }; 93 : 94 : } // namespace SITL 95 : 96 : #endif // AP_SIM_TEMPERATURE_SHT3X_ENABLED