|
| 1 | +import java.sql.Time; |
| 2 | +import java.text.SimpleDateFormat; |
| 3 | +import java.util.Calendar; |
| 4 | +import java.util.Timer; |
| 5 | +import java.util.TimerTask; |
| 6 | +import lejos.hardware.lcd.LCD; |
| 7 | +import lejos.hardware.motor.EV3LargeRegulatedMotor; |
| 8 | +import lejos.hardware.port.MotorPort; |
| 9 | +import lejos.hardware.port.Port; |
| 10 | +import lejos.hardware.port.SensorPort; |
| 11 | +import lejos.hardware.sensor.EV3ColorSensor; |
| 12 | +import lejos.robotics.SampleProvider; |
| 13 | +import lejos.utility.Delay; |
| 14 | + |
| 15 | + |
| 16 | +public class Robot2Roues { |
| 17 | + |
| 18 | + private EV3LargeRegulatedMotor roueGauche; |
| 19 | + private EV3LargeRegulatedMotor roueDroite; |
| 20 | + private static double Lumiere; |
| 21 | + private static double Correction; |
| 22 | + private static double VitesseB; |
| 23 | + private static double VitesseC; |
| 24 | + private static double maxSpeed; |
| 25 | + |
| 26 | + /** |
| 27 | + * |
| 28 | + * @param portRoueGauche |
| 29 | + * MotorPort.(Lettre du port) |
| 30 | + * @param portRoueDroite |
| 31 | + * MotorPort.(Lettre du port) |
| 32 | + */ |
| 33 | + public Robot2Roues(Port portRoueGauche, Port portRoueDroite) { |
| 34 | + roueGauche = new EV3LargeRegulatedMotor(portRoueGauche); |
| 35 | + roueDroite = new EV3LargeRegulatedMotor(portRoueDroite); |
| 36 | + } |
| 37 | + |
| 38 | + /* |
| 39 | + * public static EV3LargeRegulatedMotor MotorA = new |
| 40 | + * EV3LargeRegulatedMotor(MotorPort.A); public static EV3LargeRegulatedMotor |
| 41 | + * roueDroite = new EV3LargeRegulatedMotor(MotorPort.B); public static |
| 42 | + * EV3LargeRegulatedMotor roueGauche = new |
| 43 | + * EV3LargeRegulatedMotor(MotorPort.C); public static EV3LargeRegulatedMotor |
| 44 | + * MotorD = new EV3LargeRegulatedMotor(MotorPort.D); |
| 45 | + */ |
| 46 | + |
| 47 | + public int NumberClarck; |
| 48 | + |
| 49 | + public void rotateWaitAColour(int color, int time, int vitesse) { |
| 50 | + |
| 51 | + EV3LargeRegulatedMotor[] syncList = new EV3LargeRegulatedMotor[1]; |
| 52 | + syncList[0] = roueDroite; |
| 53 | + |
| 54 | + roueGauche.synchronizeWith(syncList); |
| 55 | + |
| 56 | + roueGauche.setSpeed(1000); |
| 57 | + roueDroite.setSpeed(1000); |
| 58 | + roueGauche.startSynchronization(); |
| 59 | + roueDroite.forward(); |
| 60 | + roueGauche.forward(); |
| 61 | + roueGauche.endSynchronization(); |
| 62 | + |
| 63 | + Delay.msDelay(1000); |
| 64 | + |
| 65 | + roueGauche.startSynchronization(); |
| 66 | + roueDroite.stop(); |
| 67 | + roueGauche.stop(); |
| 68 | + roueGauche.endSynchronization(); |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + public void getRedAndDrawInTheBick() { |
| 73 | + SampleProvider ultra = new EV3ColorSensor(SensorPort.S3).getRedMode(); |
| 74 | + float[] sample = new float[ultra.sampleSize()]; |
| 75 | + while (true) { |
| 76 | + |
| 77 | + ultra.fetchSample(sample, 0); |
| 78 | + LCD.drawString(Double.toString(sample[0]), 1, 1); |
| 79 | + Delay.msDelay(100); |
| 80 | + LCD.clear(); |
| 81 | + |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public void followTheLineForEver() { |
| 86 | + SampleProvider color = new EV3ColorSensor(SensorPort.S4).getRedMode(); |
| 87 | + float[] sample = new float[color.sampleSize()]; |
| 88 | + while (true) { |
| 89 | + |
| 90 | + color.fetchSample(sample, 0); |
| 91 | + Lumiere = sample[0]; |
| 92 | + |
| 93 | + Correction = (Lumiere - 0.325) * 0.4; |
| 94 | + VitesseC = ((Correction * -1) + 0.4) * roueDroite.getMaxSpeed(); |
| 95 | + VitesseB = ((Correction * 1) + 0.4) * roueGauche.getMaxSpeed(); |
| 96 | + |
| 97 | + roueDroite.setSpeed((float) VitesseC); |
| 98 | + LCD.drawString(Double.toString(VitesseB), 1, 1); |
| 99 | + LCD.drawString(Double.toString(VitesseC), 1, 2); |
| 100 | + LCD.drawString(Double.toString(Lumiere), 1, 3); |
| 101 | + |
| 102 | + roueGauche.setSpeed((float) VitesseB); |
| 103 | + Delay.msDelay(10); |
| 104 | + roueDroite.forward(); |
| 105 | + roueGauche.forward(); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + public void followTheLineWait(Port capteurCouleur, double moyenCouleur, int timerMs) { |
| 110 | + SampleProvider color = new EV3ColorSensor(capteurCouleur).getRedMode(); |
| 111 | + float[] sample = new float[color.sampleSize()]; |
| 112 | + Calendar cal = Calendar.getInstance(); |
| 113 | + SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); |
| 114 | + while(cal.compareTo(Calendar.getInstance())*-1>= timerMs){ |
| 115 | + |
| 116 | + LCD.drawString( sdf.format(cal.getTime()), 1, 1); |
| 117 | + Delay.msDelay(1000); |
| 118 | + color.fetchSample(sample, 0); |
| 119 | + Lumiere = sample[0]; |
| 120 | + |
| 121 | + Correction = (Lumiere - moyenCouleur) * 0.4; |
| 122 | + VitesseC = ((Correction * -1) + 0.4) * roueDroite.getMaxSpeed(); |
| 123 | + VitesseB = ((Correction * 1) + 0.4) * roueGauche.getMaxSpeed(); |
| 124 | + |
| 125 | + roueDroite.setSpeed((float) VitesseC); |
| 126 | + LCD.drawString(Double.toString(VitesseB), 1, 1); |
| 127 | + LCD.drawString(Double.toString(VitesseC), 1, 2); |
| 128 | + LCD.drawString(Double.toString(Lumiere), 1, 3); |
| 129 | + |
| 130 | + roueGauche.setSpeed((float) VitesseB); |
| 131 | + Delay.msDelay(10); |
| 132 | + roueDroite.forward(); |
| 133 | + roueGauche.forward(); |
| 134 | + |
| 135 | + |
| 136 | + } |
| 137 | + /* |
| 138 | + |
| 139 | + |
| 140 | + while (true) { |
| 141 | +
|
| 142 | + color.fetchSample(sample, 0); |
| 143 | + Lumiere = sample[0]; |
| 144 | +
|
| 145 | + Correction = (Lumiere - 0.325) * 0.4; |
| 146 | + VitesseC = ((Correction * -1) + 0.4) * roueDroite.getMaxSpeed(); |
| 147 | + VitesseB = ((Correction * 1) + 0.4) * roueGauche.getMaxSpeed(); |
| 148 | +
|
| 149 | + roueDroite.setSpeed((float) VitesseC); |
| 150 | + LCD.drawString(Double.toString(VitesseB), 1, 1); |
| 151 | + LCD.drawString(Double.toString(VitesseC), 1, 2); |
| 152 | + LCD.drawString(Double.toString(Lumiere), 1, 3); |
| 153 | +
|
| 154 | + roueGauche.setSpeed((float) VitesseB); |
| 155 | + Delay.msDelay(10); |
| 156 | + roueDroite.forward(); |
| 157 | + roueGauche.forward(); |
| 158 | + } |
| 159 | + */ |
| 160 | + } |
| 161 | + |
| 162 | +} |
0 commit comments