Explains with examples of how to use the library functions
The basic procedure to get measurement data from the sensor is:
The source code for the procedure described above is as follows:
// \~japanese シリアル接続でのセンサとの接続と距離データの取得 // \~english Connects to the sensor via serial interface and gets range data #include "urg_sensor.h" #include "urg_utils.h" #include <stdlib.h> int main(void) { urg_t urg; int ret; long *length_data; int length_data_size; // \~japanese "COM1" は、センサが認識されているデバイス名にする必要がある // \~english "COM1" is, in this case, the device name detected for the sensor const char connect_device[] = "COM1"; const long connect_baudrate = 115200; // \~japanese センサに対して接続を行う。 // \~english Connects to the sensor ret = urg_open(&urg, URG_SERIAL, connect_device, connect_baudrate); // \todo check error code // \~japanese データ受信のための領域を確保する // \~english Allocates memory to hold received measurement data length_data = (long *)malloc(sizeof(long) * urg_max_data_size(&urg)); // \todo check length_data is not NULL // \~japanese 距離データの計測開始。 // \~english Starts range data measurement ret = urg_start_measurement(&urg, URG_DISTANCE, 1, 0); // \todo check error code // \~japanese センサから距離データを取得する。 // \~english Receives the measurement data length_data_size = urg_get_distance(&urg, length_data, NULL); // \todo process length_data array // \~japanese センサとの接続を閉じる。 // \~english Disconnects from the sensor urg_close(&urg); return 0; }
In the program, the data structure urg_t is initialized by the urg_open() function. This function additionally enables and manages sensor communications, and gets sensor state information and necessary configuration parameters.
In the program, range data is stored in the length_data array, the number of scan points (steps) is then copied into length_data_size variable.
To display each range data, you can add the following fragment to the program:
for (i = 0; i < length_data_size; ++i) { printf("%ld, ", length_data[i]); } printf("\n");
For an Ethernet interface connection, use the urg_open() function as illustrated in the following code fragment:
// \~japanese イーサーネット接続でのセンサとの接続と距離データの取得 // \~english Connects to the sensor via Ethernet and receives range data const char connect_address[] = "192.168.0.10"; const long connect_port = 10940; // \~japanese センサに対して接続を行う。 // \~english Connects to the sensor ret = urg_open(&urg, URG_ETHERNET, connect_address, connect_port); // \todo check error code
The following code illustrates how to get scan_times continuous scans from the sensor:
// \~japanese scan_times 回のスキャンデータを取得 // \~english Obtains measurement data for scan_times scans // \~japanese urg_start_measurement() 関数でスキャン回数を指定し // \~english Uses urg_start_measurement() function to define the number of scans // \~japanese urg_get_distance() 関数で指定した回数だけデータを受信する。 // \~english Uses urg_get_distance() function to receive the measurement data const int scan_times = 123; int length_data_size; int i; // \~japanese センサから距離データを取得する。 // \~english Starts range data measurement ret = urg_start_measurement(&urg, URG_DISTANCE, scan_times, 0); // \todo check error code for (i = 0; i < scan_times; ++i) { length_data_size = urg_get_distance(&urg, length_data, NULL); // \todo process length_data array }
Normally, to receive measurement data from the sensor, the functions urg_start_measurement() and urg_get_distance() are called (in pairs) many times (hand-shake measurement mode). If the number of scans is used, the function urg_start_measurement() is called just once, and then only call urg_get_distance() repeatedly to receive data more effectively from the sensor.
Measurement parameters allow defining the measurement scope (start and end steps), step grouping, and scan skipping. For example, reducing the measurement scope and/or grouping steps reduces the volume of data (message length) transmitted from the sensor and thus the library response is increased. The following code illustrates how to configure measurement parameters.
// \~japanese 計測パラメータの設定 // \~english Configures measurement parameters // \~japanese センサに対して接続を行う。 // \~japanese 接続を行うと、計測パラメータの設定は初期化される // \~english Connects to the sensor // \~english Upon connection, measurement parameters are initialized (default values) ret = urg_open(&urg, URG_SERIAL, connect_device, connect_baudrate); // \todo check error code // \~japanese 計測範囲を指定する // \~japanese センサ正面方向の 90 [deg] 範囲のデータ取得を行い、ステップ間引きを行わない例 // \~english Defines the measurement scope (start, end steps) // \~english Defines a measurement scope of 90 [deg] at the front of the sensor, and no step grouping in this example first_step = urg_deg2step(&urg, -45); last_step = urg_deg2step(&urg, +45); skip_step = 0; ret = urg_set_scanning_parameter(&urg, first_step, last_step, skip_step); // \todo check error code // \~japanese 計測回数と計測の間引きを指定して、計測を開始する // \~japanese 123 回の計測を指示し、スキャンの間引きを行わない例 // \~english Defines the number of scans // \~english 123 scans are requested, and no scan skipping in this example scan_times = 123; skip_scan = 0; ret = urg_start_measurement(&urg, URG_DISTANCE, scan_times, skip_scan); // \todo check error code
It is possible to know the timing at which measurement data was captured on the sensor using the urg_get_distance() function. The following code illustrates how to extract the timestamp value on the received scan data.
// \~japanese タイムスタンプの取得 // \~english Gets timestamp values // \~japanese urg_get_distance() 関数に変数を与え、タイムスタンプを取得する。 // \~english Uses the urg_get_distance() function and returns the timestamp values for each scan const int scan_times = 123; int length_data_size; long timestamp; int i; // \~japanese センサから距離データを取得する。 // \~english Starts range data measurement ret = urg_start_measurement(&urg, URG_DISTANCE, scan_times, 0); // \todo check error code for (i = 0; i < scan_times; ++i) { length_data_size = urg_get_distance(&urg, length_data, ×tamp); // \todo process length_data array // \~japanese 取得したタイムスタンプを出力する // \~english Outputs the received timestamp value printf("%ld\n", timestamp); }
Timestamp and measurement data are obtained on the sensor at a constant scanning speed (scan period). If the computer using this library has a heavy load, it is possible that the time interval between scans is longer than the sensor scan period. If that is the case, use the step grouping and/or scan skipping parameters to reduce the load.
To convert measurement data into X-Y coordinates, use the urg_index2rad() function to calculate the angle (in radians) for each step. The following code illustrates how to perform coordinates conversion.
// \~japanese 距離データを X-Y 座標系に変換して表示する // \~english Converts data to X-Y coordinates and displays it length_data_size = urg_get_distance(&urg, length_data, NULL); for (i = 0; i < length_data_size; ++i) { // \~japanese その距離データのラジアン角度を求め、X, Y の座標値を計算する // \~english Gets the angle in radians for range data, and convert to X-Y coordinates double radian; long length; long x; long y; radian = urg_index2rad(&urg, i); length = length_data[i]; // \todo check length is valid x = (long)(length * cos(radian)); y = (long)(length * sin(radian)); printf("(%ld, %ld), ", x, y); } printf("\n");
For the urg_index2rad() function, the front of the sensor corresponds to 0 [radians] and using that reference all the other angles are calculated.