Files
allwpilib/hal/lib/Athena/Semaphore.cpp
Alex Henning cb56c9a144 Initial commit of the WPILib simulation support in an alpha quality state.
Fixes to deal with the switch to .hpp files in the HAL and other misc problems due to rebasing.

Added Omar's changes to the compressor interface

Fixes to make C++ plugin compile on linux.

Added import of the WPILibSim code from the graduate class. It shows up as wpilibJavaSim to follow the convention set by wpilibJava, wpilibJavaJNI and wpilibJavaFinal.

Fixed wpilibJavaSim artifactId to mirror the new convention.

Modified the build of the java plugin to pull in the simulation dependencies.

Added stacktrace printing.

Fixed support for creating projects.

Added support for the isReal() and isSimulation() methods along with the AnalogPotentiometer object to support simulating GearsBot.

Added support for a "WPILib Simulate" button.

Added GearsBot to the built in examples.

Added support for specifying the world file during project creation and switched the default from BluntObjectBot to GearsBot.

Removed unused import.

Added file browser for world files.

Added support for debugging in simulation.

Change simulate icon to be a Gazebo icon.

Switched over to the gazebo messaging system.

Updated location of default world file.

Reverted cmake change.

Fixed bug in WPILibJSim, added better logging and cleaned up code.

Made the frc_gazebo_plugin build using raw cmake instead of catkin, breaking the final ROS dependencies.

Added installation to frc_gazebo_plugin Makefile.

Fixed running of simulation to actually use frcsim.

Initial commit of simulation library for C++. Has the minimal subset of features necessary for having a Simple Robot run in teleoperated mode.

Added notes for generating protobuf messages.

Import of the debuild process into the main repository.

Moved frc_gazebo_plugin under simulation and removed the gazebo folder.

Updated the gazebo plugin to remove excessive printing and limit motor signal to [-1,1].

Updated WPILibJSim to support latching messages and to sleep for 20ms in iterative robot.

Reduced delay between starting frcsim and the users program to 1 second.

Updated GearsBot example.

Fixed a few minor issues for demoable state.

Added simulator support for Victors, Jaguars and Talons.

Added NetworkTables, SmartDashboard and LiveWindow to the simulator.

Added AnalogPotentiometer for simulation.

Added support for simulating encoders.

Added simulation support for Gyro.

Added IterativeRobot, Fixed Timers, Notifiers, PIDControllers and other minor fixes + cleanup.

Added RobotDrive support to simulation.

Separated out JavaGazebo so that SimDS will be able to reuse it.

Separated out SimDS into its own application..

Fixes so that the SimDS is distributed and runs properly for Java with the eclipse plugins.

Added DriverStation support to WPILibCSim

Cleanup of DriverStation, WaitUntilCommand and AnalogPotentiometer for WPILibCSim.

Cleanup of includes for WPILibCSim

Added AnalogPotentiometer to the real WPILibC.

Added AnalogPotentiometer to the real WPILibC.

Added GearsBot example to C++ eclipse plugin.

WPILibCSim fixes to work with launching from the plugin.

Package libwpilibsim in a deb file.

Added includes to plugin distribution.

Added support for external-limit-switches to Gazebo, Java and C++.

Added support for Gazebo Rangefinders and Analog channels to read their values in C++ and Java.

Added support for internal limit switches.

Updated GearsBot programs to use limit switches + range finders.

Added disabling of motors when robot is disabled to more closely mimic the real robot.

Fixes to deal with the switch to .hpp files in the HAL and other misc problems due to rebasing.

Change-Id: I624c5f4d0f28282616a7c92083575bf68adcdce2
2014-06-13 09:31:32 -04:00

138 lines
3.0 KiB
C++

#include "HAL/Semaphore.hpp"
#include "Log.hpp"
// set the logging level
TLogLevel semaphoreLogLevel = logDEBUG;
#define SEMAPHORE_LOG(level) \
if (level > semaphoreLogLevel) ; \
else Log().Get(level)
// See: http://www.vxdev.com/docs/vx55man/vxworks/ref/semMLib.html
const uint32_t SEMAPHORE_Q_FIFO= 0x01; // TODO: Support
const uint32_t SEMAPHORE_Q_PRIORITY = 0x01; // TODO: Support
const uint32_t SEMAPHORE_DELETE_SAFE = 0x04; // TODO: Support
const uint32_t SEMAPHORE_INVERSION_SAFE = 0x08; // TODO: Support
const int32_t SEMAPHORE_NO_WAIT = 0;
const int32_t SEMAPHORE_WAIT_FOREVER = -1;
const uint32_t SEMAPHORE_EMPTY = 0;
const uint32_t SEMAPHORE_FULL = 1;
MUTEX_ID initializeMutexRecursive()
{
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
MUTEX_ID sem = new pthread_mutex_t();
pthread_mutex_init(sem, &attr);
pthread_mutexattr_destroy(&attr);
return sem;
}
MUTEX_ID initializeMutexNormal()
{
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
MUTEX_ID sem = new pthread_mutex_t();
pthread_mutex_init(sem, &attr);
pthread_mutexattr_destroy(&attr);
return sem;
}
void deleteMutex(MUTEX_ID sem)
{
pthread_mutex_destroy(sem);
delete sem;
}
/**
* Lock the semaphore, blocking until it's available.
* @return 0 for success, -1 for error. If -1, the error will be in errno.
*/
int8_t takeMutex(MUTEX_ID sem)
{
return pthread_mutex_lock(sem);
}
int8_t tryTakeMutex(MUTEX_ID sem)
{
return pthread_mutex_trylock(sem);
}
/**
* Unlock the semaphore.
* @return 0 for success, -1 for error. If -1, the error will be in errno.
*/
int8_t giveMutex(MUTEX_ID sem)
{
return pthread_mutex_unlock(sem);
}
SEMAPHORE_ID initializeSemaphore(uint32_t initial_value) {
SEMAPHORE_ID sem = new sem_t;
sem_init(sem, 0, initial_value);
return sem;
}
void deleteSemaphore(SEMAPHORE_ID sem) {
sem_destroy(sem);
delete sem;
}
/**
* Lock the semaphore, blocking until it's available.
* @return 0 for success, -1 for error. If -1, the error will be in errno.
*/
int8_t takeSemaphore(SEMAPHORE_ID sem)
{
return sem_wait(sem);
}
int8_t tryTakeSemaphore(SEMAPHORE_ID sem)
{
return sem_trywait(sem);
}
/**
* Unlock the semaphore.
* @return 0 for success, -1 for error. If -1, the error will be in errno.
*/
int8_t giveSemaphore(SEMAPHORE_ID sem)
{
return sem_post(sem);
}
MULTIWAIT_ID initializeMultiWait() {
pthread_condattr_t attr;
pthread_condattr_init(&attr);
MULTIWAIT_ID cond = new pthread_cond_t();
pthread_cond_init(cond, &attr);
pthread_condattr_destroy(&attr);
return cond;
}
void deleteMultiWait(MULTIWAIT_ID sem) {
pthread_cond_destroy(sem);
delete sem;
}
int8_t takeMultiWait(MULTIWAIT_ID sem, int32_t timeout) {
MUTEX_ID m = initializeMutexNormal();
takeMutex(m);
int8_t val = pthread_cond_wait(sem, m);
deleteMutex(m);
return val;
}
int8_t giveMultiWait(MULTIWAIT_ID sem) {
return pthread_cond_broadcast(sem);
}