Hardware Management, Metrics, PWM/GPIO support. (#12)

* [Server] Hardware Management

* [Server] Hardware Management

* [Server] Hardware Management

* [Server] Hardware Management

* [Server] Hardware Management

* [Server] Hardware Management

* Added metricsPublisher

* [Server] Hardware Management

* [Server] Hardware Management

* Fill in HardwareConfig, allow JSON Comments

* Use hardware config

* [Hardware Management] Use Hardware Config

* [Hardware Management] Use softPWM for dimming

* [Hardware] Added HardwareConfig Test

* [Hardware] Started HardwareManager

* Start metrics thread in hardwareManager

* [Hardware] Added Hardware Manager Test

* [Hardware] Spotless

* [Hardware] Added logging, cleaned up HardwareConfig

* [Hardware] Added logging to PWM class

* [Hardware] Rebase off master, fix merge conflicts

* [Hardware] Ignore metrics commands if on pi

* [Hardware] Remove GPIO provision after shutdown

* [Hardware] Switch over to diozero

* [Hardware] Use broadcom pins

* [Hardware] Fix PWM port

* [Hardware] Use jpi instead of pigpio

* [Hardware] Use dizero-core

* [Hardware] No need to close LED

* [Hardware] Switch to jpigpio

* [Hardware] Initalize JPiGPIO in unit tests

* [Hardware] Use dutyCycle for LED dimming

* [Hardware] Add blink test to HardwareManager

* [Hardware] Fix PWM port

* [Hardware] Fix HardwareManagerTest

* [Hardware] Fix HardwareManagerTest

* [Hardware] Use waves for LED blinking

* [Hardware] Make blinking part of PWM

* [Hardware] Add API methods to hardware Manager

* [Hardware] Only start pigpio if on pi

* [Hardware] Merge PWM classes into GPIO

* [Hardware] Add Hardware stuff to VisionModules

* [Hardware] Remove random semicolon

Co-authored-by: Banks Troutman <btrout.dhrs@gmail.com>
This commit is contained in:
Xzibit
2020-07-31 15:43:58 -04:00
committed by GitHub
parent 65964726a5
commit 0b98dc3c9f
24 changed files with 1096 additions and 33 deletions

View File

@@ -0,0 +1,52 @@
/*
* Copyright (C) 2020 Photon Vision.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.photonvision.hardware;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.photonvision.common.configuration.HardwareConfig;
import org.photonvision.common.hardware.GPIO.CustomGPIO;
import org.photonvision.common.util.TestUtils;
public class HardwareConfigTest {
@Test
public void loadJson() {
try {
System.out.println("Loading Hardware configs...");
var config =
new ObjectMapper().readValue(TestUtils.getHardwareConfigJson(), HardwareConfig.class);
assertEquals(config.deviceName, "PhotonVision");
assertEquals(config.deviceLogoPath, "photonvision.png");
assertEquals(config.supportURL, "https://support.photonvision.com");
Assertions.assertArrayEquals(
config.ledPins.stream().mapToInt(i -> i).toArray(), new int[] {2, 13});
Assertions.assertArrayEquals(
config.ledPWMRange.stream().mapToInt(i -> i).toArray(), new int[] {0, 100});
CustomGPIO.setConfig(config);
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright (C) 2020 Photon Vision.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.photonvision.hardware;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.photonvision.common.configuration.HardwareConfig;
import org.photonvision.common.hardware.HardwareManager;
import org.photonvision.common.util.TestUtils;
public class HardwareManagerTest {
@Test
public void ManagementTest() throws IOException {
var config =
new ObjectMapper().readValue(TestUtils.getHardwareConfigJson(), HardwareConfig.class);
HardwareManager.getInstance().setConfig(config);
var instance = HardwareManager.getInstance();
instance.getGPIO(13).setPwmRange(List.of(0, 100));
Assertions.assertEquals(instance.getGPIO(13).getPwmRange().get(0), 0);
Assertions.assertEquals(instance.getGPIO(13).getPwmRange().get(1), 100);
instance.getGPIO(13).blink(250, 5);
for (int i = 0; i < 101; i++) {
instance.getGPIO(13).dimLED(i);
}
}
}

View File

@@ -0,0 +1,97 @@
/*
* Copyright (C) 2020 Photon Vision.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.photonvision.hardware;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.photonvision.common.hardware.GPIO.CustomGPIO;
import org.photonvision.common.hardware.GPIO.GPIOBase;
import org.photonvision.common.hardware.GPIO.PiGPIO;
import org.photonvision.common.hardware.Platform;
import org.photonvision.common.hardware.metrics.CPU;
import org.photonvision.common.hardware.metrics.GPU;
import org.photonvision.common.hardware.metrics.RAM;
public class HardwareTest {
@Test
public void testHardware() {
CPU cpu = CPU.getInstance();
RAM ram = RAM.getInstance();
GPU gpu = GPU.getInstance();
if (!Platform.isRaspberryPi()) return;
System.out.println("Testing on platform: " + Platform.CurrentPlatform);
System.out.println("Printing CPU Info:");
System.out.println("Memory: " + cpu.getMemory() + "MB");
System.out.println("Temperature: " + cpu.getTemp() + "C");
System.out.println("Utilization: : " + cpu.getUtilization() + "%");
System.out.println("Printing GPU Info:");
System.out.println("Memory: " + gpu.getMemory() + "MB");
System.out.println("Temperature: " + gpu.getTemp() + "C");
System.out.println("Printing RAM Info: ");
System.out.println("Used RAM: : " + ram.getUsedRam() + "MB");
}
@Test
public void testGPIO() {
GPIOBase gpio;
if (Platform.isRaspberryPi()) {
gpio = new PiGPIO(18, 0, 100);
} else {
gpio = new CustomGPIO(18);
}
gpio.setHigh(); // HIGH
assertTrue(gpio.getState());
gpio.setLow(); // LOW
assertFalse(gpio.getState());
gpio.togglePin(); // HIGH
assertTrue(gpio.getState());
gpio.togglePin(); // LOW
assertFalse(gpio.getState());
gpio.setState(true); // HIGH
assertTrue(gpio.getState());
gpio.setState(false); // LOW
assertFalse(gpio.getState());
var success = gpio.shutdown();
assertTrue(success);
}
@Test
public void testBlink() {
if (!Platform.isRaspberryPi()) return;
GPIOBase pwm = new PiGPIO(18, 0, 100);
pwm.blink(125, 3);
var startms = System.currentTimeMillis();
while (true) {
if (System.currentTimeMillis() - startms > 4500) break;
}
}
}