Skip to content

Commit

Permalink
Rel. 2.1.2
Browse files Browse the repository at this point in the history
- Bug fix Battery setup: some limits was not set according to cell number
- Added HAL sensor calibration for future use (WIP)
  • Loading branch information
mspider65 committed Sep 3, 2020
1 parent be8f56e commit 8326d45
Show file tree
Hide file tree
Showing 7 changed files with 465 additions and 19 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "spider65.ebike.tsdz2_esp32"
minSdkVersion 23
targetSdkVersion 28
versionCode 8
versionName "2.1.1"
versionCode 9
versionName "2.1.2"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

Expand Down
8 changes: 6 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="spider65.ebike.tsdz2_esp32">

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
Expand All @@ -11,10 +12,12 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS"
<uses-permission
android:name="android.permission.WRITE_SETTINGS"
android:maxSdkVersion="25"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"
<uses-permission
android:name="android.permission.WRITE_SECURE_SETTINGS"
android:maxSdkVersion="25"
tools:ignore="ProtectedPermissions" />

Expand All @@ -26,6 +29,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".activities.HalSensorCalibrationActivity"></activity>
<activity
android:name=".activities.ESP32ConfigActivity"
android:label="@string/title_activity_esp32_config"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ private void saveCfg() {
}
cfg.ui8_battery_cells_number = val;

if ((val = checkRange(binding.whResetET, 36*cfg.ui8_battery_cells_number, 43*cfg.ui8_battery_cells_number)) == null) {
showDialog(getString(R.string.wh_reset_volt), getString(R.string.range_error, 36*cfg.ui8_battery_cells_number, 43*cfg.ui8_battery_cells_number));
if ((val = checkRange(binding.whResetET, 38*cfg.ui8_battery_cells_number, 43*cfg.ui8_battery_cells_number)) == null) {
showDialog(getString(R.string.wh_reset_volt), getString(R.string.range_error, 38*cfg.ui8_battery_cells_number, 43*cfg.ui8_battery_cells_number));
return;
}
cfg.ui16_battery_voltage_reset_wh_counter_x10 = val;

if ((val = checkRange(binding.batteryCutOffET, 24*cfg.ui8_battery_cells_number, 33*cfg.ui8_battery_cells_number)) == null) {
showDialog(getString(R.string.volt_cut_off), getString(R.string.range_error, 24*cfg.ui8_battery_cells_number, 33*cfg.ui8_battery_cells_number));
if ((val = checkRange(binding.batteryCutOffET, 26*cfg.ui8_battery_cells_number, 33*cfg.ui8_battery_cells_number)) == null) {
showDialog(getString(R.string.volt_cut_off), getString(R.string.range_error, 26*cfg.ui8_battery_cells_number, 33*cfg.ui8_battery_cells_number));
return;
}
cfg.ui16_battery_low_voltage_cut_off_x10 = val;
Expand All @@ -96,26 +96,26 @@ private void saveCfg() {
}
cfg.ui16_battery_pack_resistance_x1000 = val;

if ((val = checkRange(binding.cellOvervoltET, 300, 440)) == null) {
showDialog(getString(R.string.cell_overvolt), getString(R.string.range_error, 300, 450));
if ((val = checkRange(binding.cellOvervoltET, 39*cfg.ui8_battery_cells_number, 44*cfg.ui8_battery_cells_number)) == null) {
showDialog(getString(R.string.cell_overvolt), getString(R.string.range_error, 39*cfg.ui8_battery_cells_number, 44*cfg.ui8_battery_cells_number));
return;
}
cfg.ui8_li_io_cell_overvolt_x100 = val;

if ((val = checkRange(binding.cellEmptyET, 250, 330)) == null) {
showDialog(getString(R.string.cell_empty), getString(R.string.range_error, 259, 330));
if ((val = checkRange(binding.cellEmptyET, 27*cfg.ui8_battery_cells_number, 33*cfg.ui8_battery_cells_number)) == null) {
showDialog(getString(R.string.cell_empty), getString(R.string.range_error, 27*cfg.ui8_battery_cells_number, 33*cfg.ui8_battery_cells_number));
return;
}
cfg.ui8_li_io_cell_empty_x100 = val;

if ((val = checkRange(binding.cellOneBarET, 250, 340)) == null) {
showDialog(getString(R.string.cell_one_bar), getString(R.string.range_error, 250, 340));
if ((val = checkRange(binding.cellOneBarET, 28*cfg.ui8_battery_cells_number, 34*cfg.ui8_battery_cells_number)) == null) {
showDialog(getString(R.string.cell_one_bar), getString(R.string.range_error, 28*cfg.ui8_battery_cells_number, 34*cfg.ui8_battery_cells_number));
return;
}
cfg.ui8_li_io_cell_one_bar_x100 = val;

if ((val = checkRange(binding.cellAllBarsET, 370, 430)) == null) {
showDialog(getString(R.string.cell_all_bars), getString(R.string.range_error, 370, 430));
if ((val = checkRange(binding.cellAllBarsET, 38*cfg.ui8_battery_cells_number, 43*cfg.ui8_battery_cells_number)) == null) {
showDialog(getString(R.string.cell_all_bars), getString(R.string.range_error, 38*cfg.ui8_battery_cells_number, 43*cfg.ui8_battery_cells_number));
return;
}
cfg.ui8_li_io_cell_full_bars_x100 = val;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
package spider65.ebike.tsdz2_esp32.activities;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import java.util.Timer;
import java.util.TimerTask;

import spider65.ebike.tsdz2_esp32.R;
import spider65.ebike.tsdz2_esp32.TSDZBTService;
import spider65.ebike.tsdz2_esp32.data.TSDZ_Debug;
import spider65.ebike.tsdz2_esp32.utils.Utils;

public class HalSensorCalibrationActivity extends AppCompatActivity {
private static final String TAG = "CadenceSensorCalib";
private static final byte CMD_HAL_CALIBRATION = 7;
private static final byte CALIBRATION_STOP = 0;
private static final byte CALIBRATION_ON = 1;
private static final byte CALIBRATION_START = 2;

private IntentFilter mIntentFilter = new IntentFilter();
TextView erpsTV,hal1TV,hal2TV,hal3TV,hal4TV,hal5TV,hal6TV;
Button startBT,stopBT, cancelBT;
boolean calibrationRunning = false;
TSDZBTService service;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hal_sensor_calibration);

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

erpsTV = findViewById(R.id.erpsTV);
hal1TV = findViewById(R.id.hal1TV);
hal2TV = findViewById(R.id.hal2TV);
hal3TV = findViewById(R.id.hal3TV);
hal4TV = findViewById(R.id.hal4TV);
hal5TV = findViewById(R.id.hal5TV);
hal6TV = findViewById(R.id.hal6TV);
startBT = findViewById(R.id.startButton);
stopBT = findViewById(R.id.stopButton);
cancelBT = findViewById(R.id.cancelButton);

mIntentFilter.addAction(TSDZBTService.TSDZ_DEBUG_BROADCAST);
mIntentFilter.addAction(TSDZBTService.TSDZ_COMMAND_BROADCAST);
service = TSDZBTService.getBluetoothService();
if (service == null || service.getConnectionStatus() != TSDZBTService.ConnectionState.CONNECTED) {
showDialog(getString(R.string.error), getString(R.string.connection_error));
}
}

@Override
protected void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, mIntentFilter);
}

@Override
protected void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
}

@Override
protected void onStop() {
resetTimer();
if (calibrationRunning && service != null)
service.writeCommand(new byte[] {CMD_HAL_CALIBRATION,CALIBRATION_STOP});
calibrationRunning = false;
finish();
super.onStop();
}

public void onButtonClick(View view) {
switch (view.getId()) {
case R.id.startButton:
startCalib();
break;
case R.id.stopButton:
stopCalib();
break;
case R.id.cancelButton:
resetTimer();
if (calibrationRunning && service != null)
service.writeCommand(new byte[] {CMD_HAL_CALIBRATION,CALIBRATION_STOP});
calibrationRunning = false;
finish();
break;
}
}

private void startCalib() {
if (service == null || service.getConnectionStatus() != TSDZBTService.ConnectionState.CONNECTED) {
showDialog(getString(R.string.error), getString(R.string.connection_error));
return;
}
service.writeCommand(new byte[] {CMD_HAL_CALIBRATION,CALIBRATION_ON});
}

private void stopCalib() {
if (service == null || service.getConnectionStatus() != TSDZBTService.ConnectionState.CONNECTED) {
showDialog(getString(R.string.error), getString(R.string.connection_error));
return;
}
service.writeCommand(new byte[] {CMD_HAL_CALIBRATION,CALIBRATION_STOP});
}

private void showDialog (String title, String message) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
if (title != null)
builder.setTitle(title);
builder.setMessage(message);
builder.setPositiveButton(android.R.string.ok, null);
builder.show();
}

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() == null)
return;
byte[] data = intent.getByteArrayExtra(TSDZBTService.VALUE_EXTRA);
if (TSDZBTService.TSDZ_DEBUG_BROADCAST.equals(intent.getAction())) {
Log.d(TAG,"TSDZ_DEBUG_BROADCAST: " + Utils.bytesToHex(data));
TSDZ_Debug debugData = new TSDZ_Debug();
debugData.setData(data);
int val;
erpsTV.setText(String.valueOf(debugData.motorERPS));
val = ((debugData.adcThrottle & 255) << 8) + (debugData.throttle & 255);
hal1TV.setText(String.valueOf(val));
hal2TV.setText(String.valueOf(debugData.torqueSensorValue));
val = ((debugData.rxcErrors & 255) << 8) + (debugData.rxlErrors & 255);
hal3TV.setText(String.valueOf(val));
hal4TV.setText(String.valueOf(debugData.pTorque*100));
hal5TV.setText(String.valueOf(debugData.pcbTemperature*10));
hal6TV.setText(String.valueOf(debugData.notUsed));
} else if (TSDZBTService.TSDZ_COMMAND_BROADCAST.equals(intent.getAction())) {
Log.d(TAG,"TSDZ_COMMAND_BROADCAST: " + Utils.bytesToHex(data));
if (data[0] == CMD_HAL_CALIBRATION) {
switch(data[1]) {
case CALIBRATION_ON:
if (data[2] != (byte)0x0) {
showDialog(getString(R.string.error), getString(R.string.cadenceStartError));
} else {
// start calibration procedure 5sec after motor startup
scheduleTimer();
calibrationRunning = true;
startBT.setEnabled(false);
stopBT.setEnabled(true);
}
break;
case CALIBRATION_STOP:
if (data[2] != (byte)0x0) {
showDialog(getString(R.string.error), getString(R.string.cadenceStopError));
} else {
resetTimer();
calibrationRunning = false;
startBT.setEnabled(true);
stopBT.setEnabled(false);
}
break;
case (byte)0xff:
showDialog(getString(R.string.error), getString(R.string.commandError));
break;
}
}
}
}
};

private Timer timer;
private class TimerExpired extends TimerTask {
@Override
public void run(){
Log.w(TAG, "Timeout!");
service.writeCommand(new byte[] {CMD_HAL_CALIBRATION,CALIBRATION_START});
}
}

private void scheduleTimer() {
resetTimer();
TimerExpired timerExpired = new TimerExpired();
timer = new Timer();
timer.schedule(timerExpired, 5000);
}

private void resetTimer() {
if (timer != null) {
timer.cancel();
timer = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ private void saveCfg() {

checked = binding.torqueFixCB.isChecked();
if (checked) {
if ((val = checkRange(binding.torqueADCOffsetET, 0, 300)) == null) {
showDialog(getString(R.string.torque_adc_offset), getString(R.string.range_error, 0, 100));
if ((val = checkRange(binding.torqueADCOffsetET, 40, 300)) == null) {
showDialog(getString(R.string.torque_adc_offset), getString(R.string.range_error, 40, 300));
return;
}
cfg.ui16_torque_offset_ADC = val;
Expand Down
Loading

0 comments on commit 8326d45

Please sign in to comment.