Browse files

Joystick added, working on improving car visaulization.

  • Loading branch information...
1 parent f6359dd commit a246ab308cce58e5a51a44dfac0645a0257ca46d @jgoppert jgoppert committed Apr 5, 2012
View
18 CMakeLists.txt
@@ -383,6 +383,9 @@ if(WITH_JOYSTICK)
src/jsinput.h
src/jssuper.h
)
+list(APPEND ARKSCICOS_BLOCKS
+ joystick
+ )
endif()
# mavlink blocks
@@ -496,13 +499,14 @@ set(TOOLBOX_DEPS
if (APPLE)
message(STATUS "apple scicoslab command-line does not function, please run builde.sce using the ScicosLab app before packaging with cmake")
else()
- list(APPEND TOOLBOX_DEPS "arkscicos-toolbox/scicos/arkscicos/arkscicos.cosf")
+ list(APPEND TOOLBOX_DEPS "toolbox-build-stamp")
endif()
-add_custom_command(OUTPUT "arkscicos-toolbox/scicos/arkscicos/arkscicos.cosf"
+add_custom_command(OUTPUT "toolbox-build-stamp"
COMMAND "${CMAKE_COMMAND}" -E chdir "${CMAKE_BINARY_DIR}/arkscicos-toolbox"
${SCICOSLAB_SCILAB_PROGRAM} "-nb" "-nwni" "-e" "\"exec('builder.sce');exit\""
- COMMAND "${CMAKE_COMMAND}" -E touch "toolbox-stamp"
+ COMMAND "${CMAKE_COMMAND}" -E touch toolbox-build-stamp
+ DEPENDS arkscicos
)
add_custom_target(toolbox ALL DEPENDS ${TOOLBOX_DEPS})
@@ -522,6 +526,14 @@ install(DIRECTORY "${PROJECT_BINARY_DIR}/arkscicos-toolbox"
PATTERN "*.cvs*" EXCLUDE
)
+# tests
+if (WITH_TESTING)
+ if (WITH_JOYSTICK)
+ add_executable(joystick src/js_demo.cxx)
+ target_link_libraries(joystick ${PLIB_LIBRARIES})
+ endif()
+endif()
+
# packaging
if (WIN32)
# set NSIS image, nsis bug requuires atleast one file with 4 slashes to install correctly
View
BIN arkscicos-toolbox/demos/block/carJoystick.cos
Binary file not shown.
View
BIN arkscicos-toolbox/demos/block/joystick.cos
Binary file not shown.
View
3 arkscicos-toolbox/demos/blockDemos.sce
@@ -13,6 +13,7 @@ n=x_choose([
'Sailboat Autopilot';
'Digtal PID Controller w/ Low Pass Filter';
'Digtal PID Controller w/ Derivative Feedback';
+'Joystick Demo';
],'arkscicos demos');
if (n==1)
scicos(arkscicosPath+'demos/block/JSBSimBackside.cos');
@@ -42,6 +43,8 @@ elseif (n==13)
scicos(arkscicosPath+'demos/block/PidDLP.cos');
elseif (n==14)
scicos(arkscicosPath+'demos/block/PidDFB.cos');
+elseif (n==15)
+ scicos(arkscicosPath+'demos/block/joystick.cos');
else
disp('unknown demo');
end
View
22 arkscicos-toolbox/scicos/arkscicos/car.sci
@@ -28,7 +28,7 @@ function [x,y,typ]=car(job,arg1,arg2)
// You should have received a copy of the GNU General Public License along
// with this program. If not, see <http://www.gnu.org/licenses/>.
//
-mode(-1)
+mode(-1);
x=[];y=[];typ=[];
select job
case 'plot' then
@@ -39,21 +39,22 @@ select job
[x,y,typ]=standard_outputs(arg1)
case 'getorigin' then
[x,y]=standard_origin(arg1)
- case 'set' then
+ case 'set' then
x=arg1;
graphics=arg1.graphics;exprs=graphics.exprs
model=arg1.model;
while %t do
labels=[..
- 'car model'];
- [ok,ModelPath,exprs]=..
- getvalue('Set Car Parameters',labels,..
- list('str',-1),exprs);
+ 'car model';'ground texture'];
+ [ok,ModelPath,TexturePath,exprs]=..
+ getvalue('Set Quad Parameters',labels,..
+ list('str',-1,'str',-1),exprs);
if ~ok then break,end
[model,graphics,ok]=check_io(model,graphics,6,[],1,[])
if ok then
model.ipar=[..
- length(evstr(ModelPath)),ascii(evstr(ModelPath)),0];
+ length(evstr(ModelPath)),ascii(evstr(ModelPath)),0,..
+ length(evstr(TexturePath)),ascii(evstr(TexturePath)),0];
graphics.exprs=exprs;
x.graphics=graphics;
x.model=model;
@@ -72,16 +73,19 @@ select job
// jsbsim parameters
ModelPath="arkscicosPath+""/data/arkosg/models/rcTruck.ac""";
+ TexturePath="arkscicosPath+""/data/arkosg/images/lz.rgb""";
model.ipar=[..
- length(evstr(ModelPath)),ascii(evstr(ModelPath)),0];
+ length(evstr(ModelPath)),ascii(evstr(ModelPath)),0,..
+ length(evstr(TexturePath)),ascii(evstr(TexturePath)),0];
// intial state
// save state
// initialize strings for gui
exprs=[
- strcat(ModelPath)];
+ strcat(ModelPath),..
+ strcat(TexturePath)];
//setup icon
gr_i=['xstringb(orig(1),orig(2),..
View
87 arkscicos-toolbox/scicos/arkscicos/joystick.sci
@@ -0,0 +1,87 @@
+function [x,y,typ]=joystick(job,arg1,arg2)
+//
+// joystick.sci
+//
+// USAGE:
+//
+// output:
+// vector of axis values
+//
+// input:
+// user connected joystick
+//
+// Options:
+//
+// port: the port of the joystick
+//
+// AUTHOR:
+//
+// Copyright (C) James Goppert 2012 <jgoppert@users.sourceforge.net>
+//
+// joystick.sci 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.
+//
+// joystick.sci 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 <http://www.gnu.org/licenses/>.
+//
+mode(-1);
+x=[];y=[];typ=[];
+
+select job
+ case 'plot' then
+ standard_draw(arg1)
+ case 'getinputs' then
+ [x,y,typ]=standard_inputs(arg1)
+ case 'getoutputs' then
+ [x,y,typ]=standard_outputs(arg1)
+ case 'getorigin' then
+ [x,y]=standard_origin(arg1)
+ case 'set' then
+ x=arg1;
+ graphics=arg1.graphics;exprs=graphics.exprs
+ model=arg1.model;
+ while %t do
+ labels=['port number'];
+ [ok,portNumber,exprs]=..
+ getvalue('Set Joystick Parameters',labels,..
+ list('vec',1),exprs);
+ if ~ok then break,end
+ model.out=[10];
+ [model,graphics,ok]=check_io(model,graphics,[],model.out,[],[])
+ if ok then
+ model.ipar=[portNumber];
+ graphics.exprs=exprs;
+ x.graphics=graphics;
+ x.model=model;
+ break
+ end
+ end
+ case 'define' then
+ // set model properties
+ model=scicos_model()
+ model.sim=list('sci_joystick',4)
+ model.out=[10]
+ model.blocktype='c'
+ model.dep_ut=[%f %t]
+
+ // jsbsim parameters
+ portNumber=0;
+ model.ipar=[portNumber];
+
+ // initialize strings for gui
+ exprs=[strcat(sci2exp(portNumber))];
+
+ // setup icon
+ gr_i=['xstringb(orig(1),orig(2),''joystick'',sz(1),sz(2),''fill'');']
+ x=standard_define([5 2],model,exprs,gr_i)
+ end
+endfunction
+
+// vim:ts=4:sw=4
View
8 src/sci_car.cpp
@@ -33,9 +33,10 @@ class VisCar : public Viewer
public:
Car * car;
- VisCar(char* model) : car(new Car(std::string(model)))
+ VisCar(char* model, char * texture) : car(new Car(std::string(model)))
{
osg::Group * root = new Frame(1,"N","E","D");
+ root->addChild(new Terrain(std::string(texture),osg::Vec3(10,10,0)));
if (car) root->addChild(car);
getCameraManipulator()->setHomePosition(osg::Vec3(-3,3,-3),
osg::Vec3(0,0,0),osg::Vec3(0,0,-1));
@@ -65,15 +66,16 @@ extern "C"
int * ipar=block->ipar;
char ** stringArray;
int * intArray;
- getIpars(1,0,ipar,&stringArray,&intArray);
+ getIpars(2,0,ipar,&stringArray,&intArray);
char * model = stringArray[0];
+ char * texture = stringArray[1];
// handle flags
if (flag==scicos::initialize)
{
try
{
- vis = new VisCar(model);
+ vis = new VisCar(model,texture);
}
catch (const std::runtime_error & e)
{
View
181 src/sci_joystick.cpp
@@ -0,0 +1,181 @@
+/*
+ * sci_joystick.cpp
+ * Copyright (C) James Goppert 2010 <jgoppert@users.sourceforge.net>
+ *
+ * sci_joystick.cpp 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.
+ *
+ * sci_joystick.cpp 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 <http://www.gnu.org/licenses/>.
+ *
+ * y: vector of axis values
+ */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#ifdef HAVE_WINDOWS_H
+# include <windows.h>
+#endif
+
+#include <string.h> // plib/js.h should really include this !!!!!!
+#include <plib/js.h>
+#include <stdexcept>
+#include <iostream>
+
+class Joystick {
+public:
+ Joystick(int portNumber) : _joystick(NULL), _portNumber(0) {
+
+ // set the port number
+ setPortNumber(portNumber);
+
+ // initialize js library
+ if (!_jsInitialized) {
+ jsInit();
+ _jsInitialized = true;
+ }
+
+ // allocate joystick
+ _joystick = new jsJoystick(getPortNumber());
+
+ // check joystick is functional
+ checkJoystick();
+ }
+ void read(double * y) {
+ // check joystick is functional
+ checkJoystick();
+
+ // read value
+ int buttons = 0;
+ float * values = new float[getNumAxes()]();
+ _joystick->read ( &buttons, values);
+ for (int i=0; i<getNumAxes();i++) {
+ y[i] = values[i];
+ }
+ }
+ int getNumAxes() {
+ return _joystick->getNumAxes();
+ }
+ int getPortNumber() {
+ return _portNumber;
+ }
+private:
+ static bool _jsInitialized;
+ jsJoystick * _joystick;
+ int _portNumber;
+ void checkJoystick() {
+ char message[50];
+ // check joystick is functional
+ if (!_joystick) {
+ sprintf(message,"failed to allocate joystick #%i",_portNumber);
+ throw std::runtime_error(message);
+ return;
+ } else if(_joystick->notWorking()) {
+ sprintf(message,"unable to connect to joystick #%i",_portNumber);
+ throw std::runtime_error(message);
+ return;
+ }
+ }
+ void setPortNumber(int portNumber) {
+ if (portNumber<0) {
+ char message[50];
+ sprintf(message,"joystick port number cannot be negative, attempted to set to: %i",portNumber);
+ _portNumber = 0;
+ throw std::runtime_error(message);
+ return;
+ }
+ _portNumber = portNumber;
+ }
+};
+bool Joystick::_jsInitialized = false;
+
+
+extern "C"
+{
+
+#include <scicos/scicos_block4.h>
+#include <math.h>
+#include "definitions.hpp"
+
+ void sci_joystick(scicos_block *block, scicos::enumScicosFlags flag)
+ {
+ // data
+ double *y=(double*)GetOutPortPtrs(block,1);
+ void ** work = GetPtrWorkPtrs(block);
+ int * ipar=block->ipar;
+ int * intArray;
+ int portNumber = ipar[0];
+ Joystick * joystick = NULL;
+
+ //handle flags
+ if (flag==scicos::initialize)
+ {
+ //std::cout << "initializing" << std::endl;
+ try
+ {
+ // initialize
+ joystick = new Joystick(portNumber);
+ }
+ catch (const std::exception & e)
+ {
+ std::cout << "exception: " << e.what() << std::endl;
+ Coserror((char *)e.what());
+ return;
+ }
+ catch (...)
+ {
+ Coserror((char *)"unknown error");
+ return;
+ }
+ *work = (void *)joystick;
+ }
+ else if (flag==scicos::terminate)
+ {
+ //std::cout << "terminating" << std::endl;
+ joystick = (Joystick *)*work;
+ if (joystick)
+ {
+ delete joystick;
+ joystick = NULL;
+ }
+ }
+ else if (flag==scicos::computeOutput)
+ {
+ //std::cout << "computing output" << std::endl;
+ joystick = (Joystick *)*work;
+ try {
+ joystick->read(y);
+ } catch (const std::exception & e)
+ {
+ std::cout << "exception: " << e.what() << std::endl;
+ Coserror((char *)e.what());
+ return;
+ }
+ catch (...)
+ {
+ Coserror((char *)"unknown error");
+ return;
+ }
+ }
+ else
+ {
+ //std::cout << "unhandled flag: " << flag << std::endl;
+ }
+ }
+
+} // extern c
+
+// vim:ts=4:sw=4
+
+
+
+// vim:ts=4:sw=4

0 comments on commit a246ab3

Please sign in to comment.