Advertisement
Guest User

Untitled

a guest
Apr 21st, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. package week7;
  2.  
  3. import lejos.nxt.BasicMotorPort;
  4. import lejos.nxt.Button;
  5. import lejos.nxt.LightSensor;
  6. import lejos.nxt.MotorPort;
  7. import lejos.nxt.SensorPort;
  8.  
  9. public class vehicle2 {
  10.  
  11. private static int max = 1023;
  12. private static LightSensor ls1 = new LightSensor(SensorPort.S2);
  13. private static LightSensor ls2 = new LightSensor(SensorPort.S3);
  14.  
  15. private static boolean ab = true; // true = a, false = b;
  16.  
  17. public static void main(String [] args) throws Exception
  18. {
  19.  
  20. float maxLight = 10000;
  21. float minLight = 0;
  22. float alpha = 0.01F;
  23.  
  24. while(!Button.ESCAPE.isDown()) {
  25.  
  26. /*float lightValue1 = ls1.readValue();
  27. float lightValue2 = ls2.readValue();*/
  28.  
  29. // Inhibitory
  30. /*lightValue1 = 100-lightValue1;
  31. lightValue2 = 100-lightValue2;*/
  32.  
  33. float lightValue1 = ls1.readNormalizedValue();
  34. float lightValue2 = ls2.readNormalizedValue();
  35.  
  36. if(lightValue1 < maxLight*2) {
  37. maxLight = (maxLight * (1-alpha)) + lightValue1*alpha;
  38. }
  39. if(lightValue2 < maxLight*2) {
  40. maxLight = (maxLight * (1-alpha)) + lightValue2*alpha;
  41. }
  42. if(lightValue1 > minLight/2) {
  43. minLight = (minLight * (1-alpha)) + lightValue1*alpha;
  44. }
  45. if(lightValue2 > minLight/2) {
  46. minLight = (minLight * (1-alpha)) + lightValue2*alpha;
  47. }
  48.  
  49. lightValue1 = normalize(lightValue1, maxLight, minLight);
  50. lightValue2 = normalize(lightValue2, maxLight, minLight);
  51.  
  52. // Inhibitor
  53. /*lightValue1 = 100 - lightValue1;
  54. lightValue2 = 100 - lightValue2;*/
  55.  
  56.  
  57. if(ab) {
  58. MotorPort.A.controlMotor((int) lightValue1, BasicMotorPort.FORWARD);
  59. MotorPort.C.controlMotor((int) lightValue2, BasicMotorPort.FORWARD);
  60. }
  61. else {
  62. MotorPort.A.controlMotor((int) lightValue2, BasicMotorPort.FORWARD);
  63. MotorPort.C.controlMotor((int) lightValue1, BasicMotorPort.FORWARD);
  64. }
  65.  
  66. }
  67. }
  68.  
  69. public static float normalize(float lightValue1, float maxLight, float minLight) {
  70.  
  71. float output = 100 - ((lightValue1 - maxLight) * 100) / (minLight - maxLight);
  72. if(output > 100) {
  73. output = 100;
  74. }
  75. if(output < 0) {
  76. output = 0;
  77. }
  78. return output;
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement