Code:
float measure_distance(){ float distance_cm = 0.0; gpio_set_input_enabled( ECHO_PIN, 1 ); gpio_put( TRIGGER_PIN, 0 ); // 设置低电平 sleep_ms(2); // 等待 uint echo_pin = gpio_get( ECHO_PIN);// printf("\nPulsed the pin high echo pin is %d", echo_pin ); gpio_put( TRIGGER_PIN, 1); // 设置高电平 sleep_ms( 10 ); // 等待三毫秒 gpio_put( TRIGGER_PIN, 0); // 开关触发低电平,再次让SR04发送脉冲 /*The echo pin outputs a pulse between 150 micro-seconds and 25 mili-seconds, or if no object is found, it will send a 38 ms pulse. speed of sound is 343 meters per second. This would depend upon the elevation and hummidity, but sufficiently accurate for our application. 回声引脚输出的脉冲在150微秒到25毫秒之间,或者如果没有找到物体,它将发送38毫秒的脉冲。 声速是每秒343米。这将取决于海拔和湿度,但对我们的应用来说足够准确。speed = distance travelled / time taken 速度 = 距离 / 时间so, distance = (speed * time taken)/2 -- Divide by 2 because we are listening to the echo 所以,距离 = (速度 * 时间)/2 -- 需要除以2,因为我们需要顾及 回声[echo]Now, speed of sound is 343 meters/second, whch is 3.43 centimeters per second, which is 现在,声速是343米/秒,也就是3.43厘米每秒,也就是3.43/ 1000000 = 0.0343 cm/microsecond. 3.43/ 1000000 = 0.0343 厘米 / 微秒. */ absolute_time_t listen_start_time = get_absolute_time(); absolute_time_t max_wait_time = delayed_by_ms( listen_start_time, 30 ); // No point waiting more than 30 Mili sec. // 没必要等待超过30毫秒 do{ absolute_time_t t_now = get_absolute_time(); //int64_t t_now = get_absolute_time(); // 获取绝对时间戳 if(t_now > max_wait_time) { break; } echo_pin = gpio_get( ECHO_PIN); if( echo_pin != 0 ){ // 我们收到了回声 absolute_time_t first_echo_time = t_now; printf(" the pin went high "); while(echo_pin == 1 && t_now < max_wait_time){ echo_pin = gpio_get( ECHO_PIN ); t_now = get_absolute_time(); }printf(" pin is at %d", echo_pin );if( echo_pin == 1 ){break;// 返回值 0cm}//printf("Start %llu ", listen_start_time );//printf( " end %llu ", t_now ); int64_t pulse_high_time = absolute_time_diff_us( first_echo_time, t_now );float pulse_high_time_float = (float) pulse_high_time; printf(" High Time %f ", pulse_high_time_float ); distance_cm = pulse_high_time_float * 0.01715; //pulse_high_time_float / 58.0 ; break; } } while( true ); gpio_set_input_enabled( ECHO_PIN, 0 ); return distance_cm;}
invalid operands to binary > (have 'absolute_time_t' and 'absolute_time_t')
I don't understand. Why is that?
Request answer, please.
Statistics: Posted by SweetColore — Sat May 11, 2024 1:13 pm — Replies 0 — Views 12