I'm working with following code which gives access to low level monitor configuration using Windows APIs
https://github.com/scottaxcell/winddcutil/blob/main/winddcutil/winddcutil.cpp
And I would like to create a new function that increases or decreases the brightness, I was able to do this using Powershell but since the C++ code looks somewhat easy to understand I want to have a crack at it and try my luck and hopefully integrate it with an ambient light sensor later.
The powershell code I have is as follows which works with above executable: (its very crude at this stage)
$cb = [int]([uint32]("0x" + ((C:\Users\Nick\WindowsScripts\winddcutil-main\x64\Release\winddcutil.exe getvcp 0 10) -join "`n").split(" ")[2]))
if ($args[0] -eq "increase") {
if ( $cb -ne 100) {
$nb = "{0:x}" -f ($cb + 10)
C:\Users\Nick\WindowsScripts\winddcutil-main\x64\Release\winddcutil.exe setvcp 0 10 $nb
}
} elseif ($args[0] -eq "decrease") {
if ( $cb -ne 10) {
$nb = "{0:x}" -f ($cb - 10)
C:\Users\Nick\WindowsScripts\winddcutil-main\x64\Release\winddcutil.exe setvcp 0 10 $nb
}
}
It gets current brightness and if argument given is "increase" and if brightness is not already 100 then adds 10, in case of "decrease" it subtracts 10. Values are coveted to and from hex to decimals.
I understand if I want to integrate this inside the C++ code directly I would have something like following:
int increaseBrightness(std::vector<std::string> args) {
size_t did = INT_MAX;
did = std::stoi(args[0]);
//0 is monitor ID and 10 is the feature code for brightness
//currentBrightness = getVcp("0 10")
//calculate new value
//setVcp("0 10 NewValue")
}
Ultimetaly I would like to call the executable like "winddcutil.exe increasebrightness 0" (0 being the display ID)
I can keep digging around on how to do the calculation in C++ but internally calling the functions and passing the arguments so far turned out to be very challenging for me and I would appreciate some help there.
you need to add a needed option here
line 164
std::unordered_map<std::string,std::function<int(std::vector<std::string>)>> commands
{
{ "help", printUsage },
{ "detect", detect},
{ "capabilities", capabilities },
{ "getvcp", getVcp },
{ "setvcp", setVcp},
{"increasebrightness ", increaseBrightness } // update here
};
to get current brightness you can't use getVcp api due to its result will be printed to stdout , it isn't returned via returned value, follow getVcp to get brighness value , use this
DWORD currentValue;
bool success = GetVCPFeatureAndVCPFeatureReply(physicalMonitorHandle, vcpCode, NULL, ¤tValue, NULL);
if (!success) {
std::cerr << "Failed to get the vcp code value" << std::endl;
return success;
}
then
define your increaseBrightness like
int increaseBrightness(std::vector<std::string> args) {
size_t did = INT_MAX;
did = std::stoi(args[0]);
DWORD currentBrightness;
bool success = GetVCPFeatureAndVCPFeatureReply(
physicalMonitorHandle, vcpCode, NULL, ¤tBrightness, NULL);
if (!success) {
std::cerr << "Failed to get the vcp code value" << std::endl;
return success;
}
//example + 10
auto newValue = did + 10;
success = setVcp({"0", "10", std::to_string(newValue)});
if(success)
{
// your handler
}
// 0 is monitor ID and 10 is the feature code for brightness
// currentBrightness = getVcp("0 10")
// calculate new value
// setVcp("0 10 NewValue")
}
test for passing argument:
https://godbolt.org/z/5n5Gq3d7e
note: make sure your have increaseBrightness's declaration before std::unordered_map<std::string,std::function<int(std::vector<std::string>)>> commands to avoid compiler's complaint
Related
I am writing code for the double-tap event but it's not working perfectly so I think that configuring the below internal registers might help to get better results:
doubleTapDur,
doubleTapQuiet,
tapThs,
axes
To set the value of the registers, I'm using ADB bridge via the Movesense showcase adb bridge app. here is the Link of the APK:Showcaseapp-debug_adb-bridge-build.apk
I use the below command to read and write double-tap parameters. and it's working.
for read:
adb.exe shell am broadcast -a "android.intent.action.MOVESENSE" --es type get --es path Component/LSM6DS3/TapParams --es value '''{}'''
for write:
adb.exe shell am broadcast -a "android.intent.action.MOVESENSE" --es type put --es path Component/LSM6DS3/TapParams --es value '''{\"newParams\":{\"doubleTapDur\": 8, \"doubleTapQuiet\": 3, \"tapThs\": 8, \"axes\": 8}}'''
To set these parameters internally (inside code), I wrote a function DoubleTAPConfig(), which sets the value in params. But it's not working. I am not sure what I'm missing. Any help would be greatly appreciated.
Here is my code:-
#include "DOUBLETAP.h"
#include "common/core/debug.h"
#include "component_led/resources.h"
#include "component_lsm6ds3/resources.h"
#include "movesense.h"
#include "system_mode/resources.h"
#include "system_states/resources.h"
#include "ui_ind/resources.h"
const size_t BLINK_PERIOD_MS = 1000;
#define AVAILABILITY_TIME 5000
// LED blinking period in adertsing mode
#define LED_BLINKING_PERIOD 100
const char* const DOUBLETAP::LAUNCHABLE_NAME = "DOUBLETAP";
DOUBLETAP::DOUBLETAP()
: ResourceClient(WBDEBUG_NAME(__FUNCTION__), WB_EXEC_CTX_APPLICATION),
LaunchableModule(LAUNCHABLE_NAME, WB_EXEC_CTX_APPLICATION),
DOUBLETAPEnabled(false),
mTimer(wb::ID_INVALID_TIMER),
mCounter(0)
{
}
DOUBLETAP::~DOUBLETAP()
{
}
bool DOUBLETAP::initModule()
{
DEBUGLOG("DOUBLETAP::initModule");
this->mModuleState = WB_RES::ModuleStateValues::INITIALIZED;
return true;
}
void DOUBLETAP::deinitModule()
{
DEBUGLOG("DOUBLETAP::deinitModule");
this->mModuleState = WB_RES::ModuleStateValues::UNINITIALIZED;
}
bool DOUBLETAP::startModule()
{
DEBUGLOG("DOUBLETAP::startModule");
this->mModuleState = WB_RES::ModuleStateValues::STARTED;
this->mTimer = startTimer(BLINK_PERIOD_MS, true); // Start LED timer. true = trigger repeatedly
// set internal resistor value for doubletap mode
DoubleTAPConfig();
// Subscribe to DOUBLETAP detection
this->asyncSubscribe(WB_RES::LOCAL::SYSTEM_STATES_STATEID(), AsyncRequestOptions::Empty, WB_RES::StateIdValues::DOUBLETAP);
return true;
}
void DOUBLETAP::stopModule()
{
DEBUGLOG("DOUBLETAP::stopModule");
this->stopTimer(mTimer); // Stop LED timer
this->mTimer = wb::ID_INVALID_TIMER;
// Unsubscribe DOUBLETAP detection
this->asyncUnsubscribe(WB_RES::LOCAL::SYSTEM_STATES_STATEID(), AsyncRequestOptions::Empty, WB_RES::StateIdValues::DOUBLETAP);
this->mModuleState = WB_RES::ModuleStateValues::STOPPED;
}
void DOUBLETAP::onTimer(wb::TimerId timerId)
{
if (DOUBLETAPEnabled)
{
this->asyncPut(WB_RES::LOCAL::UI_IND_VISUAL(), AsyncRequestOptions::Empty, WB_RES::VisualIndTypeValues::SHORT_VISUAL_INDICATION);
// break;
}
else
{
DOUBLETAPEnabled = false;
// break;
}
}
void DOUBLETAP::onNotify(wb::ResourceId resourceId, const wb::Value& value, const wb::ParameterList& parameters)
{
DEBUGLOG("DOUBLETAP::onNotify");
switch (resourceId.localResourceId)
{
case WB_RES::LOCAL::SYSTEM_STATES_STATEID::LID: {
const WB_RES::StateChange& stateChange = value.convertTo<const WB_RES::StateChange&>();
if (stateChange.stateId == WB_RES::StateIdValues::DOUBLETAP)
{
DEBUGLOG("Lead state updated. newState: %d", stateChange.newState);
DOUBLETAPEnabled = !DOUBLETAPEnabled;
}
}
}
}
void DOUBLETAP::DoubleTAPConfig()
{
WB_RES::TapParams TapConfig;
TapConfig.doubleTapDur = 8; // set 0-15 Default:_ 0000
TapConfig.doubleTapQuiet = 3; // set 0-3 Default value:_ 00
TapConfig.tapThs = 8; // set 0-31 Default value:_ 00000
TapConfig.axes = 8; // set 0-14 bitfield:_ x=8, y=4, z=2
asyncPut(WB_RES::LOCAL::COMPONENT_LSM6DS3_TAPPARAMS(), AsyncRequestOptions::Empty, TapConfig);
// also try with this coomund line
// asyncPut(WB_RES::LOCAL::COMPONENT_LSM6DS3_WAKEUP(),AsyncRequestOptions(NULL, 0, true), TapConfig);
}
I am not sure what I'm missing. Any help would be greatly appreciated.
The /Component/LSM6DS3/TapParams -resource accessed the LSM6 chip registers directly, so in your firmware code the following happens:
You set the tap-params into the registers (DoubleTAPConfig method)
You start DOUBLE_TAP detection, which immediately resets the tap parameters in the registers to defaults
You'll need to:
first subscribe to the DOUBLE_TAP service
only when it's running (safest to wait until you receive the onSubscribeResult-callback with resultCode == 200), call the DoubleTAPConfig().
Full disclosure: I work for the Movesense team
I'm having fun coding simple OpenGL demos and I recently decided to use Lua with my C++ engine in order to change the rendering dynamically without having to recompile on and on my project. Thus I can tweak more easily the rendering algorithm. But I know that my current rendering update functions are probably far from being efficient.
For the moment, I'm transfering a matrix from C++ to Lua, modifying it in a Lua script and sending it back to my C++ rendering engine. But I'm reloading the Lua script each time I get an update call from the C++ engine, and I'm losing all of the variable context. That means I'm always starting from scratch and my rendering is far from being smooth. I include some code sample below to explain what I'm doing. I am currently learning Lua with C++ embedding, so I know I still don't have the best practices.
update.lua
function transform(m)
amplitude = 1.5
frequency = 500
phase = 0.0
r = {}
for i = 1, #m do
r[i] = {}
for j = 1, #m[i] do
if (i % 2) then
r[i][j] = amplitude * math.sin(m[i][j] + phase)
else
r[i][j] = -amplitude * math.sin(m[i][j] + phase)
end
phase = phase + 0.001
end
end
return r
end
-- called by c++
function update()
m = pull()
r = transform(m)
push(r)
end
matrix.cpp
// pull matrix from lua point of view
static int pull(lua_State * _L)
{
_push(_L, &_m);
return 1;
}
// push matrix from lua point of view
static int push(lua_State * _L)
{
// get number of arguments
int n = lua_gettop(_L);
if(1 == n) {
_pull(_L, 1, &_m);
}
return 1;
}
void matrix::load_file(char * file, char * function)
{
int status;
// load the file containing the script we are going to run
status = luaL_loadfile(_L, file);
switch (status) {
case LUA_OK:
break;
case LUA_ERRFILE:
std::cout << "LUA_ERRFILE: " << lua_error(_L) << std::endl;
break;
case LUA_ERRSYNTAX:
std::cout << "LUA_ERRSYNTAX: " << lua_error(_L) << std::endl;
break;
default:
std::cout << lua_error(_L) << std::endl;
}
lua_getglobal(_L, function);
status = lua_pcall(_L, 1, 1, 0);
if (status != LUA_OK) {
std::cout << "error running file" << lua_error(_L) << std::endl;
}
}
void matrix::update()
{
load_file("lua/update.lua", "update");
}
I'm thinking of passing some arguments when calling the update() function, but I'm wondering if the C++ to Lua then back to C++ approach is correct and efficient. Especially considering the fact that I might transfer and modify huge matrix in Lua. I probably lack some embedded Lua knowledge to keep context while loading a script. Do you have some general advice on how I would improve my code ? I know that my current approach is overly complicated.
A quick fix would be to only load the file if it has been modified since the last frame:
static time_t last_modified = 0;
struct stat sbuf;
stat(file, &sbuf);
if (sbuf.st_mtime > last_modified) {
last_modified = sbuf.st_mtime;
status = luaL_loadfile(_L, file);
// etc
}
// Now call the function
lua_getglobal(_L, function);
status = lua_pcall(_L, 1, 1, 0);
OK, loading the chunk of the update() function into a global variable and having a global parameter table in the Lua script is the way to go. I achieved this using the following guidelines, and I will post the detailed steps below. Basically, loading the script entirely first ensures that all global variables are stored in the C++ context. Then storing the wanted function as an index allows us to run it again, while keeping the global variables in the script evolving on their own.
Step 1
First call luaL_loadfile once at init
Step 2
Run the script once using lua_pcall(_L, 0, 0, 0);
This ensures that the global variables, which are used as parameters in the Lua script are in memory.
Step 3
Store the Lua function. I managed to do it with the following C++ code:
void matrix::store(char * function)
{
lua_newtable(_L); // create table for functions
_idx = luaL_ref(_L, LUA_REGISTRYINDEX); // store said table in pseudo-registry
lua_rawgeti(_L, LUA_REGISTRYINDEX, _idx); // retrieve table for functions
lua_getglobal(_L, function); // retrieve function to store
if (lua_isfunction(_L, -1)) {
_f = luaL_ref(_L, -2); // store a function in the function table
}
else {
lua_pop(_L, 1);
std::cout << "can't find " << function << std::endl;
}
// table is two places up the current stack counter
lua_pop(_L, 1); // we are done with the function table, so pop it
std::cout << "idx: " << _idx << ", function: " << _f << std::endl;
}
Step 4
Call the stored function again when rendering using the following C++ function:
void matrix::run()
{
int status;
if (_f == -1) {
std::cout << "invalid function index " << _f << std::endl;
}
else {
lua_rawgeti(_L, LUA_REGISTRYINDEX, _idx); // retrieve function table
lua_rawgeti(_L, -1, _f); // retrieve function
//use function
status = lua_pcall(_L, 0, 0, 0); // 0 arguments, 0 results
if (status != LUA_OK) {
std::cout << "error running function" << lua_error(_L) << std::endl;
}
//don't forget to pop the function table from the stack
lua_pop(_L, 1);
}
}
Step 5 (optional)
If we set all the Lua parameters in a global table, we can retrieve them dynamically in C++ using the following piece of code:
void matrix::get_params(char * p)
{
lua_getglobal(_L, p);
lua_pushnil(_L);
int i = 0;
while(lua_next(_L,-2))
{
const char * key = lua_tostring(_L,-2);
double value = lua_tonumber(_L,-1);
lua_pop(_L,1);
std::cout << key << " = " << value << std::endl;
_h[i].key.assign(key);
_h[i].value = value;
i++;
}
lua_pop(_L, 1);
}
Where _his a simple dynamic structure defined as such:
typedef struct {
std::string key;
float value;
} hash;
I only use float, so this simple structure is convenient enough for my needs, and allows me to add lots of variables in my Lua script without bothering with a structure definition in C++. This way I can add as many parameters in my Lua table and do the maths when updating.
Step 6
Tweak the Lua script forever ! Et voila:
p = {
amplitude = 1.5,
frequency = 500,
phase = 0.0
}
function transform(m)
r = {}
for i = 1, #m do
r[i] = {}
for j = 1, #m[i] do
if (i % 2) then
r[i][j] = p.amplitude * math.sin(m[i][j] + p.phase)
else
r[i][j] = -p.amplitude * math.sin(m[i][j] + p.phase)
end
p.phase = p.phase + 0.001
end
end
return r
end
-- called by c++
function update()
m = pull()
r = transform(m)
push(r)
end
This solution fits my needs, but seems very complicated and inefficient. But it was a fine hacking session anyway.
This question already has answers here:
Printing an array in C++?
(14 answers)
Closed 3 years ago.
I'm brand new to C++, and I've been trying to make a simple game. I want a system for status effects the player can apply to the enemy, so I made an array of strings that can hold 5 elements, like "burning" or "poisoned" etc. When I tested this, however, it seemed to always apply the status effect (in this case, burning) to the first AND second status "slot". I want it to only apply to my first empty slot. I took this portion of the code out and expanded it from a for loop to an if statement, and this time I outputted the value of statusEffect and it gave me 0x1000020f0. I have no idea what that even is.
Here's my code, I'm working on Xcode if that helps.
int statusEffectDuration[5] = {0, 0, 0, 0, 0};
string statusEffect[5] = {"NONE", "NONE", "NONE", "NONE", "NONE"};
// Function for applying status effects
void applyStatusEffect(string appliedEffect, int statusDuration) {
if (statusEffect[0] == "NONE") { // If first status effect is nothing then changes first status to "EFFECT"
statusEffect[0] = appliedEffect;
statusEffectDuration[0] = statusDuration;
}
else if (statusEffect[1] == "NONE") { // Same as first, but if second status is nothing then changes second status to "EFFECT"
statusEffect[1] = appliedEffect;
statusEffectDuration[1] = statusDuration;
}
else if (statusEffect[2] == "NONE") { // Third
statusEffect[2] = appliedEffect;
statusEffectDuration[2] = statusDuration;
}
else if (statusEffect[3] == "NONE") { // Fourth
statusEffect[3] = appliedEffect;
statusEffectDuration[3] = statusDuration;
}
else if (statusEffect[4] == "NONE") { // Fifth
statusEffect[4] = appliedEffect;
statusEffectDuration[4] = statusDuration;
}
else if (statusEffect[5] == "NONE") { // Sixth
statusEffect[5] = appliedEffect;
statusEffectDuration[5] = statusDuration;
}
}
int main() {
applyStatusEffect("EFFECT", 5); // "EFFECT" is the effect that is given, 5 is duration of effect
// Displays status effect and effect duration
cout << statusEffect; // Displays 0x1000020f0
cout << "\n";
cout << statusEffectDuration; // Displays 0x1000020f0
return 0;
}
EDIT: I'm dumb. I was printing the address. But the problem with it giving the value to the first two empty spaces instead of one is the original reason I posted this.
You are telling the computer to print the address of the array.
To print the contents of the array, you'll need to use a loop:
for (i = 0; i < 5; ++i)
{
cout << statusEffect[i] << "\n";
}
I am writing some software to control a quadcopter and have got completely stuck with RtosTimers. I am getting the error "Error: No instance of constructor "rtos::RtosTimer::RtosTimer" matches the argument list in "flightController.h", Line: 13, Col: 29"
I have looked at the example code in the handbook and my code seems to match. I have also googled but I couldn't find anything on using RtosTimers inside of RtosThreads.
Maybe I am going about this the wrong way so if anyone has any suggestions it would be much appreciated.
Here is the code that is causing me problems
//Rtos Timers
RtosTimer UpdateFlightTimer(Task500Hz, osTimerPeriodic, (void *)0);
RtosTimer UpdateCommandTimer(Task50Hz, osTimerPeriodic, (void *)0);
// A thread to monitor the serial ports
void FlightControllerThread(void const *args)
{
UpdateFlightTimer.start(2);
UpdateCommandTimer.start(20);
// Wait here forever
Thread::wait(osWaitForever);
}
void Task500Hz(void const *n)
{
//Get IMU data and convert to yaw, pitch, roll
_freeIMU.getQ(_rawQuaternion);
_freeIMU.getRate(_gyroRate);
GetAttitude();
//Rate mode
if(_rate == true && _stab == false)
{
//Update rate PID process value with gyro rate
_yawRatePIDController->setProcessValue(_gyroRate[0]);
_pitchRatePIDController->setProcessValue(_gyroRate[2]);
_rollRatePIDController->setProcessValue(_gyroRate[1]);
//Update rate PID set point with desired rate from RC
_yawRatePIDController->setSetPoint(_rcConstrainedCommands[0]);
_pitchRatePIDController->setSetPoint(_rcConstrainedCommands[1]);
_rollRatePIDController->setSetPoint(_rcConstrainedCommands[2]);
//Compute rate PID outputs
_ratePIDControllerOutputs[0] = _yawRatePIDController->compute();
_ratePIDControllerOutputs[1] = _pitchRatePIDController->compute();
_ratePIDControllerOutputs[2] = _rollRatePIDController->compute();
}
//Stability mode
else
{
//Update stab PID process value with ypr
_yawStabPIDController->setProcessValue(_yrp[0]);
_pitchStabPIDController->setProcessValue(_yrp[2]);
_rollStabPIDController->setProcessValue(_yrp[1]);
//Update stab PID set point with desired angle from RC
_yawStabPIDController->setSetPoint(_yawTarget);
_pitchStabPIDController->setSetPoint(_rcConstrainedCommands[1]);
_rollStabPIDController->setSetPoint(_rcConstrainedCommands[2]);
//Compute stab PID outputs
_stabPIDControllerOutputs[0] = _yawStabPIDController->compute();
_stabPIDControllerOutputs[1] = _pitchStabPIDController->compute();
_stabPIDControllerOutputs[2] = _rollStabPIDController->compute();
//if pilot commanding yaw
if(abs(_rcConstrainedCommands[0]) > 5)
{
_stabPIDControllerOutputs[0] = _rcConstrainedCommands[0]; //Feed to rate PID (overwriting stab PID output)
_yawTarget = _yrp[0];
}
//Update rate PID process value with gyro rate
_yawRatePIDController->setProcessValue(_gyroRate[0]);
_pitchRatePIDController->setProcessValue(_gyroRate[2]);
_rollRatePIDController->setProcessValue(_gyroRate[1]);
//Update rate PID set point with desired rate from stab PID
_yawRatePIDController->setSetPoint(_stabPIDControllerOutputs[0]);
_pitchRatePIDController->setSetPoint(_stabPIDControllerOutputs[1]);
_rollRatePIDController->setSetPoint(_stabPIDControllerOutputs[2]);
//Compute rate PID outputs
_ratePIDControllerOutputs[0] = _yawRatePIDController->compute();
_ratePIDControllerOutputs[1] = _pitchRatePIDController->compute();
_ratePIDControllerOutputs[2] = _rollRatePIDController->compute();
}
//Calculate motor power if flying
if(_rcCommands[3] > 0 && _armed == true)
{
_motorPower[0] = Constrain(_rcConstrainedCommands[3] + _ratePIDControllerOutputs[1] + _ratePIDControllerOutputs[2] + _ratePIDControllerOutputs[0], MOTORS_MIN, MOTORS_MAX);
_motorPower[1] = Constrain(_rcConstrainedCommands[3] + _ratePIDControllerOutputs[1] - _ratePIDControllerOutputs[2] - _ratePIDControllerOutputs[0], MOTORS_MIN, MOTORS_MAX);
_motorPower[2] = Constrain(_rcConstrainedCommands[3] - _ratePIDControllerOutputs[1] - _ratePIDControllerOutputs[2] + _ratePIDControllerOutputs[0], MOTORS_MIN, MOTORS_MAX);
_motorPower[3] = Constrain(_rcConstrainedCommands[3] - _ratePIDControllerOutputs[1] + _ratePIDControllerOutputs[2] - _ratePIDControllerOutputs[0], MOTORS_MIN, MOTORS_MAX);
}
//Not flying
else
{
//Disable motors
_motorPower[0] = MOTORS_OFF;
_motorPower[1] = MOTORS_OFF;
_motorPower[2] = MOTORS_OFF;
_motorPower[3] = MOTORS_OFF;
_notFlying ++;
if(_notFlying > 200) //Not flying for 1 second
{
//Reset iteratior
_notFlying = 0;
//Zero gyro
_freeIMU.zeroGyro();
//Reset I
_yawRatePIDController->reset();
_pitchRatePIDController->reset();
_rollRatePIDController->reset();
_yawStabPIDController->reset();
_pitchStabPIDController->reset();
_rollStabPIDController->reset();
}
}
//Set motor power
_motor1.write(_motorPower[0]);
_motor2.write(_motorPower[1]);
_motor3.write(_motorPower[2]);
_motor4.write(_motorPower[3]);
}
void Task50Hz(void const *n)
{
//Get RC control values
//Constrain
//Rate mode
if(_rate == true && _stab == false)
{
_rcConstrainedCommands[0] = Constrain(_rcCommands[0], RC_YAW_RATE_MIN, RC_YAW_RATE_MAX);
_rcConstrainedCommands[1] = Constrain(_rcCommands[1], RC_PITCH_RATE_MIN, RC_PITCH_RATE_MAX);
_rcConstrainedCommands[2] = Constrain(_rcCommands[2], RC_ROLL_RATE_MIN, RC_ROLL_RATE_MAX);
_rcConstrainedCommands[3] = Constrain(_rcCommands[3], RC_THRUST_MIN, RC_THRUST_MAX);
}
else
{
_rcConstrainedCommands[0] = Constrain(_rcCommands[0], RC_YAW_RATE_MIN, RC_YAW_RATE_MAX);
_rcConstrainedCommands[1] = Constrain(_rcCommands[1], RC_PITCH_ANGLE_MIN, RC_PITCH_ANGLE_MAX);
_rcConstrainedCommands[2] = Constrain(_rcCommands[2], RC_ROLL_ANGLE_MIN, RC_ROLL_ANGLE_MAX);
_rcConstrainedCommands[3] = Constrain(_rcCommands[3], RC_THRUST_MIN, RC_THRUST_MAX);
}
}
My program can be found at http://mbed.org/users/joe4465/code/QuadMK5/
And the problem is in flightController.h I think it should be clear what I am trying to do but if anyone isn't sure let me know.
I also have another totally unrelated problem. I can set my PID variables over serial and then save them to a config file but 1 in 3 times if will hang just after it has saved the data to the file and I'm not sure why. Does anyone have any idea what could cause this?
Thanks Joe
I forgot to put void const *n in the parameters of the functions where they are defined at the top
I've written a simple, though highly multi-threaded, prime numbers generator.
The algorithm goes like this:
Thread 0: generates consecutive numbers.
Threads 1 .. N: filter out numbers that are not prime.
Upon each 'new' prime discovery, a new filter thread is added.
Take I: no flow control at all.
Thread 0 'send's numbers absolutely freely.
The program finishes with signal 11 (seg. fault), rarely signal 8, even more rarely finishes successfully.
Take II: flow control with 'setMaxMailboxSize' to 1.
Most of the time, everything works well.
Take III:
Now, if it all was a result of some internal unheld overflow, it should do well with 'setMaxMailboxSize' to 2 (or even 10), am I wrong ?
Thread 0 becomes stuck after it blocks for the first time.
Could someone please direct me what do I miss ?
Note 1:
I use DMD v2.053 under Ubuntu 10.04
Note 2:
This is my code:
#!/usr/bin/dmd -run
import std.stdio;
import std.conv;
import std.concurrency;
void main(string[] args)
{
/* parse command line arguments */
if (args.length < 2) {
writeln("Usage: prime <number of primes to generate>");
return;
}
auto nPrimes = to!int(args[1]);
auto tid = spawn(&generate, thisTid);
/* gather produced primes */
for (;;) {
auto prime = receiveOnly!int();
writeln(prime);
if (--nPrimes <= 0) {
break;
}
}
tid.send("stop");
}
void generate(Tid parentTid)
{
bool terminate = false;
// filter stage 1
auto tid = spawn(&filter_stage, parentTid);
/* WHAT DO I MISS HERE ? */
setMaxMailboxSize(tid, 1, OnCrowding.block);
for (int i = 2; !terminate; i++) {
receiveTimeout(0,
(string cmd) {
writeln(cmd);
terminate = true;
}
);
tid.send(i);
}
}
void filter_stage(Tid parentTid)
{
auto prime = receiveOnly!int();
parentTid.send(prime);
// filter stage 'N'
auto tid = spawn(&filter_stage, parentTid);
filter(prime, tid);
}
void filter(int prime, Tid tid)
{
for (;;) {
receive (
(int number) {
if (number % prime != 0) {
tid.send(number);
}
}
);
}
}
Sounds like a bug in std.concurrency. Try upgrading DMD to 2.055. I'm not sure if this specific bug is fixed but there are a lot of bug fixes between 2.053 and 2.055. If it's still broken then please file a bug report at http://d.puremagic.com/issues/.