diff --git a/app/src/main/java/spider65/ebike/tsdz2_esp32/TSDZConst.java b/app/src/main/java/spider65/ebike/tsdz2_esp32/TSDZConst.java
index 22de9a3..f516740 100644
--- a/app/src/main/java/spider65/ebike/tsdz2_esp32/TSDZConst.java
+++ b/app/src/main/java/spider65/ebike/tsdz2_esp32/TSDZConst.java
@@ -22,9 +22,14 @@ public interface TSDZConst {
byte CMD_STM8S_OTA_START = 0x03;
byte CMD_ESP_OTA_STATUS = 0x05;
byte CMD_STM8_OTA_STATUS = 0x06;
+ byte CMD_HALL_DATA = 0x07; // Notification sent during Motor Test
byte CMD_ESP32_CONFIG = 0x08;
byte CMD_STREET_MODE = 0x09;
byte CMD_ASSIST_MODE = 0x0A;
+ byte CMD_MOTOR_TEST = 0x0B; // following 3 bytes: START/STOP, Duty Cycle, Phase Angle adj
+ byte TEST_STOP = 0; // second byte of CMD_MOTOR_TEST command
+ byte TEST_START = 1; // second byte of CMD_MOTOR_TEST command
+
byte STREET_MODE_LCD_MASTER = 0;
byte STREET_MODE_FORCE_OFF = 1;
@@ -51,4 +56,12 @@ public interface TSDZConst {
// limit values used in the LevelSetupActivity
int PWM_DUTY_CYCLE_MAX = 254;
int WALK_ASSIST_DUTY_CYCLE_MAX = 80;
+
+ // Default Hall Counter Offset values
+ int DEFAULT_HALL_DOWN_OFFSET = 23; // Hall counter Offset for counter starting from Hall falling edge
+ int DEFAULT_HALL_UP_OFFSET = 43; // Hall counter Offset for counter starting from Hall rising edge
+
+ // Default motor Phase
+ int DEFAULT_PHASE_OFFSET = 4; // Phase angle adjust regarding the rotor reference Hall angle
+ int DEFAULT_PHASE_ANGLE = 64; // Phase has 90 deg difference from rotor position
}
diff --git a/app/src/main/java/spider65/ebike/tsdz2_esp32/activities/HallCalibrationActivity.java b/app/src/main/java/spider65/ebike/tsdz2_esp32/activities/HallCalibrationActivity.java
index 6842031..02c5a78 100644
--- a/app/src/main/java/spider65/ebike/tsdz2_esp32/activities/HallCalibrationActivity.java
+++ b/app/src/main/java/spider65/ebike/tsdz2_esp32/activities/HallCalibrationActivity.java
@@ -22,24 +22,22 @@
import spider65.ebike.tsdz2_esp32.R;
import spider65.ebike.tsdz2_esp32.TSDZBTService;
+import spider65.ebike.tsdz2_esp32.TSDZConst;
import spider65.ebike.tsdz2_esp32.data.TSDZ_Config;
import spider65.ebike.tsdz2_esp32.utils.LinearRegression;
import spider65.ebike.tsdz2_esp32.utils.RollingAverage;
+import static spider65.ebike.tsdz2_esp32.TSDZConst.CMD_HALL_DATA;
+import static spider65.ebike.tsdz2_esp32.TSDZConst.CMD_MOTOR_TEST;
+import static spider65.ebike.tsdz2_esp32.TSDZConst.TEST_START;
+import static spider65.ebike.tsdz2_esp32.TSDZConst.TEST_STOP;
+
public class HallCalibrationActivity extends AppCompatActivity {
private static final String TAG = "HallCalibration";
- private static final byte CMD_HALL_DATA = 0x07;
- private static final byte CMD_MOTOR_TEST = 0x0B;
- private static final byte TEST_STOP = 0;
- private static final byte TEST_START = 1;
- private static final byte CALIB_ANGLE_ADJ = 0;
+
private static final int SETUP_STEPS = 15;
private static final int AVG_SIZE = 150;
- private static final int DEFAULT_PHASE_OFFSET = 10;
- private static final int DEFAULT_PHASE_ANGLE = 64;
- private static final int DEFAULT_UP_DN_HALL_DIFF = 20;
-
private final TSDZ_Config cfg = new TSDZ_Config();
private final IntentFilter mIntentFilter = new IntentFilter();
@@ -102,7 +100,7 @@ protected void onCreate(Bundle savedInstanceState) {
errorTV[5] = findViewById(R.id.err6TV);
hallValuesTV = findViewById(R.id.hallValuesTV);
hallUpDnDiffTV = findViewById(R.id.hallUpDnDiffTV);
- resetBT = findViewById(R.id.resetButton);
+ resetBT = findViewById(R.id.defaultBT);
cancelBT = findViewById(R.id.exitButton);
saveBT = findViewById(R.id.saveButton);
progressBar = findViewById(R.id.progressBar);
@@ -186,18 +184,20 @@ public void onButtonClick(View view) {
TSDZBTService.getBluetoothService().writeCfg(cfg);
} else
showDialog(getString(R.string.error), getString(R.string.connection_error), false);
- } else if (view.getId() == R.id.resetButton) {
+ } else if (view.getId() == R.id.defaultBT) {
for (int i=0; i<6; i++) {
angleTV[i].setText(R.string.dash);
offsetTV[i].setText(R.string.dash);
errorTV[i].setText(R.string.dash);
}
for (int i=0; i<6; i++) {
- double v = Math.round(((30D + 60D * (double) i) * (256D / 360D) + DEFAULT_PHASE_OFFSET - DEFAULT_PHASE_ANGLE));
+ double v = Math.round(((30D + 60D * (double) i) * (256D / 360D)
+ + TSDZConst.DEFAULT_PHASE_OFFSET
+ - TSDZConst.DEFAULT_PHASE_ANGLE));
if (v < 0) v += 256;
phaseAngles[i] = (int)v;
}
- hallUpDnDiff = DEFAULT_UP_DN_HALL_DIFF;
+ hallUpDnDiff = TSDZConst.DEFAULT_HALL_UP_OFFSET - TSDZConst.DEFAULT_HALL_DOWN_OFFSET;
refresValues();
saveBT.setEnabled(true);
showDialog("", getString(R.string.defaultLoaded), false);
@@ -243,7 +243,7 @@ private void startCalib() {
msgCounter = 0;
for (RollingAverage ra: avg)
ra.reset();
- TSDZBTService.getBluetoothService().writeCommand(new byte[] {CMD_MOTOR_TEST, TEST_START, dutyCycles[step], CALIB_ANGLE_ADJ});
+ TSDZBTService.getBluetoothService().writeCommand(new byte[] {CMD_MOTOR_TEST, TEST_START, dutyCycles[step], (byte)cfg.ui8_phase_angle_adj});
}
private void stopCalib() {
@@ -297,7 +297,9 @@ private void updateResult() {
double offset = 0;
for (int i=0; i<6; i++) {
calcRefAngles[i] = calcRefAngles[i] - error + 256D/12D;
- int v = (int)Math.round(calcRefAngles[i]) +DEFAULT_PHASE_OFFSET -DEFAULT_PHASE_ANGLE;
+ int v = (int)Math.round(calcRefAngles[i])
+ + TSDZConst.DEFAULT_PHASE_OFFSET
+ - TSDZConst.DEFAULT_PHASE_ANGLE;
if (v<0) v += 256;
phaseAngles[i] = v;
offset += Math.abs(linearRegression[i].intercept());
diff --git a/app/src/main/java/spider65/ebike/tsdz2_esp32/activities/MotorSetupActivity.java b/app/src/main/java/spider65/ebike/tsdz2_esp32/activities/MotorSetupActivity.java
index af5818d..f8a8187 100644
--- a/app/src/main/java/spider65/ebike/tsdz2_esp32/activities/MotorSetupActivity.java
+++ b/app/src/main/java/spider65/ebike/tsdz2_esp32/activities/MotorSetupActivity.java
@@ -17,6 +17,7 @@
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import spider65.ebike.tsdz2_esp32.R;
import spider65.ebike.tsdz2_esp32.TSDZBTService;
+import spider65.ebike.tsdz2_esp32.TSDZConst;
import spider65.ebike.tsdz2_esp32.data.TSDZ_Config;
@@ -25,16 +26,10 @@ public class MotorSetupActivity extends AppCompatActivity {
private static final int MAX_ANGLE = 10;
private static final int MIN_OFFSET = 7;
private static final int MAX_OFFSET = 40;
- private static final int MIN_DIFF = 0;
private static final int MAX_DIFF = 30;
- private static final int DEFAULT_PHASE_SHIFT = 0;
- private static final int DEFAULT_DN_HALL_OFFSET = 8;
- private static final int DEFAULT_UP_DN_HALL_DIFF = 20;
-
-
private final IntentFilter mIntentFilter = new IntentFilter();
- private TextView angleValTV, offsetValTV, diffValTV;
+ private TextView angleValTV, hall124ValTV, hall356ValTV;
private final TSDZ_Config cfg = new TSDZ_Config();
@@ -46,9 +41,9 @@ protected void onCreate(Bundle savedInstanceState) {
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
- offsetValTV = findViewById(R.id.offsetValTV);
angleValTV = findViewById(R.id.angleValTV);
- diffValTV = findViewById(R.id.diffValTV);
+ hall124ValTV = findViewById(R.id.hall124ValTV);
+ hall356ValTV = findViewById(R.id.hall356ValTV);
mIntentFilter.addAction(TSDZBTService.TSDZ_CFG_READ_BROADCAST);
mIntentFilter.addAction(TSDZBTService.TSDZ_CFG_WRITE_BROADCAST);
@@ -58,6 +53,7 @@ protected void onCreate(Bundle savedInstanceState) {
showDialog("", getString(R.string.connection_error), true);
else
TSDZBTService.getBluetoothService().readCfg();
+ showDialog(getString(R.string.warning), getString(R.string.warning_cfg), false);
}
@Override
@@ -86,15 +82,15 @@ else if (view.getId() == R.id.saveButton) {
updated = true;
}
- int hallOffset = Integer.parseInt(offsetValTV.getText().toString());
- if (hallOffset != cfg.ui8_hall_counter_offset_down) {
- cfg.ui8_hall_counter_offset_down = hallOffset;
+ int hall124Offset = Integer.parseInt(hall124ValTV.getText().toString());
+ if (hall124Offset != cfg.ui8_hall_counter_offset_down) {
+ cfg.ui8_hall_counter_offset_down = hall124Offset;
updated = true;
}
- int hallDiff = Integer.parseInt(diffValTV.getText().toString());
- if (hallDiff != (cfg.ui8_hall_counter_offset_up - cfg.ui8_hall_counter_offset_down)) {
- cfg.ui8_hall_counter_offset_up = hallOffset + hallDiff;
+ int hall356Offset = Integer.parseInt(hall356ValTV.getText().toString());
+ if (hall356Offset != cfg.ui8_hall_counter_offset_up) {
+ cfg.ui8_hall_counter_offset_up = hall356Offset;
updated = true;
}
@@ -110,26 +106,26 @@ else if (view.getId() == R.id.saveButton) {
val = Integer.parseInt(angleValTV.getText().toString());
if (val > -MAX_ANGLE)
angleValTV.setText(String.valueOf(--val));
- } else if (view.getId() == R.id.offsetAddBT) {
- val = Integer.parseInt(offsetValTV.getText().toString());
- if (val < MAX_OFFSET)
- offsetValTV.setText(String.valueOf(++val));
- } else if (view.getId() == R.id.offsetSubBT) {
- val = Integer.parseInt(offsetValTV.getText().toString());
+ } else if (view.getId() == R.id.hall124AddBT) {
+ val = Integer.parseInt(hall124ValTV.getText().toString());
+ if ((val < MAX_OFFSET) && (val < Integer.parseInt(hall356ValTV.getText().toString())))
+ hall124ValTV.setText(String.valueOf(++val));
+ } else if (view.getId() == R.id.hall124SubBT) {
+ val = Integer.parseInt(hall124ValTV.getText().toString());
if (val > MIN_OFFSET)
- offsetValTV.setText(String.valueOf(--val));
- } else if (view.getId() == R.id.diffAddBT) {
- val = Integer.parseInt(diffValTV.getText().toString());
- if (val < MAX_DIFF)
- diffValTV.setText(String.valueOf(++val));
- } else if (view.getId() == R.id.diffSubBT) {
- val = Integer.parseInt(diffValTV.getText().toString());
- if (val > MIN_DIFF)
- diffValTV.setText(String.valueOf(--val));
- } else if (view.getId() == R.id.resetButton) {
- angleValTV.setText(String.format(Locale.getDefault(),"%d", DEFAULT_PHASE_SHIFT));
- offsetValTV.setText(String.format(Locale.getDefault(), "%d", DEFAULT_DN_HALL_OFFSET));
- diffValTV.setText(String.format(Locale.getDefault(), "%d", DEFAULT_UP_DN_HALL_DIFF));
+ hall124ValTV.setText(String.valueOf(--val));
+ } else if (view.getId() == R.id.hall356AddBT) {
+ val = Integer.parseInt(hall356ValTV.getText().toString());
+ if (val < MAX_OFFSET+MAX_DIFF)
+ hall356ValTV.setText(String.valueOf(++val));
+ } else if (view.getId() == R.id.hall356SubBT) {
+ val = Integer.parseInt(hall356ValTV.getText().toString());
+ if (val > Integer.parseInt(hall124ValTV.getText().toString()))
+ hall356ValTV.setText(String.valueOf(--val));
+ } else if (view.getId() == R.id.defaultBT) {
+ angleValTV.setText("0");
+ hall124ValTV.setText(String.format(Locale.getDefault(), "%d", TSDZConst.DEFAULT_HALL_DOWN_OFFSET));
+ hall356ValTV.setText(String.format(Locale.getDefault(), "%d", TSDZConst.DEFAULT_HALL_UP_OFFSET));
showDialog("", getString(R.string.defaultLoaded), false);
}
}
@@ -161,9 +157,8 @@ public void onReceive(Context context, Intent intent) {
int v = cfg.ui8_phase_angle_adj;
if (v >= 128) v-=256;
angleValTV.setText(String.format(Locale.getDefault(),"%d", v));
- offsetValTV.setText(String.format(Locale.getDefault(), "%d", cfg.ui8_hall_counter_offset_down));
- diffValTV.setText(String.format(Locale.getDefault(), "%d",
- cfg.ui8_hall_counter_offset_up - cfg.ui8_hall_counter_offset_down));
+ hall124ValTV.setText(String.format(Locale.getDefault(), "%d", cfg.ui8_hall_counter_offset_down));
+ hall356ValTV.setText(String.format(Locale.getDefault(), "%d", cfg.ui8_hall_counter_offset_up));
findViewById(R.id.saveButton).setEnabled(true);
break;
case TSDZBTService.TSDZ_CFG_WRITE_BROADCAST:
diff --git a/app/src/main/java/spider65/ebike/tsdz2_esp32/activities/MotorTestActivity.java b/app/src/main/java/spider65/ebike/tsdz2_esp32/activities/MotorTestActivity.java
index c4ed30d..3d61c99 100644
--- a/app/src/main/java/spider65/ebike/tsdz2_esp32/activities/MotorTestActivity.java
+++ b/app/src/main/java/spider65/ebike/tsdz2_esp32/activities/MotorTestActivity.java
@@ -12,7 +12,6 @@
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
-import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
@@ -29,18 +28,20 @@
import spider65.ebike.tsdz2_esp32.R;
import spider65.ebike.tsdz2_esp32.TSDZBTService;
+import spider65.ebike.tsdz2_esp32.utils.RollingAverage;
+
+import static spider65.ebike.tsdz2_esp32.TSDZConst.CMD_HALL_DATA;
+import static spider65.ebike.tsdz2_esp32.TSDZConst.CMD_MOTOR_TEST;
+import static spider65.ebike.tsdz2_esp32.TSDZConst.TEST_START;
+import static spider65.ebike.tsdz2_esp32.TSDZConst.TEST_STOP;
// Run motor (without load) with a fixed duty cycle and move rotor offset angle in order to find the best one (max ERPS)
// The result should be a table with ERPS and corresponding best offset angle
public class MotorTestActivity extends AppCompatActivity {
- private static final byte CMD_HALL_DATA = 0x07;
- private static final byte CMD_MOTOR_TEST = 0x0B;
- private static final byte TEST_STOP = 0;
- private static final byte TEST_START = 1;
- private static final int MAX_DUTY_CYCLE = 250;
- private static final int MAX_ANGLE = 8;
+ private static final int MAX_DUTY_CYCLE = 254;
+ private static final int MAX_ANGLE = 10;
private static final int CREATE_FILE_REQUEST_CODE = 40;
@@ -52,15 +53,12 @@ private enum TestStatus {
private final IntentFilter mIntentFilter = new IntentFilter();
TextView erpsTV, angleValTV, resultsTV;
- CheckBox autoTuneCB;
- EditText dcValET, dcStepET, dcMaxET, angleMaxET, stepSamplesET;
- Button startBT, stopBT, saveBT, exitBT, dcAddBT, dcSubBT, angleAddBT, angleSubBT;
+ EditText dcValET;
+ Button startBT, stopBT, exitBT, dcAddBT, dcSubBT, angleAddBT, angleSubBT;
TSDZBTService service;
- boolean autoTune = false;
int currentDC;
byte currentAngle;
- int maxDC, stepDC, maxAngle, stepSamples;
TestStatus status = TestStatus.stopped;
RollingAverage avg = new RollingAverage(256);
@@ -68,7 +66,7 @@ private enum TestStatus {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_motor_tuning);
+ setContentView(R.layout.activity_motor_test);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
@@ -76,18 +74,12 @@ protected void onCreate(Bundle savedInstanceState) {
erpsTV = findViewById(R.id.erpsTV);
dcValET = findViewById(R.id.dcValET);
angleValTV = findViewById(R.id.angleValTV);
- autoTuneCB = findViewById(R.id.autoTuneCB);
- dcStepET = findViewById(R.id.dcStepET);
- dcMaxET = findViewById(R.id.dcMaxET);
- angleMaxET = findViewById(R.id.angleMaxET);
- stepSamplesET = findViewById(R.id.stepSamplesET);
dcAddBT = findViewById(R.id.dcAddBT);
dcSubBT = findViewById(R.id.dcSubBT);
angleAddBT = findViewById(R.id.angleAddBT);
angleSubBT = findViewById(R.id.angleSubBT);
startBT = findViewById(R.id.startButton);
stopBT = findViewById(R.id.stopButton);
- saveBT = findViewById(R.id.saveButton);
exitBT = findViewById(R.id.exitButton);
resultsTV = findViewById(R.id.resultsTV);
resultsTV.setMovementMethod(new ScrollingMovementMethod());
@@ -140,82 +132,57 @@ protected void onStop() {
public void onButtonClick(View view) {
int val;
- switch (view.getId()) {
- case R.id.startButton:
- final AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle(R.string.warning);
- builder.setMessage(R.string.warningMotorStart);
- builder.setPositiveButton(R.string.ok, (dialog, which) -> {
+ if (view.getId() == R.id.startButton) {
+ final AlertDialog.Builder builder = new AlertDialog.Builder(this);
+ builder.setTitle(R.string.warning);
+ builder.setMessage(R.string.warningMotorStart);
+ builder.setPositiveButton(R.string.ok, (dialog, which) -> startTest());
+ builder.setNegativeButton(R.string.cancel, null);
+ builder.show();
+ } else if (view.getId() == R.id.stopButton) {
+ String s = "DutyCycle: " + currentDC
+ + " Angle: " + currentAngle
+ + " ERPS: " + String.format(Locale.getDefault(), "%.2f", 250000D/avg.getAverage())
+ + "\n";
+ resultsTV.append(s);
+ stopTest();
+ } else if (view.getId() == R.id.exitButton) {
+ if (status != TestStatus.stopped && service != null)
+ service.writeCommand(new byte[] {CMD_MOTOR_TEST,TEST_STOP});
+ status = TestStatus.stopped;
+ finish();
+ } else if (view.getId() == R.id.saveButton) {
+ saveFile();
+ } else if (view.getId() == R.id.dcAddBT) {
+ val = Integer.parseInt(dcValET.getText().toString());
+ if (val < MAX_DUTY_CYCLE) {
+ val += 1;
+ dcValET.setText(String.valueOf(val));
+ if (status != TestStatus.stopped)
startTest();
- });
- builder.setNegativeButton(R.string.cancel, null);
- builder.show();
- break;
- case R.id.stopButton:
- String s = "DutyCycle: " + currentDC
- + " Angle: " + currentAngle
- + " ERPS: " + String.format(Locale.getDefault(), "%.2f", 250000D/avg.getAverage())
- + "\n";
- resultsTV.append(s);
- stopTest();
- break;
- case R.id.exitButton:
- if (status != TestStatus.stopped && service != null)
- service.writeCommand(new byte[] {CMD_MOTOR_TEST,TEST_STOP});
- status = TestStatus.stopped;
- finish();
- break;
- case R.id.saveButton:
- saveFile();
- break;
- case R.id.dcAddBT:
- val = Integer.parseInt(dcValET.getText().toString());
- if (val < MAX_DUTY_CYCLE) {
- val += 1;
- dcValET.setText(String.valueOf(val));
- if (status != TestStatus.stopped)
- startTest();
- }
- break;
- case R.id.dcSubBT:
- val = Integer.parseInt(dcValET.getText().toString());
- if (val > 5) {
- val -= 1;
- dcValET.setText(String.valueOf(val));
- if (status != TestStatus.stopped)
- startTest();
- }
- break;
- case R.id.angleAddBT:
- val = Integer.parseInt(angleValTV.getText().toString());
- if (val < MAX_ANGLE) {
- angleValTV.setText(String.valueOf(++val));
- if (status != TestStatus.stopped)
- startTest();
- }
- break;
- case R.id.angleSubBT:
- val = Integer.parseInt(angleValTV.getText().toString());
- if (val > -MAX_ANGLE) {
- angleValTV.setText(String.valueOf(--val));
- if (status != TestStatus.stopped)
- startTest();
- }
- break;
- case R.id.autoTuneCB:
- autoTune = ((CheckBox) view).isChecked();
- if (autoTune) {
- dcStepET.setEnabled(true);
- dcMaxET.setEnabled(true);
- angleMaxET.setEnabled(true);
- stepSamplesET.setEnabled(true);
- } else {
- dcStepET.setEnabled(false);
- dcMaxET.setEnabled(false);
- angleMaxET.setEnabled(false);
- stepSamplesET.setEnabled(false);
- }
- break;
+ }
+ } else if (view.getId() == R.id.dcSubBT) {
+ val = Integer.parseInt(dcValET.getText().toString());
+ if (val > 5) {
+ val -= 1;
+ dcValET.setText(String.valueOf(val));
+ if (status != TestStatus.stopped)
+ startTest();
+ }
+ } else if (view.getId() == R.id.angleAddBT) {
+ val = Integer.parseInt(angleValTV.getText().toString());
+ if (val < MAX_ANGLE) {
+ angleValTV.setText(String.valueOf(++val));
+ if (status != TestStatus.stopped)
+ startTest();
+ }
+ } else if (view.getId() == R.id.angleSubBT) {
+ val = Integer.parseInt(angleValTV.getText().toString());
+ if (val > -MAX_ANGLE) {
+ angleValTV.setText(String.valueOf(--val));
+ if (status != TestStatus.stopped)
+ startTest();
+ }
}
}
@@ -225,71 +192,21 @@ private void startTest() {
return;
}
currentDC = Integer.parseInt(dcValET.getText().toString());
- if (autoTune) {
- stepDC = Integer.parseInt(dcStepET.getText().toString());
- if (stepDC > 128) {
- showDialog(getString(R.string.warning), "D.C. Step should be less than 129");
- return;
- }
- maxDC = Integer.parseInt(dcMaxET.getText().toString());
- if (maxDC > MAX_DUTY_CYCLE) {
- showDialog(getString(R.string.warning), "D.C. Max should be less than " + MAX_DUTY_CYCLE+1);
- return;
- }
- maxAngle = Integer.parseInt(angleMaxET.getText().toString());
- if (maxAngle > MAX_ANGLE) {
- showDialog(getString(R.string.warning), "Angle (+/-) should be less than " + MAX_ANGLE+1);
- return;
- }
- stepSamples = Integer.parseInt(stepSamplesET.getText().toString());
- if (stepSamples > 1024){
- showDialog(getString(R.string.warning), "Step Samples should be less than 1025");
- return;
- }
- currentAngle = (byte)(-maxAngle & 0xff);
- avg = new RollingAverage(stepSamples);
- } else {
- currentAngle = (byte)(Integer.parseInt(angleValTV.getText().toString()) & 0xff);
- avg = new RollingAverage(20);
- }
+ currentAngle = (byte)(Integer.parseInt(angleValTV.getText().toString()) & 0xff);
+ avg = new RollingAverage(20);
service.writeCommand(new byte[] {CMD_MOTOR_TEST,
TEST_START,
(byte)(currentDC & 0xff),
currentAngle});
}
- private boolean stopTest() {
+ private void stopTest() {
resetTimer();
if (service == null || service.getConnectionStatus() != TSDZBTService.ConnectionState.CONNECTED) {
showDialog(getString(R.string.error), getString(R.string.connection_error));
- return false;
+ return;
}
service.writeCommand(new byte[] {CMD_MOTOR_TEST, TEST_STOP});
- return true;
- }
-
- private void stepDone() {
- status = TestStatus.starting;
- String s = "DutyCycle: " + currentDC
- + " Angle: " + currentAngle
- + " ERPS: " + String.format(Locale.getDefault(), "%.2f", 250000D/avg.getAverage())
- + "\n";
- resultsTV.append(s);
-
- currentAngle++;
- if (currentAngle > maxAngle) {
- currentDC += stepDC;
- if (currentDC > maxDC) {
- stopTest();
- return;
- }
- currentAngle = (byte)(-maxAngle & 0xff);
- }
- avg = new RollingAverage(stepSamples);
- service.writeCommand(new byte[] {CMD_MOTOR_TEST,
- TEST_START,
- (byte)(currentDC & 0xff),
- currentAngle});
}
private void saveFile()
@@ -362,9 +279,7 @@ public void onReceive(Context context, Intent intent) {
status = TestStatus.stopped;
resetTimer();
startBT.setEnabled(true);
- saveBT.setEnabled(true);
exitBT.setEnabled(true);
- autoTuneCB.setEnabled(true);
dcAddBT.setEnabled(true);
dcSubBT.setEnabled(true);
angleAddBT.setEnabled(true);
@@ -383,15 +298,7 @@ public void onReceive(Context context, Intent intent) {
status = TestStatus.starting;
scheduleTimer();
startBT.setEnabled(false);
- autoTuneCB.setEnabled(false);
- saveBT.setEnabled(false);
exitBT.setEnabled(false);
- if (autoTune) {
- dcAddBT.setEnabled(false);
- dcSubBT.setEnabled(false);
- angleAddBT.setEnabled(false);
- angleSubBT.setEnabled(false);
- }
stopBT.setEnabled(true);
}
break;
@@ -402,9 +309,7 @@ public void onReceive(Context context, Intent intent) {
status = TestStatus.stopped;
resetTimer();
startBT.setEnabled(true);
- saveBT.setEnabled(true);
exitBT.setEnabled(true);
- autoTuneCB.setEnabled(true);
dcAddBT.setEnabled(true);
dcSubBT.setEnabled(true);
angleAddBT.setEnabled(true);
@@ -427,47 +332,12 @@ public void onReceive(Context context, Intent intent) {
+ (((data[12] & 255) << 8) + (data[11] & 255));
avg.add(value);
erpsTV.setText(String.format(Locale.getDefault(), "%.2f", 250000D/avg.getAverage()));
- if (autoTune && (avg.getIndex() == 0))
- stepDone();
}
}
}
}
};
- private static class RollingAverage {
-
- private final int size;
- private long total = 0;
- private int index = 0;
- private final int[] samples;
- private boolean rollover = false;
-
- public RollingAverage(int size) {
- this.size = size;
- samples = new int[size];
- for (int i = 0; i < size; i++) samples[i] = 0;
- }
-
- public void add(int x) {
- total -= samples[index];
- samples[index] = x;
- total += x;
- if (++index == size) {
- index = 0; // cheaper than modulus
- rollover = true;
- }
- }
-
- public double getAverage() {
- return rollover ?(double)total/(double)size:(double)total/(double)index;
- }
-
- public int getIndex() {
- return index;
- }
- }
-
private Timer timer;
private class TimerExpired extends TimerTask {
@Override
@@ -480,7 +350,7 @@ private void scheduleTimer() {
resetTimer();
TimerExpired timerExpired = new TimerExpired();
timer = new Timer();
- timer.schedule(timerExpired, 1500);
+ timer.schedule(timerExpired, 1000);
}
private void resetTimer() {
diff --git a/app/src/main/res/layout/activity_hal_sensor_calibration.xml b/app/src/main/res/layout/activity_hal_sensor_calibration.xml
index 337130e..f27b5ca 100644
--- a/app/src/main/res/layout/activity_hal_sensor_calibration.xml
+++ b/app/src/main/res/layout/activity_hal_sensor_calibration.xml
@@ -427,7 +427,7 @@
android:text="@string/start_stop" />
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+ android:layout_height="2dp"
+ android:background="?android:attr/listDivider" />
-
+ android:text="@string/hall_calib" />
+ android:text="@string/motorTuning" />
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index ec22239..0511d38 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -232,14 +232,12 @@
T Offset
R2
+ Dont\'change these values unless you know well what are you doing!
Phase Angle shift
- Hall Tfall Delay
- Hall Trise-Tfall
- Auto Tune
- D.C. step
- D.C. Max
- Angle (+/-)
- Step Samples
+ Circle/256
+ Hall Counter Offset
+ States 1 2 4 (falling edge)
+ States 3 5 6 (rising edge)
Cannot start motor test
Cannot stop motor test
Motor is running\nStop test then exit