在Arduino上使用两个HCSR 04超声波传感器计算速度

wi3ka0sx  于 11个月前  发布在  其他
关注(0)|答案(2)|浏览(80)

我们有两个传感器。我们想要计算物体经过传感器前面的速度,就像汽车一样。首先物体经过传感器1的前面,然后经过传感器2的前面。在两个传感器之间,我们有5厘米的距离。我们还有两个LED灯,当物体经过传感器前面时,我们想要点亮它们。例如,当物体经过第一个传感器前面时,我们想要LED 1点亮它们。当物体经过第二个传感器前时,我们希望led 2亮起。但我们还需要传感器计算速度,因为我们希望它们识别通过的物体是汽车还是人。如果物体的速度大于60 km/h,传感器会感觉到它是汽车,如果不是,它是人,led灯就会亮起,led灯不会亮起
我使用的代码根本不起作用

f4t66c6m

f4t66c6m1#

你没有给我们任何东西来工作,但是假设一个假设的函数objectPresent( sensorid )在没有物体存在时返回0,在存在时返回1,并且其中sensorid是两个接近检测器之一,那么速度可以通过每个传感器上0到1转换之间的时间来确定。物体的大小可以通过其通过传感器之一的时间来估计。

┌───────────────────────────────┐
          │                               │
SENSOR A  │                               │
──────────┘                               └────────────────────────
          |                               │
          │                     ┌─────────┼─────────────────────┐
SENSOR B  │                     │         │                     │
          │                     │         │                     |
──────────┼─────────────────────┘         │                     └──
          │                     |         │
          │                     │         │
          ◄─────────────────────►         │
          │                     │         │
          │Time determines speed          │
          │                               │
          ◄───────────────────────────────►
          │ Time at speed determines size │

字符串
因此,给定从传感器对捕获所需定时的函数,例如:

bool sensor_time( unsigned long& speed_time,
                  unsigned long& size_time )
{
  static int stateA = objectPresent( sensorA ) ;
  static int stateB = objectPresent( sensorB ) ;
  static unsigned long sense_a_time_us = 0ul ;
  static unsigned long sense_b_time_us = 0ul ;
  static unsigned long pulse_time_us = 0ul ;
  bool new_detection = false ;

  unsigned long now_us = micros() ;

  // Test sensor A state...
  int new_state = objectPresent( sensorA ) ;
  if( stateA != new_state )
  {
    if( new_state == 1 )
    {
      // 0 to 1 transition
      sense_a_time_us = now_us ;
    }
    else if( stateB == 1) // Reject narrow objects
    {
      // On 1 to 0 transition, latch new timing
      pulse_time_us = now_us - sense_a_time_us ;
      size_time = pulse_time_us ;
      speed_time = sense_b_time_us - sense_a_time_us ;      
      
      new_detection = true ;
    }
    
    stateA = new_state ;
    digitalWrite( LEDA, stateA ) ;
  }

  // Test sensor B sensor state...
  new_state = objectPresent( sensorB ) ;
  if( stateB != new_state )
  {
    if( new_state == 1 )
    {
      sense_b_time_us = now_us ;
    }
    
    stateB = new_state ;
    digitalWrite( LEDB, stateB ) ;
  }
  
  return new_detection ; 
}


然后,该函数可以在执行循环中使用,因此:

void loop() 
{
  static unsigned people_count = 0 ;

  // Get sensor timing/count
  unsigned long speed_time = 0 ;
  unsigned_long size_time = 0 ;
  
  // If new object detected...
  if( sensor_time( speed_time, size_time ) )
  {
    static const unsigned SENSOR_SEPARATION_CM = 5u ;
    unsigned long transit_speed_cm_per_sec = (SENSOR_SEPARATION_CM * 10000ul) / speed_time ;
    unsigned long object_size_cm = (transit_speed_cm_per_sec * size_time) / 1000000ul ;

    static const MAX_HUMAN_SPEED_CM_SEC = 1000ul ;
    static const MAX_HUMAN_WIDTH_CM = 100ul ;
    bool human = transit_speed_cm_per_sec <= MAX_HUMAN_SPEED_CM_SEC && object_size_cm <= MAX_HUMAN_WIDTH_CM ;

    if( human )
    {
      people_count++ ;

      ...
    }
    
  }
}

ma8fv8wu

ma8fv8wu2#

我不知道你做了什么。换我也会这么做。

//Global Variables
unsigned long initialTime=0, endTime=0;                                 //Time of entrance and exit of the object
bool read1=true, read2=true;                                            //Flags to read or not the ultrasonic snesors
int Ultrasonic1Trig, Ultrasonic1Trig, Ultrasonic2Trig, Ultrasonic2Echo; //Pins the ultrasonic sensors are using
float carLimit, maxLimit;

//User Functions
bool presenceDetected(int trig, int echo){
  float dist;
  //Measure distance
  //
  //
  
  if (dist>maxLimit)
    return false;//No object detected
  else
    return true;//Object detected
}

void setup() {
  //Setup of all devices

}

void loop() {
  if (presenceDetected(Ultrasonic1Trig, Ultrasonic1Trig)){
    digitalWrite(led1, true)//Turn on led
    if (read1){
      initialTime=millis();//record time of entrance
      read1=false;//Stop reading
    }
  }
  else
    digitalWrite(led1, false);//Turn off led
    
    
  if (presenceDetected(Ultrasonic2Trig, Ultrasonic2Trig)){
    digitalWrite(led2, true);//Turn on led
    if (read2){
      endTime=millis();//record time of exit
      read2=false;//Stop reading
    }
  }
  else
    digitalWrite(led2, false);//Turn off led
    
    
  if (!read1 && !read2){//Both measurements have been recorded
    unsigned long timeObj = endTime-initialTime;//Time from entrance to exit
    float speed = 500/timeObj;//Speed in m/s
    Serial.print("Speed of object is: ");
    Serial.print(speed);
    Serial.println(" m/s");
    if (speed > carLimit)
      Serial.println("It's a car!");
    else 
      Serial.println("It's a person!");
    read1=true;
    read2=true;
  }
}

字符串
这只是一个有一些基本想法的模板。

相关问题