Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. package org.firstinspires.ftc.teamcode.devices;
  2.  
  3. import android.util.Log;
  4.  
  5. import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
  6. import com.qualcomm.robotcore.eventloop.opmode.OpMode;
  7. import com.qualcomm.robotcore.hardware.HardwareMap;
  8. import com.qualcomm.robotcore.hardware.I2cAddr;
  9. import com.qualcomm.robotcore.hardware.I2cDevice;
  10. import com.qualcomm.robotcore.hardware.I2cDeviceSynch;
  11. import com.qualcomm.robotcore.hardware.I2cDeviceSynchImpl;
  12.  
  13. /**
  14. * Created by Ben on 3/28/17.
  15. */
  16.  
  17. public class MultiplexedColorSensor {
  18. // Registers
  19. static final int ENABLE = 0x80;
  20. static final int ATIME = 0x81;
  21. static final int CONTROL = 0x8F;
  22. static final int ID = 0x92;
  23. static final int STATUS = 0x93;
  24. static final int CDATAL = 0x94;
  25.  
  26. // Default I2C address for multiplexer. The address can be changed to any
  27. // value from 0x70 to 0x77, so this line would need to be changed if a
  28. // non-default address is to be used.
  29. static final I2cAddr MUX_ADDRESS = new I2cAddr(0x70);
  30. private I2cDevice mux;
  31. private I2cDeviceSynch muxReader;
  32.  
  33. // Only one color sensor is needed in code as the multiplexer switches
  34. // between the physical sensors
  35. private byte[] adaCache;
  36. // I2C address for color sensor
  37. static final I2cAddr ADA_ADDRESS = new I2cAddr(0x29);
  38. private I2cDevice ada;
  39. private I2cDeviceSynch adaReader;
  40.  
  41. private HardwareMap hardwareMap;
  42. private int[] sensorPorts;
  43. private double miliseconds;
  44. private int gain;
  45. private String muxName;
  46. private String colorName;
  47.  
  48.  
  49. public static int GAIN_1X = 0x00;
  50. public static int GAIN_4X = 0x01;
  51. public static int GAIN_16X = 0x02;
  52. public static int GAIN_60X = 0x03;
  53.  
  54. /**
  55. * Initializes Adafruit color sensors on the specified ports of the I2C
  56. * multiplexer.
  57. *
  58. * @param hardwareMap hardwareMap from OpMode
  59. * @param muxName Configuration name of I2CDevice for multiplexer
  60. * @param colorName Configuration name of I2CDevice for color sensor
  61. * @param sensorPorts Out ports on multiplexer with color sensors attached
  62. * @param milliSeconds Integration time in milliseconds
  63. * @param gain Gain (GAIN_1X, GAIN_4X, GAIN_16X, GAIN_60X)
  64. */
  65. public MultiplexedColorSensor(HardwareMap hardwareMap, String muxName, String colorName, int[] sensorPorts, double milliSeconds, int gain) {
  66. this.hardwareMap = hardwareMap;
  67. this.muxName = muxName;
  68. this.colorName = colorName;
  69. this.sensorPorts = sensorPorts;
  70. this.miliseconds = milliSeconds;
  71. this.gain = gain;
  72. }
  73.  
  74. public void init(LinearOpMode linearOpMode) {
  75. Log.i("ColorInit", "Active: "+ linearOpMode.opModeIsActive() + " \tStopReq: " + linearOpMode.isStopRequested());
  76. mux = hardwareMap.i2cDevice.get(muxName);
  77. Log.i("ColorInit", "Active: "+ linearOpMode.opModeIsActive() + " \tStopReq: " + linearOpMode.isStopRequested());
  78. muxReader = new I2cDeviceSynchImpl(mux, MUX_ADDRESS, false);
  79. Log.i("ColorInit", "Active: "+ linearOpMode.opModeIsActive() + " \tStopReq: " + linearOpMode.isStopRequested());
  80. muxReader.engage();
  81. Log.i("ColorInit", "Active: "+ linearOpMode.opModeIsActive() + " \tStopReq: " + linearOpMode.isStopRequested());
  82.  
  83. ada = hardwareMap.i2cDevice.get(colorName);
  84.  
  85. Log.i("ColorInitLoop", "Active: "+ linearOpMode.opModeIsActive() + " \tStopReq: " + linearOpMode.isStopRequested());
  86. //muxReader.write8(0x0, 1 << sensorPorts[0], true); // Write to given output port on the multiplexer
  87. Log.i("ColorInitLoop", "Active: "+ linearOpMode.opModeIsActive() + " \tStopReq: " + linearOpMode.isStopRequested());
  88.  
  89. adaReader = new I2cDeviceSynchImpl(ada, ADA_ADDRESS, false);
  90. Log.i("ColorInitLoop", "Active: "+ linearOpMode.opModeIsActive() + " \tStopReq: " + linearOpMode.isStopRequested());
  91. adaReader.engage();
  92. Log.i("ColorInitLoop", "Active: "+ linearOpMode.opModeIsActive() + " \tStopReq: " + linearOpMode.isStopRequested());
  93.  
  94. final int time = integrationByte(miliseconds);
  95.  
  96. adaReader.write8(ENABLE, 0x03, true); // Power on and enable ADC
  97. adaReader.read8(ID); // Read device ID
  98. adaReader.write8(CONTROL, gain, true); // Set gain
  99. adaReader.write8(ATIME, time, true); // Set integration time
  100.  
  101. }
  102.  
  103. /**
  104. * Set the integration time on all the color sensors
  105. * @param milliSeconds Time in millseconds
  106. */
  107. public void setIntegrationTime(double milliSeconds) {
  108. int val = integrationByte(milliSeconds);
  109.  
  110. for (int i = 0; i < sensorPorts.length; i++) {
  111. muxReader.write8(0x0, 1 << sensorPorts[i], true);
  112. adaReader.write8(ATIME, val, true);
  113. }
  114. }
  115.  
  116. private int integrationByte(double milliSeconds) {
  117. int count = (int)(milliSeconds/2.4);
  118. if (count<1) count = 1; // Clamp the time range
  119. if (count>256) count = 256;
  120. return (256 - count);
  121. }
  122.  
  123.  
  124.  
  125. /**
  126. * Retrieve the color read by the given color sensor
  127. *
  128. * @param port Port on multiplexer of given color sensor
  129. * @return Array containing the Clear, Red, Green, and Blue color values
  130. */
  131. public int[] getCRGB(int port) {
  132. // Write to I2C port on the multiplexer
  133. muxReader.write8(0x0, 1 << port, true);
  134.  
  135. // Read color registers
  136. adaCache = adaReader.read(CDATAL, 8);
  137.  
  138. // Combine high and low bytes
  139. int[] crgb = new int[4];
  140. for (int i=0; i<4; i++) {
  141. crgb[i] = (adaCache[2*i] & 0xFF) + (adaCache[2*i+1] & 0xFF) * 256;
  142. }
  143. return crgb;
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement