Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. #pragma config(Sensor, S1, LeftLight, sensorLightActive)
  2. #pragma config(Sensor, S4, RightColor, sensorEV3_Color)
  3. #pragma config(Motor, motorB, leftMotor, tmotorEV3_Large, PIDControl, encoder)
  4. #pragma config(Motor, motorC, rightMotor, tmotorEV3_Large, PIDControl, encoder)
  5. //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
  6.  
  7. int colorSensorCalibrationBlack;
  8. int colorSensorCalibrationWhite;
  9. int lightSensorCalibrationBlack;
  10. int lightSensorCalibrationWhite;
  11. int colorRight;
  12. int lightLeft;
  13. /*
  14. Colour sensor modes
  15. 0 - modeEV3Color_Reflected
  16. 1 - modeEV3Color_Ambient
  17. 2 - modeEV3Color_Color
  18. 3 - modeEV3Color_Reflected_Raw
  19. 4 - modeEV3Color_RGB_Raw
  20. 5 - modeEV3Color_Calibration - Not utilized
  21. */
  22. int sum_array(int *a, int num_elements){
  23. int i, sum=0;
  24. for (i=0; i<num_elements; i++)
  25. {
  26. sum = sum + a[i];
  27. }
  28. return(sum/num_elements);
  29. }
  30.  
  31. void calibrate() {
  32. int first[26];
  33. int second[26];
  34. int i;
  35. writeDebugStream("Here2");
  36. while(!getButtonPress(buttonAny)==1){}
  37. writeDebugStream("Here3");
  38.  
  39. for (i = 0; i < 13 ;i+=2) {
  40. setMotorSpeed(leftMotor, 25);
  41. setMotorSpeed(rightMotor, -25);
  42. sleep(100);
  43. first[i] = SensorValue[RightColor];
  44. second[i+1] = SensorValue[LeftLight];
  45. sleep(20);
  46.  
  47.  
  48. setMotorSpeed(leftMotor, -25);
  49. setMotorSpeed(rightMotor, 25);
  50. sleep(100);
  51. first[i] = SensorValue[RightColor];
  52. second[i+1] = SensorValue[LeftLight];
  53. sleep(20);
  54. }
  55. setMotorSpeed(leftMotor,0);
  56. setMotorSpeed(rightMotor,0);
  57. colorSensorCalibrationBlack = sum_array(first,26);
  58. lightSensorCalibrationBlack = sum_array(second,26);
  59. while(!getButtonPress(buttonAny)==1){}
  60. for (i = 0; i < 13 ;i+=2) {
  61. setMotorSpeed(leftMotor, 25);
  62. setMotorSpeed(rightMotor, -25);
  63. sleep(100);
  64. first[i] = SensorValue[RightColor];
  65. second[i+1] = SensorValue[LeftLight];
  66. sleep(20);
  67.  
  68.  
  69.  
  70. setMotorSpeed(leftMotor, -25);
  71. setMotorSpeed(rightMotor, 25);
  72. sleep(100);
  73. first[i] = SensorValue[RightColor];
  74. second[i+1] = SensorValue[LeftLight];
  75. sleep(20);
  76. }
  77. colorSensorCalibrationWhite = sum_array(first,26);
  78. lightSensorCalibrationWhite = sum_array(second,26);
  79. setMotorSpeed(leftMotor,0);
  80. setMotorSpeed(rightMotor,0);
  81.  
  82. }
  83.  
  84. task main()
  85. {
  86.  
  87. calibrate();
  88. colorRight = abs(colorSensorCalibrationBlack-colorSensorCalibrationWhite)/4;
  89. lightLeft = abs( lightSensorCalibrationBlack- lightSensorCalibrationWhite)/4;
  90. while (true){
  91. writeDebugStream("%d,%d/n",abs(SensorValue[LeftLight] - lightSensorCalibrationWhite)<lightLeft,abs(SensorValue[RightColor]-colorSensorCalibrationBlack)<colorRight);
  92. if(abs(SensorValue[LeftLight] - lightSensorCalibrationWhite)<lightLeft && abs(SensorValue[RightColor]-colorSensorCalibrationBlack)<colorRight){
  93. setMotorSpeed(leftMotor, 10);
  94. setMotorSpeed(rightMotor, 5);
  95.  
  96. }else if(abs(SensorValue[LeftLight] -lightSensorCalibrationBlack)<lightLeft && abs(SensorValue[RightColor]-colorSensorCalibrationBlack)<colorRight){
  97. setMotorSpeed(leftMotor, 5);
  98. setMotorSpeed(rightMotor, 10);
  99.  
  100.  
  101. }else {
  102. setMotorSpeed(leftMotor, 10);
  103. setMotorSpeed(rightMotor, 10);
  104. }
  105. sleep(100);
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement