forked from scanse/sweep-sdk
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
608ea48
commit cd470c4
Showing
27 changed files
with
4,633 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Ignore everything | ||
|
||
arm/* | ||
build/* | ||
release/* | ||
.gradle/ | ||
|
||
*.project | ||
.settings/org.eclipse.buildship.core.prefs | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import org.gradle.internal.os.OperatingSystem | ||
|
||
plugins { | ||
id 'net.ltgt.errorprone' version '0.0.8' | ||
id 'edu.wpi.first.wpilib.versioning.WPILibVersioningPlugin' version '1.6' | ||
} | ||
|
||
allprojects { | ||
repositories { | ||
mavenCentral() | ||
} | ||
} | ||
|
||
if (!hasProperty('releaseType')) { | ||
WPILibVersion { | ||
releaseType = 'official' | ||
} | ||
} | ||
|
||
ext.useDriver = true | ||
|
||
ext.compilerPrefixToUse = project.hasProperty('compilerPrefix') ? project.compilerPrefix : 'arm-frc-linux-gnueabi-' | ||
|
||
ext.setupDefines = { project, binaries -> | ||
binaries.all { | ||
if (project.hasProperty('debug')) { | ||
project.setupDebugDefines(cppCompiler, linker) | ||
} else { | ||
project.setupReleaseDefines(cppCompiler, linker) | ||
} | ||
} | ||
} | ||
|
||
apply from: "locations.gradle" | ||
|
||
ext.addUserLinks = { linker, targetPlatform, implLib -> | ||
def libPattern = /.*((\\/|\\).*)+lib(?<libName>.+).(.+)$/ | ||
def libraryArgs = [] | ||
def libraryPath = file(driverLibraryLib).path | ||
|
||
// adds all libraries found in the driver folder | ||
def libraryTree = fileTree(libraryPath) | ||
libraryTree.include '*.so' | ||
libraryTree.include '*.a' | ||
|
||
libraryTree.each { lib -> | ||
def nameMatcher = (lib.path =~ libPattern) | ||
if (nameMatcher[0].size() > 1) { | ||
def name = nameMatcher.group('libName') | ||
libraryArgs << '-l' + name | ||
} | ||
} | ||
|
||
if (implLib) { | ||
// adds all libraries found in the impl folder | ||
def implLibraryPath = file(cppLibraryLib).path | ||
def implLibraryTree = fileTree(implLibraryPath) | ||
implLibraryTree.include '*.so' | ||
implLibraryTree.include '*.a' | ||
|
||
implLibraryTree.each { lib -> | ||
def nameMatcher = (lib.path =~ libPattern) | ||
if (nameMatcher[0].size() > 1) { | ||
def name = nameMatcher.group('libName') | ||
libraryArgs << '-l' + name | ||
} | ||
} | ||
} | ||
|
||
// Add all arguments | ||
String architecture = targetPlatform.architecture | ||
if (architecture.contains('arm')){ | ||
linker.args << '-L' + libraryPath | ||
linker.args.addAll(libraryArgs) | ||
} | ||
} | ||
|
||
apply from: "properties.gradle" | ||
|
||
apply from: "dependencies.gradle" | ||
|
||
if (useDriver) { | ||
apply from: "driver.gradle" | ||
} | ||
apply from: "cpp.gradle" | ||
|
||
// Empty task for build so that zips will be | ||
// built when running ./gradlew build | ||
task build | ||
|
||
apply from: "releases.gradle" | ||
|
||
task clean(type: Delete) { | ||
description = "Deletes the build directory" | ||
group = "Build" | ||
delete buildDir | ||
} | ||
|
||
clean { | ||
delete releaseDir | ||
} | ||
|
||
task wrapper(type: Wrapper) { | ||
gradleVersion = '3.3' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
defineWpiUtilProperties() | ||
defineHALProperties() | ||
defineNetworkTablesProperties() | ||
defineWpiLibProperties() | ||
defineCsCoreProperties() | ||
|
||
def cppSetupModel = { project -> | ||
project.model { | ||
components { | ||
sweep(NativeLibrarySpec) { | ||
targetPlatform 'arm' | ||
setupDefines(project, binaries) | ||
|
||
binaries.all { | ||
tasks.withType(CppCompile) { | ||
cppCompiler.args "-DNAMESPACED_WPILIB" | ||
addUserLinks(linker, targetPlatform, false) | ||
addHalLibraryLinks(it, linker, targetPlatform) | ||
addWpiUtilLibraryLinks(it, linker, targetPlatform) | ||
addNetworkTablesLibraryLinks(it, linker, targetPlatform) | ||
addWpilibLibraryLinks(it, linker, targetPlatform) | ||
addCsCoreLibraryLinks(it, linker, targetPlatform) | ||
} | ||
} | ||
|
||
sources { | ||
cpp { | ||
source { | ||
srcDirs = [cppSrc, cppLibrarySrc] | ||
includes = ["**/*.cpp"] | ||
} | ||
exportedHeaders { | ||
srcDirs = [cppInclude, driverLibraryInclude, cppLibraryInclude, wpilibInclude, halInclude, wpiUtilInclude, netTablesInclude, cscoreInclude] | ||
includes = ['**/*.h'] | ||
} | ||
if (useDriver) { | ||
lib project: ':arm:driver', library: 'sweepDriver', linkage: 'static' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
ext.mergeStaticSetup = { pjt -> | ||
if (combineStaticLibs && useDriver) { | ||
pjt.tasks.whenObjectAdded { task -> | ||
def name = task.name.toLowerCase() | ||
if (name.contains('create') && name.contains('staticlibrary')) { | ||
def libraryPath = task.outputFile.parent | ||
def library = file(task.outputFile.absolutePath) | ||
project(':arm:driver').model { | ||
binaries { | ||
withType(StaticLibraryBinarySpec) { binary -> | ||
ext.driverLibrary = new File(binary.staticLibraryFile.absolutePath) | ||
} | ||
} | ||
} | ||
task.doLast { | ||
library.renameTo(file("${library}.m")) | ||
copy { | ||
from driverLibrary | ||
into libraryPath | ||
} | ||
|
||
ByteArrayOutputStream mriFile = new ByteArrayOutputStream() | ||
mriFile << "create lib${libraryName}.a\n" | ||
mriFile << "addlib lib${libraryName}.a.m\n" | ||
mriFile << "addlib lib${libraryName}Driver.a\n" | ||
mriFile << "save\n" | ||
mriFile << "end\n" | ||
|
||
def stdIn = new ByteArrayInputStream(mriFile.buf) | ||
|
||
exec { | ||
standardInput = stdIn | ||
workingDir = libraryPath | ||
commandLine "${compilerPrefixToUse}ar", '-M' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
project(':arm:cpp') { | ||
apply plugin: 'cpp' | ||
|
||
apply from: "${rootDir}/toolchains/arm.gradle" | ||
|
||
if (!useDriver) { | ||
apply from: "${rootDir}/java/java.gradle" | ||
} | ||
|
||
cppSetupModel(project) | ||
|
||
project.debugStripSetup() | ||
mergeStaticSetup(project) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* C++ Wrapper around the low-level primitives. | ||
* Automatically handles resource management. | ||
* | ||
* sweep::Sweep - device to interact with | ||
* sweep::SweepSample - a single sample in a full scan | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <vector> | ||
|
||
#include "SensorBase.h" | ||
|
||
#include "sweep.h" | ||
|
||
namespace sweep { | ||
|
||
struct SweepSample { | ||
SweepSample(int angle_, int distance_, int signalStrength_) | ||
: angle(angle_), distance(distance_), signalStrength(signalStrength_) {} | ||
int angle; | ||
int distance; | ||
int signalStrength; | ||
}; | ||
|
||
class Sweep : public frc::SensorBase { | ||
public: | ||
Sweep(); | ||
Sweep(const char* port, int bitrate); | ||
Sweep(const Sweep&) = delete; | ||
Sweep& operator=(const Sweep&) = delete; | ||
~Sweep(); | ||
|
||
void StartScanning(); | ||
void StopScanning(); | ||
|
||
int GetMotorSpeed(); | ||
void SetMotorSpeed(int speed); | ||
|
||
std::vector<SweepSample> GetScan(); | ||
|
||
void Reset(); | ||
|
||
private: | ||
sweep_device* m_device; | ||
}; | ||
|
||
} // namespace sweep |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#include "Sweep.hpp" | ||
|
||
namespace sweep { | ||
|
||
Sweep::Sweep() { | ||
sweep_error_s error = nullptr; | ||
m_device = sweep_device_construct_simple(&error); | ||
if (error) { | ||
wpi_setErrorWithContext(-100, sweep_error_message(error)); | ||
sweep_error_destruct(error); | ||
m_device = 0; | ||
} | ||
} | ||
|
||
Sweep::Sweep(const char *port, std::int32_t bitrate) { | ||
sweep_error_s error = nullptr; | ||
m_device = sweep_device_construct(port, bitrate, &error); | ||
if (error) { | ||
wpi_setErrorWithContext(-100, sweep_error_message(error)); | ||
sweep_error_destruct(error); | ||
m_device = 0; | ||
} | ||
} | ||
|
||
Sweep::~Sweep() { | ||
sweep_device_destruct(m_device); | ||
} | ||
|
||
void Sweep::StartScanning() { | ||
if (StatusIsFatal()) return; | ||
sweep_error_s error = nullptr; | ||
sweep_device_start_scanning(m_device, &error); | ||
if (error) { | ||
wpi_setErrorWithContext(-100, sweep_error_message(error)); | ||
sweep_error_destruct(error); | ||
} | ||
} | ||
|
||
void Sweep::StopScanning() { | ||
if (StatusIsFatal()) return; | ||
sweep_error_s error = nullptr; | ||
sweep_device_stop_scanning(m_device, &error); | ||
if (error) { | ||
wpi_setErrorWithContext(-100, sweep_error_message(error)); | ||
sweep_error_destruct(error); | ||
} | ||
} | ||
|
||
int Sweep::GetMotorSpeed() { | ||
if (StatusIsFatal()) return 0; | ||
sweep_error_s error = nullptr; | ||
int rv = sweep_device_get_motor_speed(m_device, &error); | ||
if (error) { | ||
wpi_setErrorWithContext(-100, sweep_error_message(error)); | ||
sweep_error_destruct(error); | ||
} | ||
return rv; | ||
} | ||
|
||
void Sweep::SetMotorSpeed(int speed) { | ||
if (StatusIsFatal()) return; | ||
sweep_error_s error = nullptr; | ||
sweep_device_set_motor_speed(m_device, speed, &error); | ||
if (error) { | ||
wpi_setErrorWithContext(-100, sweep_error_message(error)); | ||
sweep_error_destruct(error); | ||
} | ||
} | ||
|
||
std::vector<SweepSample> Sweep::GetScan() { | ||
if (StatusIsFatal()) return std::vector<SweepSample>{}; | ||
|
||
sweep_error_s error = nullptr; | ||
sweep_scan_s scan = sweep_device_get_scan(m_device, &error); | ||
if (error) { | ||
wpi_setErrorWithContext(-100, sweep_error_message(error)); | ||
sweep_error_destruct(error); | ||
return std::vector<SweepSample>{}; | ||
} | ||
|
||
int num_samples = sweep_scan_get_number_of_samples(scan); | ||
|
||
std::vector<SweepSample> result; | ||
result.reserve(num_samples); | ||
|
||
for (int n = 0; n < num_samples; ++n) { | ||
int angle = sweep_scan_get_angle(scan, n); | ||
int distance = sweep_scan_get_distance(scan, n); | ||
int signal = sweep_scan_get_signal_strength(scan, n); | ||
|
||
result.emplace_back(angle, distance, signal); | ||
} | ||
|
||
sweep_scan_destruct(scan); | ||
return result; | ||
} | ||
|
||
void Sweep::Reset() { | ||
if (StatusIsFatal()) return; | ||
sweep_error_s error = nullptr; | ||
sweep_device_reset(m_device, &error); | ||
if (error) { | ||
wpi_setErrorWithContext(-100, sweep_error_message(error)); | ||
sweep_error_destruct(error); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.