Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unsafe access to .data() and _data in Matrix #12964

Merged
merged 5 commits into from
Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/ecl
Submodule ecl updated from 3b32ee to 62a1e0
2 changes: 1 addition & 1 deletion src/lib/matrix
Original file line number Diff line number Diff line change
Expand Up @@ -495,9 +495,9 @@ bool AttitudeEstimatorQ::init()

// Fill rotation matrix
Dcmf R;
R.setRow(0, i);
R.setRow(1, j);
R.setRow(2, k);
R.row(0) = i;
R.row(1) = j;
R.row(2) = k;

// Convert to quaternion
_q = R;
Expand Down
5 changes: 4 additions & 1 deletion src/modules/commander/accelerometer_calibration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,10 @@ calibrate_return read_accelerometer_avg(int sensor_correction_sub, int (&subs)[m
for (unsigned i = 0; i < max_accel_sens; i++) {
matrix::Vector3f accel_sum_vec(&accel_sum[i][0]);
accel_sum_vec = board_rotation * accel_sum_vec;
memcpy(&accel_sum[i][0], accel_sum_vec.data(), sizeof(accel_sum[i]));

for (size_t j = 0; j < 3; j++) {
accel_sum[i][j] = accel_sum_vec(j);
}
}

for (unsigned s = 0; s < max_accel_sens; s++) {
Expand Down
9 changes: 4 additions & 5 deletions src/modules/ekf2/ekf2_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,8 @@ void Ekf2::run()
}

if (range_finder_updated) {
_ekf.setRangeData(range_finder.timestamp, range_finder.current_distance);
// TODO: Nico's pr will add proper quality handling, for now we put it to unkown
_ekf.setRangeData(range_finder.timestamp, range_finder.current_distance, -1.f);
}

// Save sensor limits reported by the rangefinder
Expand Down Expand Up @@ -1323,8 +1324,7 @@ void Ekf2::run()
}

// The rotation of the tangent plane vs. geographical north
matrix::Quatf q;
_ekf.copy_quaternion(q.data());
matrix::Quatf q = _ekf.get_quaternion();

lpos.yaw = matrix::Eulerf(q).psi();

Expand Down Expand Up @@ -1787,8 +1787,7 @@ const Vector3f Ekf2::get_vel_body_wind()
{
// Used to correct baro data for positional errors

matrix::Quatf q;
_ekf.copy_quaternion(q.data());
matrix::Quatf q = _ekf.get_quaternion();
matrix::Dcmf R_to_body(q.inversed());

// Calculate wind-compensated velocity in body frame
Expand Down
5 changes: 4 additions & 1 deletion src/modules/mavlink/mavlink_messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3431,7 +3431,10 @@ class MavlinkStreamAttitudeTarget : public MavlinkStream

} else {
matrix::Quatf q = matrix::Eulerf(att_sp.roll_body, att_sp.pitch_body, att_sp.yaw_body);
memcpy(&msg.q[0], q.data(), sizeof(msg.q));

for (size_t i = 0; i < 4; i++) {
msg.q[i] = q(i);
}
}

msg.body_roll_rate = att_rates_sp.roll;
Expand Down
8 changes: 5 additions & 3 deletions src/modules/sensors/voted_sensors_update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,8 @@ void VotedSensorsUpdate::magPoll(vehicle_magnetometer_s &magnetometer)
_last_magnetometer[uorb_index].magnetometer_ga[1] = vect(1);
_last_magnetometer[uorb_index].magnetometer_ga[2] = vect(2);

_mag.voter.put(uorb_index, mag_report.timestamp, vect.data(), mag_report.error_count, _mag.priority[uorb_index]);
_mag.voter.put(uorb_index, mag_report.timestamp, _last_magnetometer[uorb_index].magnetometer_ga, mag_report.error_count,
_mag.priority[uorb_index]);
}
}

Expand Down Expand Up @@ -834,13 +835,14 @@ void VotedSensorsUpdate::baroPoll(vehicle_air_data_s &airdata)
_baro_device_id[uorb_index] = baro_report.device_id;

got_update = true;
Vector3f vect(baro_report.pressure, baro_report.temperature, 0.f);

float vect[3] = {baro_report.pressure, baro_report.temperature, 0.f};

_last_airdata[uorb_index].timestamp = baro_report.timestamp;
_last_airdata[uorb_index].baro_temp_celcius = baro_report.temperature;
_last_airdata[uorb_index].baro_pressure_pa = corrected_pressure;

_baro.voter.put(uorb_index, baro_report.timestamp, vect.data(), baro_report.error_count, _baro.priority[uorb_index]);
_baro.voter.put(uorb_index, baro_report.timestamp, vect, baro_report.error_count, _baro.priority[uorb_index]);
}
}

Expand Down
23 changes: 9 additions & 14 deletions src/systemcmds/tests/test_matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,6 @@ bool MatrixTest::run_tests()

ut_declare_test_c(test_matrix, MatrixTest)

using matrix::Dcmf;
using matrix::Quatf;
using matrix::Eulerf;
using matrix::Vector3f;

using std::fabs;

bool MatrixTest::attitudeTests()
Expand Down Expand Up @@ -392,8 +387,6 @@ bool MatrixTest::integrationTests()
return true;
}

template class matrix::SquareMatrix<float, 3>;

bool MatrixTest::inverseTests()
{
float data[9] = {0, 2, 3,
Expand Down Expand Up @@ -438,8 +431,10 @@ bool MatrixTest::matrixAssignmentTests()

double eps = 1e-6f;

for (int i = 0; i < 9; i++) {
ut_test(fabs(data[i] - m2.data()[i]) < eps);
for (size_t i = 0; i < 3; i++) {
for (size_t j = 0; j < 3; j++) {
ut_test(fabs(data[i * 3 + j] - m2(i, j)) < eps);
}
}

float data_times_2[9] = {2, 4, 6, 8, 10, 12, 14, 16, 18};
Expand Down Expand Up @@ -493,8 +488,8 @@ bool MatrixTest::matrixAssignmentTests()
ut_test(fabs(m5(0, 0) - s) < 1e-5);

Matrix<float, 2, 2> m6;
m6.setRow(0, Vector2f(1, 1));
m6.setCol(0, Vector2f(1, 1));
m6.row(0) = Vector2f(1, 1);
m6.col(0) = Vector2f(1, 1);

return true;
}
Expand Down Expand Up @@ -578,7 +573,7 @@ bool MatrixTest::sliceTests()
};

Matrix<float, 2, 2> C(data_2);
A.set(C, 1, 1);
A.slice<2, 2>(1, 1) = C;

float data_2_check[9] = {
0, 2, 3,
Expand Down Expand Up @@ -737,7 +732,7 @@ bool MatrixTest::dcmRenormTests()

if (verbose) {
for (int row = 0; row < 3; row++) {
matrix::Vector3f rvec(A._data[row]);
Vector3f rvec(Matrix<float, 1, 3>(A.row(row)).transpose());
err += fabsf(1.0f - rvec.length());
}

Expand All @@ -749,7 +744,7 @@ bool MatrixTest::dcmRenormTests()
err = 0.0f;

for (int row = 0; row < 3; row++) {
matrix::Vector3f rvec(A._data[row]);
Vector3f rvec(Matrix<float, 1, 3>(A.row(row)).transpose());
err += fabsf(1.0f - rvec.length());
}

Expand Down