Pull request for the Extensions interface only (#655)

* Modify halsim to be able to load extension libraries if they are available.

It will read the list of libraries to try from the HALSIM_EXTENSIONS
environment variable.  Multiple libraries can be given if separated
by ';' (Windows) or ':' (Unix).

The library must have an 'HALSIM_InitExtension' method that returns >= 0 on success.

The library is expected to use the interface expressed by
  hal/src/src/main/native/include/MockData

* Add a simple halsim library that just prints robot values.

This makes a good test bed for cross platform purposes,
and provides the ultimate in light weight simulators.

This initial version only prints PWM values.
This commit is contained in:
Jeremy White
2017-10-18 02:27:55 -05:00
committed by Peter Johnson
parent 2fc60680f4
commit be77f9cb26
11 changed files with 284 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
description = "A simulation shared object that simply prints robot behaviors"
apply plugin: 'edu.wpi.first.NativeUtils'
apply plugin: 'cpp'
ext.skipAthena = true
apply from: "../../config.gradle"
model {
components {
halsim_print(NativeLibrarySpec)
}
binaries {
all {
project(':hal').addHalCompilerArguments(it)
project(':hal').addHalToLinker(it)
}
withType(StaticLibraryBinarySpec) {
it.buildable = false
}
}
}
apply from: 'publish.gradle'

View File

@@ -0,0 +1,43 @@
apply plugin: 'maven-publish'
apply plugin: 'edu.wpi.first.wpilib.versioning.WPILibVersioningPlugin'
if (!hasProperty('releaseType')) {
WPILibVersion {
releaseType = 'dev'
}
}
def pubVersion = ''
if (project.hasProperty("publishVersion")) {
pubVersion = project.publishVersion
} else {
pubVersion = WPILibVersion.version
}
def baseArtifactId = 'halsim-print'
def artifactGroupId = 'edu.wpi.first.halsim'
model {
publishing {
def libSpec
$.components.each {
if (it in NativeLibrarySpec) {
$.binaries.each {
if (it in SharedLibraryBinarySpec) {
libSpec = it.sharedLibraryFile
}
}
}
}
publications {
cpp(MavenPublication) {
artifact libSpec
artifactId = baseArtifactId
groupId artifactGroupId
version pubVersion
}
}
}
}

View File

@@ -0,0 +1,29 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#include "PrintPWM.h"
#include <iostream>
#include "MockData/HAL_Value.h"
#include "MockData/NotifyListener.h"
#include "MockData/PWMData.h"
static void PWMCallback(const char* name, void* param,
const struct HAL_Value* value) {
auto pwm = static_cast<PrintPWM*>(param);
pwm->Publish(value->data.v_double);
}
PrintPWM::PrintPWM(int port) {
m_port = port;
HALSIM_RegisterPWMSpeedCallback(port, PWMCallback, this, false);
}
void PrintPWM::Publish(double value) {
std::cout << "PWM " << m_port << ": " << value << std::endl;
}

View File

@@ -0,0 +1,34 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#include <iostream>
#include <HAL/Ports.h>
#include "HALSimPrint.h"
#include "PrintPWM.h"
/**
* Currently, robots never terminate, so we keep a single static object
* and it is never properly released or cleaned up.
*/
static HALSimPrint halsim;
extern "C" {
#if defined(WIN32) || defined(_WIN32)
__declspec(dllexport)
#endif
int HALSIM_InitExtension(void) {
std::cout << "Print Simulator Initializing." << std::endl;
int pwmCount = HAL_GetNumPWMChannels();
halsim.m_pwms.reserve(pwmCount);
for (int i = 0; i < pwmCount; i++) halsim.m_pwms.emplace_back(i);
return 0;
}
} // extern "C"

View File

@@ -0,0 +1,17 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <vector>
class PrintPWM;
class HALSimPrint {
public:
std::vector<PrintPWM> m_pwms;
};

View File

@@ -0,0 +1,19 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#pragma once
#include "HALSimPrint.h"
class PrintPWM {
public:
explicit PrintPWM(int port);
void Publish(double value);
private:
int m_port;
};