Skip to content

Commit

Permalink
WIP - Motor optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
mspider65 committed Feb 7, 2021
1 parent 03e5786 commit 883213d
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 385 deletions.
13 changes: 13 additions & 0 deletions app/src/main/java/spider65/ebike/tsdz2_esp32/TSDZConst.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand All @@ -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();


Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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:
Expand Down
Loading

0 comments on commit 883213d

Please sign in to comment.