diff --git a/Firmware/LowLevel/platformio.ini b/Firmware/LowLevel/platformio.ini index 8fcaa171..6ed4e0c2 100644 --- a/Firmware/LowLevel/platformio.ini +++ b/Firmware/LowLevel/platformio.ini @@ -42,6 +42,8 @@ debug_build_flags = -O0 -g -ggdb build_src_filter = +<*> -<.git/> -<.svn/> - - +build_flags = -DSHUTDOWN_ESC_WHEN_IDLE + [env:0_13_X] lib_ignore = JY901_SERIAL,JY901_I2C lib_deps = ${env.lib_deps} diff --git a/Firmware/LowLevel/src/datatypes.h b/Firmware/LowLevel/src/datatypes.h index f5f931a1..96494114 100644 --- a/Firmware/LowLevel/src/datatypes.h +++ b/Firmware/LowLevel/src/datatypes.h @@ -57,7 +57,7 @@ struct ll_status { // Bit 0: Initialized (i.e. setup() was a success). If this is 0, all other bits are meaningless. // Bit 1: Raspberry Power // Bit 2: Charging enabled - // Bit 3: don't care + // Bit 3: ESC power // Bit 4: Rain detected // Bit 5: Sound available // Bit 6: Sound busy diff --git a/Firmware/LowLevel/src/imu/LSM6DSO/imu.cpp b/Firmware/LowLevel/src/imu/LSM6DSO/imu.cpp index a22af4bf..e5e040a5 100644 --- a/Firmware/LowLevel/src/imu/LSM6DSO/imu.cpp +++ b/Firmware/LowLevel/src/imu/LSM6DSO/imu.cpp @@ -49,12 +49,16 @@ bool imu_read(float *acceleration_mss, float *gyro_rads, float *mag_uT) { success &= IMU.Get_X_Axes(accelerometer) == 0; success &= IMU.Get_G_Axes(gyroscope) == 0; - acceleration_mss[0] = accelerometer[0] * 9.81 / 1000.0; - acceleration_mss[1] = accelerometer[1] * 9.81 / 1000.0; + // Left down: Y = -g + acceleration_mss[1] = accelerometer[0] * 9.81 / 1000.0; + // Nose down: X = -g + acceleration_mss[0] = -accelerometer[1] * 9.81 / 1000.0; + // Flat: Z = +g acceleration_mss[2] = accelerometer[2] * 9.81 / 1000.0; - gyro_rads[0] = gyroscope[0] * (PI / 180.0) / 1000.0; - gyro_rads[1] = gyroscope[1] * (PI / 180.0) / 1000.0; + // Datasheet shows gyro and acceleromter axes are aligned + gyro_rads[1] = -gyroscope[0] * (PI / 180.0) / 1000.0; + gyro_rads[0] = gyroscope[1] * (PI / 180.0) / 1000.0; gyro_rads[2] = gyroscope[2] * (PI / 180.0) / 1000.0; mag_uT[0] = 0; diff --git a/Firmware/LowLevel/src/imu/MPU9250/imu.cpp b/Firmware/LowLevel/src/imu/MPU9250/imu.cpp index 1a901b9a..91275c8b 100644 --- a/Firmware/LowLevel/src/imu/MPU9250/imu.cpp +++ b/Firmware/LowLevel/src/imu/MPU9250/imu.cpp @@ -20,14 +20,20 @@ bool imu_read(float *acceleration_mss, float *gyro_rads, float *mag_uT) { IMU.readSensor(); - acceleration_mss[0] = IMU.getAccelX_mss(); - acceleration_mss[1] = IMU.getAccelY_mss(); + // TODO: test this + // Left down: Y = -g + acceleration_mss[1] = -IMU.getAccelX_mss(); + // Nose down: X = -g + acceleration_mss[0] = IMU.getAccelY_mss(); + // Flat: Z = +g acceleration_mss[2] = IMU.getAccelZ_mss(); - gyro_rads[0] = IMU.getGyroX_rads(); - gyro_rads[1] = IMU.getGyroY_rads(); + // MPU-9250 Datasheet shows gyro and acceleromter axes are aligned + gyro_rads[1] = -IMU.getGyroX_rads(); + gyro_rads[0] = IMU.getGyroY_rads(); gyro_rads[2] = -IMU.getGyroZ_rads(); + // Mag is NED coordinates mag_uT[0] = IMU.getMagX_uT(); mag_uT[1] = IMU.getMagY_uT(); mag_uT[2] = IMU.getMagZ_uT(); diff --git a/Firmware/LowLevel/src/imu/WT901_I2C/imu.cpp b/Firmware/LowLevel/src/imu/WT901_I2C/imu.cpp index 28c2647f..af7a115e 100644 --- a/Firmware/LowLevel/src/imu/WT901_I2C/imu.cpp +++ b/Firmware/LowLevel/src/imu/WT901_I2C/imu.cpp @@ -23,6 +23,7 @@ bool imu_read(float *acceleration_mss, float *gyro_rads, float *mag_uT) IMU.GetMag(); IMU.GetGyro(); + // WT901 axes seem to be corrected? acceleration_mss[0] = (float)IMU.stcAcc.a[0] / 32768.0f * 16.0f * 9.81f; acceleration_mss[1] = (float)IMU.stcAcc.a[1] / 32768.0f * 16.0f * 9.81f; acceleration_mss[2] = (float)IMU.stcAcc.a[2] / 32768.0f * 16.0f * 9.81f; diff --git a/Firmware/LowLevel/src/imu/WT901_SERIAL/imu.cpp b/Firmware/LowLevel/src/imu/WT901_SERIAL/imu.cpp index 7127e081..a7f113ae 100644 --- a/Firmware/LowLevel/src/imu/WT901_SERIAL/imu.cpp +++ b/Firmware/LowLevel/src/imu/WT901_SERIAL/imu.cpp @@ -23,6 +23,7 @@ bool init_imu() bool imu_read(float *acceleration_mss, float *gyro_rads, float *mag_uT) { + // WT901 axes seem to be corrected? acceleration_mss[0] = (float)IMU.stcAcc.a[0] / 32768.0f * 16.0f * 9.81f; acceleration_mss[1] = (float)IMU.stcAcc.a[1] / 32768.0f * 16.0f * 9.81f; acceleration_mss[2] = (float)IMU.stcAcc.a[2] / 32768.0f * 16.0f * 9.81f; diff --git a/Firmware/LowLevel/src/main.cpp b/Firmware/LowLevel/src/main.cpp index fb642f4b..b4e02088 100644 --- a/Firmware/LowLevel/src/main.cpp +++ b/Firmware/LowLevel/src/main.cpp @@ -36,6 +36,7 @@ #define LIFT_EMERGENCY_MILLIS 100 // Time for both wheels to be lifted in order to count as emergency (0 disable). This is to filter uneven ground. #define BUTTON_EMERGENCY_MILLIS 20 // Time for button emergency to activate. This is to debounce the button. +#define SHUTDOWN_ESC_MAX_PITCH 15.0 // Do not shutdown ESCs if absolute pitch angle is greater than this // Define to stream debugging messages via USB // #define USB_DEBUG @@ -118,6 +119,7 @@ bool ROS_running = false; unsigned long charging_disabled_time = 0; float imu_temp[9]; +float pitch_angle = 0, roll_angle = 0, tilt_angle = 0; uint16_t ui_version = 0; // Last received UI firmware version uint8_t ui_topic_bitmask = Topic_set_leds; // UI subscription, default to Set_LEDs @@ -388,6 +390,9 @@ void setup() { pinMode(PIN_ENABLE_CHARGE, OUTPUT); digitalWrite(PIN_ENABLE_CHARGE, HIGH); + pinMode(PIN_ESC_SHUTDOWN, OUTPUT); + digitalWrite(PIN_ESC_SHUTDOWN, LOW); + gpio_init(PIN_RASPI_POWER); gpio_put(PIN_RASPI_POWER, true); gpio_set_dir(PIN_RASPI_POWER, true); @@ -671,6 +676,12 @@ void loop() { sendMessage(&imu_message, sizeof(struct ll_imu)); last_imu_millis = now; + + // Update pitch, roll, tilt + pitch_angle = atan2f(imu_temp[0], imu_temp[2]) * 180.0f / M_PI; + roll_angle = atan2f(imu_temp[1], imu_temp[2]) * 180.0f / M_PI; + float accXY = sqrtf((imu_temp[0]*imu_temp[0]) + (imu_temp[1]*imu_temp[1])); + tilt_angle = atan2f(accXY, imu_temp[2]) * 180.0f / M_PI; } if (now - last_status_update_millis > STATUS_CYCLETIME) { @@ -686,6 +697,21 @@ void loop() { #else status_message.charging_current = -1.0f; #endif + +#ifdef SHUTDOWN_ESC_WHEN_IDLE + // ESC power saving when mower is IDLE + if(!ROS_running || last_high_level_state.current_mode != HighLevelMode::MODE_IDLE || fabs(pitch_angle) > SHUTDOWN_ESC_MAX_PITCH) { + // Enable escs if not idle, or if ROS is not running, or on a slope + digitalWrite(PIN_ESC_SHUTDOWN, LOW); + status_message.status_bitmask |= 0b1000; + } else { + digitalWrite(PIN_ESC_SHUTDOWN, HIGH); + status_message.status_bitmask &= 0b11110111; + } +#else + status_message.status_bitmask |= 0b1000; +#endif + status_message.status_bitmask = (status_message.status_bitmask & 0b11111011) | ((charging_allowed & 0b1) << 2); status_message.status_bitmask = (status_message.status_bitmask & 0b11011111) | ((sound_available & 0b1) << 5); diff --git a/configs/xESC/YardForce_Classic_Drive_App.xml b/configs/xESC/YardForce_Classic_Drive_App.xml index 576b43a9..271c2919 100644 --- a/configs/xESC/YardForce_Classic_Drive_App.xml +++ b/configs/xESC/YardForce_Classic_Drive_App.xml @@ -15,7 +15,7 @@ 0 50000 0 - 0 + 4 3 0 15000 diff --git a/configs/xESC/YardForce_Classic_Drive_Motor.xml b/configs/xESC/YardForce_Classic_Drive_Motor.xml index e0d0093c..2957a3e9 100644 --- a/configs/xESC/YardForce_Classic_Drive_Motor.xml +++ b/configs/xESC/YardForce_Classic_Drive_Motor.xml @@ -1,14 +1,13 @@ - 2 1 0 2 0 2 -2 - 5 - -5 + 1 + -1 15 -100000 100000 @@ -24,7 +23,7 @@ 90 85 100 - 0.15 + 0 0.005 0.95 1.5e+06 @@ -187,7 +186,7 @@ p, li { white-space: pre-wrap; } p, li { white-space: pre-wrap; } </style></head><body style=" font-family:'Roboto'; font-size:12pt; font-weight:400; font-style:normal;"> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Some comments about the motor quality. Images can be added as well.</p></body></html> - 1 + 0 45 65 0.05 diff --git a/configs/xESC/YardForce_Classic_Mower_App.xml b/configs/xESC/YardForce_Classic_Mower_App.xml index 576b43a9..271c2919 100644 --- a/configs/xESC/YardForce_Classic_Mower_App.xml +++ b/configs/xESC/YardForce_Classic_Mower_App.xml @@ -15,7 +15,7 @@ 0 50000 0 - 0 + 4 3 0 15000 diff --git a/configs/xESC/YardForce_Classic_Mower_Motor.xml b/configs/xESC/YardForce_Classic_Mower_Motor.xml index 436424de..50797c4f 100644 --- a/configs/xESC/YardForce_Classic_Mower_Motor.xml +++ b/configs/xESC/YardForce_Classic_Mower_Motor.xml @@ -1,6 +1,5 @@ - 2 1 0 2 @@ -10,9 +9,9 @@ 2 -1 15 - -100000 - 100000 - 0.8 + -14100 + 14100 + 0.95 300 1500 8 @@ -24,7 +23,7 @@ 90 85 100 - 0.15 + 0 0.005 0.95 1.5e+06 @@ -48,8 +47,8 @@ 4 -1 2000 - 0.597682 - 574.348 + 0.5977 + 574.35 25000 0.12 0 @@ -63,11 +62,11 @@ 2 2000 30000 - 0.000597682 - 2.3181e-05 - 0.574348 - 0.00712734 - 1.96854e+07 + 0.00059768 + 2.318e-05 + 0.5743 + 0.007127 + 1.969e+07 0.05 -1 10 @@ -94,7 +93,7 @@ 0 0 0 - 22.51 + 22.5 0.1 0 0 @@ -106,12 +105,12 @@ 0.001 1 1 - 2086.73 - 2047.37 - 2114.23 - 0.1375 - 0.1366 - -0.2742 + 1996.01 + 2023.24 + 2074.1 + 0.0011 + -0.0005 + -0.0005 0 0 0 @@ -187,7 +186,7 @@ p, li { white-space: pre-wrap; } p, li { white-space: pre-wrap; } </style></head><body style=" font-family:'Roboto'; font-size:12pt; font-weight:400; font-style:normal;"> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Some comments about the motor quality. Images can be added as well.</p></body></html> - 1 + 0 45 65 0.05 diff --git a/configs/xESC/YardForce_SA650ECO_Drive_Motor.xml b/configs/xESC/YardForce_SA650ECO_Drive_Motor.xml index 37a630c9..013c72e1 100644 --- a/configs/xESC/YardForce_SA650ECO_Drive_Motor.xml +++ b/configs/xESC/YardForce_SA650ECO_Drive_Motor.xml @@ -1,14 +1,13 @@ - 2 1 0 2 0 - 6.51866 - -6.51866 - 5 - -5 + 2 + -2 + 1 + -1 15 -100000 100000 @@ -17,14 +16,14 @@ 1500 8 40 - 23.8 - 21 + 6 + 6 0 85 90 - 85 - 100 - 0.15 + 190 + 190 + 0 0.005 0.95 1.5e+06 @@ -48,8 +47,8 @@ 4 -1 2000 - 2.1884 - 1238.79 + 2.2089 + 2605.41 25000 0.12 0 @@ -63,11 +62,11 @@ 2 2000 30000 - 0.00218841 - 0.00085154 - 1.2388 - 0.013281 - 5.67e+06 + 0.00220893 + 0.00135457 + 2.6054 + 0.007529 + 1.764e+07 0.05 -1 10 @@ -81,12 +80,12 @@ 0.1 0.05 255 - 167 - 36 - 2 + 166 + 34 + 0 101 - 135 - 68 + 134 + 67 255 500 4000 @@ -106,12 +105,12 @@ 0.001 1 1 - 2061.01 - 2052.83 - 2059.88 - -0.0004 - 0.0011 - -0.0006 + 2060.26 + 2108.85 + 2033.53 + 0.0003 + 0.0016 + -0.002 0 0 0 @@ -163,7 +162,7 @@ 0 0.61 1 - 14 + 4 1 0.083 0 @@ -187,7 +186,7 @@ p, li { white-space: pre-wrap; } p, li { white-space: pre-wrap; } </style></head><body style=" font-family:'Roboto'; font-size:12pt; font-weight:400; font-style:normal;"> <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Some comments about the motor quality. Images can be added as well.</p></body></html> - 1 + 0 45 65 0.05