Arduino {code}racer API
... better know the details.

Functions

unsigned long CodeRacer::usonic_measure_cm ()
 Measures the distance to the next object in front of the ultra sonic sensor in cm. More...
 
unsigned long CodeRacer::usonic_measure_us ()
 Measures the distance to the next object in front of the ultra sonic sensor in microseconds. More...
 
unsigned long CodeRacer::usonic_measure_cm (unsigned long max_echo_run_time_us)
 Measures the distance to the next object in front of the ultra sonic sensor in cm. More...
 
unsigned long CodeRacer::usonic_measure_us (unsigned long max_echo_run_time_us)
 Measures the distance to the next object in front of the ultra sonic sensor in microseconds. More...
 
unsigned long CodeRacer::usonic_measure_single_shot_cm ()
 Measures the distance to the next object in front of the ultra sonic sensor in cm. More...
 
unsigned long CodeRacer::usonic_measure_single_shot_us ()
 Measures the distance to the next object in front of the ultra sonic sensor in microseconds. More...
 
unsigned long CodeRacer::usonic_measure_single_shot_cm (unsigned long max_echo_run_time_us)
 Measures the distance to the next object in front of the ultra sonic sensor in cm. More...
 
unsigned long CodeRacer::usonic_measure_single_shot_us (unsigned long max_echo_run_time_us)
 Measures the distance to the next object in front of the ultra sonic sensor in microseconds. More...
 

Detailed Description

Function Documentation

◆ usonic_measure_cm() [1/2]

unsigned long CodeRacer::usonic_measure_cm ( )

Measures the distance to the next object in front of the ultra sonic sensor in cm.

This is the medium one out of 3 measurements. The maximum measured distance is about 100cm and defined by the US_MAX_ECHO_TIME_US setting in the header file.

Returns
The measured distance in cm.

Definition at line 672 of file CodeRacer.cpp.

673 {
674  return(usonic_measure_cm(US_MAX_ECHO_TIME_US));
675 }

◆ usonic_measure_us() [1/2]

unsigned long CodeRacer::usonic_measure_us ( )

Measures the distance to the next object in front of the ultra sonic sensor in microseconds.

This is the medium one out of 3 measurements. The maximum measured distance is about 6000 microseconds and defined by the US_MAX_ECHO_TIME_US setting in the header file.

Returns
The measured distance in microseconds.

Definition at line 682 of file CodeRacer.cpp.

683  {
684  return(usonic_measure_us(US_MAX_ECHO_TIME_US));
685  }

◆ usonic_measure_cm() [2/2]

unsigned long CodeRacer::usonic_measure_cm ( unsigned long  max_echo_run_time_us)

Measures the distance to the next object in front of the ultra sonic sensor in cm.

This is the medium one out of 3 measurements. Be careful with high values for max_echo_run_time_us - this may increase run time due to the fact that if there is nothing in range of the sensor it will wait until this specified run time of the echo is over. The maximum range the sensor is specified for is about 300cm.

Parameters
max_echo_run_time_usDefines the maximum echo run time and by that the maximum of distance that can be measured.
Returns
The measured distance in cm.

Definition at line 694 of file CodeRacer.cpp.

695 {
696  unsigned long echo_runtime_us = usonic_measure_us(max_echo_run_time_us);
697  unsigned long distance_cm = echo_runtime_us * 0.0172;
698  //Serial.print("MEASURE_DISTANCE. Distance in cm is: ");
699  //Serial.println(distance_cm);
700  _usonic_distance_cm = distance_cm;
701  return(distance_cm);
702 }

◆ usonic_measure_us() [2/2]

unsigned long CodeRacer::usonic_measure_us ( unsigned long  max_echo_run_time_us)

Measures the distance to the next object in front of the ultra sonic sensor in microseconds.

This is the medium one out of 3 measurements. Be careful with high values for max_echo_run_time_us - this may increase run time due to the fact that if there is nothing in range of the sensor it will wait until this specified run time of the echo is over. The maximum range the sensor is specified for is about 300cm.

Parameters
max_echo_run_time_usDefines the maximum echo run time in microseconds and by that the maximum of distance that can be measured.
Returns
The measured distance in microseconds.

Definition at line 711 of file CodeRacer.cpp.

712 {
713  unsigned long echo_runtime_us[3] = { 0,0,0 };
714  uint8_t measnr = 0;
715 
716  do {
717  echo_runtime_us[measnr] = usonic_measure_single_shot_us(max_echo_run_time_us);
718  if (echo_runtime_us[measnr] > 200) {
719  measnr++;
720  }
721  } while (measnr < 3);
722 
723  // we will take the medium out of 3 values ...
724  if (echo_runtime_us[0] > echo_runtime_us[1]) { std::swap(echo_runtime_us[0], echo_runtime_us[1]); }
725  if (echo_runtime_us[1] > echo_runtime_us[2]) { std::swap(echo_runtime_us[1], echo_runtime_us[2]); }
726  if (echo_runtime_us[0] > echo_runtime_us[1]) { std::swap(echo_runtime_us[0], echo_runtime_us[1]); }
727 
728  //Serial.print("MEASURE_DISTANCE_US. Echo runtime in us is: ");
729  //Serial.println(echo_runtime_us[1]);
730 
731  // if the stop at minimal distance is enabeled - check for minimal distance is reached
732  if (true == _coderacer_stop_at_distance_enabled) {
733  if (echo_runtime_us[1] <= _usonic_stop_distance_us) {
734  _coderacer_stopped_at_min_distance = true;
735  stop_driving();
736  _coderacer_stop_at_distance_enabled = false;
737  }
738  }
739  _usonic_distance_us = echo_runtime_us[1];
740  return(echo_runtime_us[1]);
741 }

◆ usonic_measure_single_shot_cm() [1/2]

unsigned long CodeRacer::usonic_measure_single_shot_cm ( )

Measures the distance to the next object in front of the ultra sonic sensor in cm.

This is a one shot measurement. The maximum measured distance is about 6000 microseconds and defined by the US_MAX_ECHO_TIME_US setting in the header file.

Returns
The measured distance in cm.

Definition at line 748 of file CodeRacer.cpp.

749 {
750  return(usonic_measure_single_shot_cm(US_MAX_ECHO_TIME_US));
751 }

◆ usonic_measure_single_shot_us() [1/2]

unsigned long CodeRacer::usonic_measure_single_shot_us ( )

Measures the distance to the next object in front of the ultra sonic sensor in microseconds.

This is a one shot measurement. The maximum measured distance is about 6000 microseconds and defined by the US_MAX_ECHO_TIME_US setting in the header file.

Returns
The measured distance in microseconds.

Definition at line 758 of file CodeRacer.cpp.

759 {
760  return(usonic_measure_single_shot_us(US_MAX_ECHO_TIME_US));
761 }

◆ usonic_measure_single_shot_cm() [2/2]

unsigned long CodeRacer::usonic_measure_single_shot_cm ( unsigned long  max_echo_run_time_us)

Measures the distance to the next object in front of the ultra sonic sensor in cm.

This is a one shot measurement. Be careful with high values for max_echo_run_time_us - this may increase run time due to the fact that if there is nothing in range of the sensor it will wait until this specified run time of the echo is over. The maximum range the sensor is specified for is about 300cm.

Parameters
max_echo_run_time_usDefines the maximum echo run time in microseconds and by that the maximum of distance that can be measured.
Returns
The measured distance in cm.

Definition at line 770 of file CodeRacer.cpp.

771 {
772  // convert into cm ... 344m/sec is the speed of noise - thus 34400cm/sec ... or 34,400cm/milisec ... or 0,0344cm/microsec
773  // the echo has to go the distance twice - forth and back - so the duration has to be the half of the measured one
774  // distance_cm = echo_duration/2 * 0,0344 or distance_cm = echo_duration/2 / 29,1 or distance_cm = echo_duration * 0,0172
775  // distance_cm = (echo_duration/2) / 29.1;
776  unsigned long echo_runtime_us = usonic_measure_single_shot_us(max_echo_run_time_us);
777  unsigned long distance_cm = echo_runtime_us * 0.0172;
778  //Serial.print("MEASURE_DISTANCE. Distance in cm is: ");
779  //Serial.println(distance_cm);
780  _usonic_distance_cm = distance_cm;
781  return(distance_cm);
782 }

◆ usonic_measure_single_shot_us() [2/2]

unsigned long CodeRacer::usonic_measure_single_shot_us ( unsigned long  max_echo_run_time_us)

Measures the distance to the next object in front of the ultra sonic sensor in microseconds.

This is a one shot measurement. Be careful with high values for max_echo_run_time_us - this may increase run time due to the fact that if there is nothing in range of the sensor it will wait until this specified run time of the echo is over. The maximum range the sensor is specified for is about 300cm.

Parameters
max_echo_run_time_usDefines the maximum echo run time in microseconds and by that the maximum of distance that can be measured.
Returns
The measured distance in microseconds.

Definition at line 791 of file CodeRacer.cpp.

792  {
793  unsigned long echo_runtime_us;
794  // start measurement - send a short pulse "HIGH" to the TRIG pin of the ultrasonic sensor
795  pinMode(_us_echo_pin, OUTPUT);
796  pinMode(_us_echo_pin, INPUT);
797  digitalWrite(_us_trigger_pin, LOW);
798  delayMicroseconds(2);
799  digitalWrite(_us_trigger_pin, HIGH);
800  delayMicroseconds(10);
801  digitalWrite(_us_trigger_pin, LOW);
802  // measure runtime in microseconds until the ECHO pin aof the sensor goes HIGH
803  echo_runtime_us = pulseInLong(_us_echo_pin, HIGH, max_echo_run_time_us);
804  if (echo_runtime_us == 0) {
805  echo_runtime_us = max_echo_run_time_us; // US_MAX_ECHO_TIME_US;
806  }
807  //Serial.print("MEASURE_DISTANCE_US. Echo runtime in us is: ");
808  //Serial.println(echo_runtime_us);
809  _usonic_distance_us = echo_runtime_us;
810  return(echo_runtime_us);
811  }