New way to send and read commands to avoid lags. Only 2 speed level for every motor

This commit is contained in:
estevez 2017-12-25 21:27:27 +02:00
parent 67c6a84399
commit ac8866ff73
2 changed files with 68 additions and 45 deletions

View File

@ -15,40 +15,60 @@ const int offsetB = -1;
SoftwareSerial BTSerial(3, 2); // RX | TX SoftwareSerial BTSerial(3, 2); // RX | TX
String reader; String reader;
int d = 600;
Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY); Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY);
Motor motor2 = Motor(BIN1, BIN2, PWMB, offsetB, STBY); Motor motor2 = Motor(BIN1, BIN2, PWMB, offsetB, STBY);
void setup() void setup()
{ {
Serial.begin(9600); //Serial.begin(9600);
Serial.println("Ready:"); //Serial.println("Ready:");
BTSerial.begin(9600); // HC-05 default speed in AT command more BTSerial.begin(9600); // HC-05 default speed in AT command more
brake(motor1, motor2); brake(motor1, motor2);
} }
void loop() void loop()
{ {
reader=""; char m1 = '9';
while (BTSerial.available()) { char m2 = '9';
delay(3); //Serial.println("There is "+String(BTSerial.available())+" available");
if (BTSerial.available()>0) { if (BTSerial.available()==2) {
char c = BTSerial.read(); m1 = BTSerial.read();
reader += c; m2 = BTSerial.read();
//Serial.println("Receved: "+String(m1)+";"+String(m2));
} switch (m1) {
} case '0':
if (reader.length() > 0) { motor1.drive(-255);
Serial.println("Receved: "+reader+""); break;
d = reader.toInt(); case '1':
} motor1.drive(-170);
break;
if (d <= 500) { //0 - full back, 255 - stop, 500 - full forward case '2':
int v1 = d - 255; motor1.drive(0);
motor1.drive(v1); break;
} else if (d >= 1000) { // 1000 - full back, 1255 - stop, 1500 - full forward case '3':
int v2 = d - 1255; motor1.drive(170);
motor2.drive(v2); break;
case '4':
motor1.drive(255);
break;
}
switch (m2) {
case '0':
motor2.drive(-255);
break;
case '1':
motor2.drive(-170);
break;
case '2':
motor2.drive(0);
break;
case '3':
motor2.drive(170);
break;
case '4':
motor2.drive(255);
break;
}
} }
} }

View File

@ -28,6 +28,8 @@ public class MainActivity extends AppCompatActivity {
SeekBar motorB; SeekBar motorB;
TextView txtA; TextView txtA;
TextView txtB; TextView txtB;
int motorACommand = 2;
int motorBCommand = 2;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -37,11 +39,11 @@ public class MainActivity extends AppCompatActivity {
btnConnect = (Button)findViewById(R.id.btnConnect); btnConnect = (Button)findViewById(R.id.btnConnect);
motorA = (SeekBar)findViewById(R.id.motorA); motorA = (SeekBar)findViewById(R.id.motorA);
motorA.setProgress(3); motorA.setProgress(2);
motorA.setMax(6); motorA.setMax(4);
motorB = (SeekBar)findViewById(R.id.motorB); motorB = (SeekBar)findViewById(R.id.motorB);
motorB.setProgress(3); motorB.setProgress(2);
motorB.setMax(6); motorB.setMax(4);
txtA = (TextView)findViewById(R.id.txtA); txtA = (TextView)findViewById(R.id.txtA);
txtB = (TextView)findViewById(R.id.txtB); txtB = (TextView)findViewById(R.id.txtB);
@ -52,14 +54,8 @@ public class MainActivity extends AppCompatActivity {
@Override @Override
public void onProgressChanged(SeekBar seekBar, public void onProgressChanged(SeekBar seekBar,
int progresValue, boolean fromUser) { int progresValue, boolean fromUser) {
progress = Integer.toString(progresValue*85+1000); motorBCommand = progresValue;
txtA.setText(progress); sendCommand();
try {
if (btOutStream != null)
btOutStream.write(progress.getBytes());
} catch (IOException e) {
//e.printStackTrace();
}
} }
@Override @Override
@ -71,7 +67,7 @@ public class MainActivity extends AppCompatActivity {
@Override @Override
public void onStopTrackingTouch(SeekBar seekBar) { public void onStopTrackingTouch(SeekBar seekBar) {
seekBar.setProgress(3); seekBar.setProgress(2);
} }
}); });
@ -82,14 +78,8 @@ public class MainActivity extends AppCompatActivity {
@Override @Override
public void onProgressChanged(SeekBar seekBar, public void onProgressChanged(SeekBar seekBar,
int progresValue, boolean fromUser) { int progresValue, boolean fromUser) {
progress = Integer.toString(progresValue*85); motorACommand = progresValue;
txtB.setText(progress); sendCommand();
try {
if (btOutStream != null)
btOutStream.write(progress.getBytes());
} catch (IOException e) {
//e.printStackTrace();
}
} }
@Override @Override
@ -101,7 +91,7 @@ public class MainActivity extends AppCompatActivity {
@Override @Override
public void onStopTrackingTouch(SeekBar seekBar) { public void onStopTrackingTouch(SeekBar seekBar) {
seekBar.setProgress(3); seekBar.setProgress(2);
} }
}); });
@ -161,4 +151,17 @@ public class MainActivity extends AppCompatActivity {
} }
}); });
} }
private void sendCommand() {
String m1 = Integer.toString(motorACommand);
String m2 = Integer.toString(motorBCommand);
txtA.setText(m1);
txtB.setText(m2);
try {
if (btOutStream != null)
btOutStream.write((m1+m2).getBytes());
} catch (IOException e) {
//e.printStackTrace();
}
}
} }