|
1 | | -packageev3dev.sensors; |
2 | | - |
3 | | -importev3dev.hardware.EV3DevDevice; |
4 | | -importev3dev.hardware.EV3DevFileSystem; |
5 | | -importev3dev.hardware.EV3DevPlatform; |
6 | | -importev3dev.utils.Sysfs; |
7 | | -importlejos.hardware.Power; |
8 | | -importorg.slf4j.Logger; |
9 | | -importorg.slf4j.LoggerFactory; |
10 | | - |
11 | | -/** |
12 | | - * The class Battery interacts with EV3Dev to get information about battery used. |
13 | | - * |
14 | | - * @see <a href="https://www.kernel.org/doc/Documentation/power/power_supply_class.txt">https://www.kernel.org/doc/Documentation/power/power_supply_class.txt</a> |
15 | | - * @see <a href="https://github.com/ev3dev/ev3dev-lang/blob/develop/wrapper-specification.md#direct-attribute-mappings-5">https://github.com/ev3dev/ev3dev-lang/blob/develop/wrapper-specification.md#direct-attribute-mappings-5</a> |
16 | | - * |
17 | | - * @author Juan Antonio Breña Moral |
18 | | - * |
19 | | - */ |
20 | | -publicclassBatteryextendsEV3DevDeviceimplementsPower { |
21 | | - |
22 | | -privatestaticfinalLoggerLOGGER =LoggerFactory.getLogger(Battery.class); |
23 | | - |
24 | | -privatefinalStringBATTERY; |
25 | | -privatefinalStringBATTERY_EV3; |
26 | | -privatefinalStringBATTERY_PISTORMS; |
27 | | -privatefinalStringBATTERY_BRICKPI; |
28 | | -privatefinalStringBATTERY_BRICKPI3; |
29 | | - |
30 | | -privateStringBATTERY_PATH; |
31 | | -privatefinalStringVOLTAGE ="voltage_now"; |
32 | | -privatefinalStringCURRENT ="current_now"; |
33 | | - |
34 | | -privateStringBATTERY_PATH_LOCAL =""; |
35 | | - |
36 | | -privatestaticBatteryinstance; |
37 | | - |
38 | | -publicstaticBatterygetInstance() { |
39 | | -if (instance ==null) { |
40 | | -instance =newBattery(); |
41 | | - } |
42 | | -returninstance; |
43 | | - } |
44 | | - |
45 | | -// Prevent duplicate objects |
46 | | -privateBattery() { |
47 | | - |
48 | | -LOGGER.debug("Init sensor"); |
49 | | - |
50 | | -BATTERY =ev3DevProperties.getProperty("battery"); |
51 | | -BATTERY_EV3 =ev3DevProperties.getProperty("ev3.battery"); |
52 | | -BATTERY_PISTORMS =ev3DevProperties.getProperty("pistorms.battery"); |
53 | | -BATTERY_BRICKPI =ev3DevProperties.getProperty("brickpi.battery"); |
54 | | -BATTERY_BRICKPI3 =ev3DevProperties.getProperty("brickpi3.battery"); |
55 | | - |
56 | | -//TODO Create separator variable for the whole project |
57 | | -BATTERY_PATH =EV3DevFileSystem.getRootPath() +"/" +BATTERY; |
58 | | -if(CURRENT_PLATFORM.equals(EV3DevPlatform.EV3BRICK)) { |
59 | | -BATTERY_PATH_LOCAL +=BATTERY_PATH +"/" +BATTERY_EV3; |
60 | | - }elseif(CURRENT_PLATFORM.equals(EV3DevPlatform.PISTORMS)) { |
61 | | -BATTERY_PATH_LOCAL +=BATTERY_PATH +"/" +BATTERY_PISTORMS; |
62 | | - }elseif(CURRENT_PLATFORM.equals(EV3DevPlatform.BRICKPI)) { |
63 | | -BATTERY_PATH_LOCAL +=BATTERY_PATH +"/" +BATTERY_BRICKPI; |
64 | | - }elseif(CURRENT_PLATFORM.equals(EV3DevPlatform.BRICKPI3)) { |
65 | | -BATTERY_PATH_LOCAL +=BATTERY_PATH +"/" +BATTERY_BRICKPI3; |
66 | | - } |
67 | | - } |
68 | | - |
69 | | -@Override |
70 | | -publicintgetVoltageMilliVolt() { |
71 | | -return (int)Sysfs.readFloat(BATTERY_PATH_LOCAL +"/" +VOLTAGE) /1000; |
72 | | - } |
73 | | - |
74 | | -/** |
75 | | - * Returns voltage of the battery in microvolts. |
76 | | - * @return voltage |
77 | | - */ |
78 | | -publicfloatgetVoltage() { |
79 | | -returnSysfs.readFloat(BATTERY_PATH_LOCAL +"/" +VOLTAGE) /1000000; |
80 | | -} |
81 | | - |
82 | | -//TODO Review output |
83 | | -//TODO Review units |
84 | | -/** |
85 | | - * Returns the current of the battery in amps. |
86 | | - * @return current |
87 | | - */ |
88 | | -publicfloatgetBatteryCurrent() { |
89 | | -if (CURRENT_PLATFORM.equals(EV3DevPlatform.EV3BRICK)){ |
90 | | -returnSysfs.readFloat(BATTERY_PATH +"/" +BATTERY_EV3 +"/" +CURRENT); |
91 | | - }else { |
92 | | -LOGGER.warn("This method is not available for {} & {}",EV3DevPlatform.PISTORMS,EV3DevPlatform.BRICKPI); |
93 | | -return -1f; |
94 | | - } |
95 | | -} |
96 | | - |
97 | | -//TODO Review this method in the future. |
98 | | -@Override |
99 | | -publicfloatgetMotorCurrent() { |
100 | | -thrownewUnsupportedOperationException("This feature is not implemented"); |
101 | | - } |
102 | | - |
103 | | -} |
| 1 | +packageev3dev.sensors; |
| 2 | + |
| 3 | +importev3dev.hardware.EV3DevDevice; |
| 4 | +importev3dev.hardware.EV3DevFileSystem; |
| 5 | +importev3dev.hardware.EV3DevPlatform; |
| 6 | +importev3dev.utils.Sysfs; |
| 7 | +importlejos.hardware.Power; |
| 8 | +importorg.slf4j.Logger; |
| 9 | +importorg.slf4j.LoggerFactory; |
| 10 | + |
| 11 | +/** |
| 12 | + * The class Battery interacts with EV3Dev to get information about battery used. |
| 13 | + * |
| 14 | + * @author Juan Antonio Breña Moral |
| 15 | + * @see <a href="https://www.kernel.org/doc/Documentation/power/power_supply_class.txt">https://www.kernel.org/doc/Documentation/power/power_supply_class.txt</a> |
| 16 | + * @see <a href="https://github.com/ev3dev/ev3dev-lang/blob/develop/wrapper-specification.md#direct-attribute-mappings-5">https://github.com/ev3dev/ev3dev-lang/blob/develop/wrapper-specification.md#direct-attribute-mappings-5</a> |
| 17 | + */ |
| 18 | +publicclassBatteryextendsEV3DevDeviceimplementsPower { |
| 19 | + |
| 20 | +privatestaticfinalLoggerLOGGER =LoggerFactory.getLogger(Battery.class); |
| 21 | + |
| 22 | +privatefinalStringBATTERY; |
| 23 | +privatefinalStringBATTERY_EV3; |
| 24 | +privatefinalStringBATTERY_PISTORMS; |
| 25 | +privatefinalStringBATTERY_BRICKPI; |
| 26 | +privatefinalStringBATTERY_BRICKPI3; |
| 27 | + |
| 28 | +privateStringBATTERY_PATH; |
| 29 | +privatefinalStringVOLTAGE ="voltage_now"; |
| 30 | +privatefinalStringCURRENT ="current_now"; |
| 31 | + |
| 32 | +privateStringBATTERY_PATH_LOCAL =""; |
| 33 | + |
| 34 | +privatestaticBatteryinstance; |
| 35 | + |
| 36 | +/** |
| 37 | + * Get a singleton Battery object |
| 38 | + * |
| 39 | + * @return Battery |
| 40 | + */ |
| 41 | +publicstaticBatterygetInstance() { |
| 42 | +//TODO Refactor |
| 43 | +if (instance ==null) { |
| 44 | +instance =newBattery(); |
| 45 | + } |
| 46 | +returninstance; |
| 47 | + } |
| 48 | + |
| 49 | +// Prevent duplicate objects |
| 50 | +privateBattery() { |
| 51 | + |
| 52 | +LOGGER.debug("Init sensor"); |
| 53 | + |
| 54 | +BATTERY =ev3DevProperties.getProperty("battery"); |
| 55 | +BATTERY_EV3 =ev3DevProperties.getProperty("ev3.battery"); |
| 56 | +BATTERY_PISTORMS =ev3DevProperties.getProperty("pistorms.battery"); |
| 57 | +BATTERY_BRICKPI =ev3DevProperties.getProperty("brickpi.battery"); |
| 58 | +BATTERY_BRICKPI3 =ev3DevProperties.getProperty("brickpi3.battery"); |
| 59 | + |
| 60 | +//TODO Create separator variable for the whole project |
| 61 | +BATTERY_PATH =EV3DevFileSystem.getRootPath() +"/" +BATTERY; |
| 62 | +if (CURRENT_PLATFORM.equals(EV3DevPlatform.EV3BRICK)) { |
| 63 | +BATTERY_PATH_LOCAL +=BATTERY_PATH +"/" +BATTERY_EV3; |
| 64 | + }elseif (CURRENT_PLATFORM.equals(EV3DevPlatform.PISTORMS)) { |
| 65 | +BATTERY_PATH_LOCAL +=BATTERY_PATH +"/" +BATTERY_PISTORMS; |
| 66 | + }elseif (CURRENT_PLATFORM.equals(EV3DevPlatform.BRICKPI)) { |
| 67 | +BATTERY_PATH_LOCAL +=BATTERY_PATH +"/" +BATTERY_BRICKPI; |
| 68 | + }elseif (CURRENT_PLATFORM.equals(EV3DevPlatform.BRICKPI3)) { |
| 69 | +BATTERY_PATH_LOCAL +=BATTERY_PATH +"/" +BATTERY_BRICKPI3; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | +@Override |
| 74 | +publicintgetVoltageMilliVolt() { |
| 75 | +return (int)Sysfs.readFloat(BATTERY_PATH_LOCAL +"/" +VOLTAGE) /1000; |
| 76 | + } |
| 77 | + |
| 78 | +/** |
| 79 | + * Returns voltage of the battery in microvolts. |
| 80 | + * |
| 81 | + * @return voltage |
| 82 | + */ |
| 83 | +publicfloatgetVoltage() { |
| 84 | +returnSysfs.readFloat(BATTERY_PATH_LOCAL +"/" +VOLTAGE) /1000000; |
| 85 | + } |
| 86 | + |
| 87 | +//TODO Review output |
| 88 | +//TODO Review units |
| 89 | + |
| 90 | +/** |
| 91 | + * Returns the current of the battery in amps. |
| 92 | + * |
| 93 | + * @return current |
| 94 | + */ |
| 95 | +publicfloatgetBatteryCurrent() { |
| 96 | +if (CURRENT_PLATFORM.equals(EV3DevPlatform.EV3BRICK)) { |
| 97 | +returnSysfs.readFloat(BATTERY_PATH +"/" +BATTERY_EV3 +"/" +CURRENT); |
| 98 | + }else { |
| 99 | +LOGGER.warn("This method is not available for {} & {}",EV3DevPlatform.PISTORMS,EV3DevPlatform.BRICKPI); |
| 100 | +return -1f; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | +//TODO Review this method in the future. |
| 105 | +@Override |
| 106 | +publicfloatgetMotorCurrent() { |
| 107 | +thrownewUnsupportedOperationException("This feature is not implemented"); |
| 108 | + } |
| 109 | + |
| 110 | +} |