← Back to team overview

nrtb-core team mailing list archive

[Merge] lp:~fpstovall/nrtb/ricks-sprint-004 into lp:nrtb

 

Rick Stovall has proposed merging lp:~fpstovall/nrtb/ricks-sprint-004 into lp:nrtb.

Requested reviews:
  NRTB Core (nrtb-core): code

For more details, see:
https://code.launchpad.net/~fpstovall/nrtb/ricks-sprint-004/+merge/201312

Created new base_object class. Unit tested and ready to merge.
-- 
https://code.launchpad.net/~fpstovall/nrtb/ricks-sprint-004/+merge/201312
Your team NRTB Core is requested to review the proposed merge of lp:~fpstovall/nrtb/ricks-sprint-004 into lp:nrtb.
=== modified file 'cpp/sim_engine/Makefile'
--- cpp/sim_engine/Makefile	2013-11-15 16:58:04 +0000
+++ cpp/sim_engine/Makefile	2014-01-12 17:49:46 +0000
@@ -37,6 +37,7 @@
 doit:
 	cd messages; make ${action}
 	cd main; make ${action}
+	cd base_object; make ${action}
 #	cd comm_manager; make ${action}
 
 ../common/nrtb_common.a:

=== added directory 'cpp/sim_engine/base_object'
=== added file 'cpp/sim_engine/base_object/Makefile'
--- cpp/sim_engine/base_object/Makefile	1970-01-01 00:00:00 +0000
+++ cpp/sim_engine/base_object/Makefile	2014-01-12 17:49:46 +0000
@@ -0,0 +1,47 @@
+#***********************************************
+# This file is part of the NRTB project (https://launchpad.net/nrtb).
+#
+#    NRTB 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.
+#
+#    NRTB 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 NRTB.  If not, see <http://www.gnu.org/licenses/>.
+#
+#***********************************************
+
+target=base_object
+
+lib:	${target}_test
+	@./${target}_test
+	@cp -v ${target}.h ../include/
+	@cp -v ${target}.o ../obj/
+	@echo build complete
+
+${target}_test:	${target}.o ${target}_test.cpp
+	@rm -f ${target}_test
+	g++ -c -O3 ${target}_test.cpp -I ../include ${bargs} 
+	g++ -o ${target}_test ${target}_test.o ${target}.o ${largs}
+
+
+${target}.o:	${target}.cpp ${target}.h Makefile
+	@rm -f ${target}.o
+	g++ -c -O3 ${target}.cpp -I ../include ${bargs} 
+
+clean:
+	@rm -vf *.o ../include/${target}.h ../obj/${target}.o ${target}_test
+	@echo all objects and executables have been erased.
+
+switches=-std=gnu++11 -D _GLIBCXX_USE_SCHED_YIELD -D _GLIBCXX_USE_NANOSLEEP 
+libs=../../common/lib/nrtb_common.a -lpthread
+include=-I../../common/include -I../include
+bargs=${switches} ${include}
+largs=${switches} ${libs}
+
+

=== added file 'cpp/sim_engine/base_object/base_object.cpp'
--- cpp/sim_engine/base_object/base_object.cpp	1970-01-01 00:00:00 +0000
+++ cpp/sim_engine/base_object/base_object.cpp	2014-01-12 17:49:46 +0000
@@ -0,0 +1,112 @@
+/***********************************************
+ This file is part of the NRTB project (https://*launchpad.net/nrtb).
+ 
+ NRTB 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.
+ 
+ NRTB 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 NRTB.  If not, see <http://www.gnu.org/licenses/>.
+ 
+ **********************************************/
+
+// see base_socket.h for documentation
+
+#include "base_object.h"
+#include <sstream>
+
+using namespace nrtb;
+
+serializer abs_effector::effector_num;
+serializer base_object::object_num;
+
+std::string base_object::as_str()
+{
+  std::stringstream returnme;
+  returnme << "ID=" << id
+    << ":loc=" << location
+    << ":att=" << attitude
+    << ":vel=" << velocity
+    << ":rot=" << rotation
+    << ":f=" << force
+    << ":t=" << torque
+    << ":acc_mod=" << accel_mod
+    << ":t_mod=" << torque_mod
+    << ":mass=" << mass
+    << ":mass_mod=" << mass_mod
+    << ":b_sphere=" << bounding_sphere.center
+    << "," << bounding_sphere.radius
+    << ":pre=";
+  for(auto a : pre_attribs)
+    returnme << a.second->as_str() << ";";
+  returnme << ":posts=";
+  for(auto a : post_attribs)
+    returnme << a.second->as_str() << ";";
+  return returnme.str();
+};
+
+bool base_object::tick(int time)
+{
+  // clean up for next pass
+  accel_mod = 0;
+  torque_mod = 0;
+  force = 0;
+  torque = 0;
+  mass_mod = 0;
+  bool killme (false);
+  for (auto e : pre_attribs)
+    if (e.second->tick(*this, time))
+      killme = true;
+  return killme;
+};
+
+bool base_object::apply(int time, float quanta)
+{
+  // move acording to forces
+  float tmass = mass + mass_mod;
+  triplet a = force / tmass;
+  triplet ra = torque / (tmass * 0.5); // not accurate!!
+  velocity += (a * quanta) + (accel_mod * quanta);
+  rotation += (ra * quanta) + (torque_mod * quanta);
+  location += velocity * quanta;
+  attitude += rotation * quanta;
+  // apply post-effectors
+  bool killme (false);
+  for (auto e : post_attribs)
+    if (e.second->tick(*this, time))
+      killme = true;
+  return killme;
+};
+
+bool base_object::check_collision(sphere s)
+{
+  float r = s.radius + bounding_sphere.radius;
+  return r <= 
+    s.center.range(bounding_sphere.center+location);
+};
+
+void base_object::add_pre(abs_effector* e)
+{
+  pre_attribs[e->id] = effector_p(e);
+};
+
+abs_effector& base_object::get_pre(long long unsigned int i)
+{
+  return *pre_attribs[i];
+};
+
+void base_object::add_post(abs_effector* e)
+{
+  post_attribs[e->id] = effector_p(e);
+};
+
+abs_effector& base_object::get_post(long long unsigned int i)
+{
+  return *post_attribs[i];
+};

=== added file 'cpp/sim_engine/base_object/base_object.h'
--- cpp/sim_engine/base_object/base_object.h	1970-01-01 00:00:00 +0000
+++ cpp/sim_engine/base_object/base_object.h	2014-01-12 17:49:46 +0000
@@ -0,0 +1,89 @@
+/***********************************************
+ This file is part of the NRTB project (https://*launchpad.net/nrtb).
+ 
+ NRTB 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.
+ 
+ NRTB 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 NRTB.  If not, see <http://www.gnu.org/licenses/>.
+ 
+ **********************************************/
+ 
+#ifndef base_object_header
+#define base_object_header
+
+#include <triad.h>
+#include <serializer.h>
+#include <map>
+#include <memory>
+
+namespace nrtb
+{
+  
+typedef triad<float> triplet;
+
+struct sphere
+{
+  triplet center;
+  float radius;
+};
+
+struct base_object;
+typedef std::shared_ptr<base_object> object_p;
+typedef std::map<unsigned long long, object_p> object_list;
+
+struct abs_effector
+{
+  static serializer effector_num;
+  unsigned long long id = effector_num();
+  std::string handle;
+  virtual std::string as_str() = 0;
+  virtual bool tick(base_object & o, int time) = 0;
+};
+  
+typedef std::shared_ptr<abs_effector> effector_p;
+typedef std::map<unsigned long long, effector_p> effector_list;
+
+struct base_object
+{
+  static serializer object_num;
+  // data
+  unsigned long long id = object_num();
+  std::string handle;
+  triplet location;
+  triplet attitude;
+  triplet velocity;
+  triplet rotation;
+  triplet force;
+  triplet torque;
+  triplet accel_mod;
+  triplet torque_mod;
+  float mass;
+  float mass_mod;
+  sphere bounding_sphere;
+  // reporting
+  virtual std::string as_str();
+  // effector management
+  virtual void add_pre(abs_effector * e);
+  virtual abs_effector & get_pre(unsigned long long i);
+  virtual void add_post(abs_effector * e);
+  virtual abs_effector & get_post(unsigned long long i);
+  // sim methods
+  virtual bool tick(int time);
+  virtual bool apply(int time, float quanta);
+  virtual bool check_collision(sphere s);
+protected:
+  effector_list pre_attribs;
+  effector_list post_attribs;    
+};
+
+} // namepace nrtb
+
+#endif // base_object_header

=== added file 'cpp/sim_engine/base_object/base_object_test.cpp'
--- cpp/sim_engine/base_object/base_object_test.cpp	1970-01-01 00:00:00 +0000
+++ cpp/sim_engine/base_object/base_object_test.cpp	2014-01-12 17:49:46 +0000
@@ -0,0 +1,158 @@
+/***********************************************
+ This file is part of the NRTB project (https://*launchpad.net/nrtb).
+ 
+ NRTB 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.
+ 
+ NRTB 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 NRTB.  If not, see <http://www.gnu.org/licenses/>.
+ 
+ **********************************************/
+
+#include "base_object.h"
+#include <iostream>
+#include <string>
+
+using namespace nrtb;
+using namespace std;
+
+struct gravity : public abs_effector
+{
+  triplet g = triplet(0,-9.81,0);
+  
+  gravity()
+  {
+    handle = "gravity";
+  };
+  
+  std::string as_str()
+  {
+    std::stringstream returnme;
+    returnme << handle << "_" << id << "=" << g;
+    return returnme.str();
+  };
+  
+  bool tick(base_object & o, int time)
+  {
+    o.accel_mod += g;
+    return false;
+  };
+};
+
+struct rocket : public abs_effector
+{
+  triplet impulse = triplet(0.0,10000.0,0.0);
+  int burn_time = 3;
+  
+  std::string as_str()
+  {
+    std::stringstream returnme;
+    returnme << "rocket_" << id << "=" << impulse;
+    return returnme.str();
+  };
+  
+  bool tick(base_object & o, int time)
+  {
+    if (time <= burn_time)
+    {
+      o.force += impulse;
+    }
+    else
+    {
+      impulse = 0;
+    };
+    return false;
+  };  
+};
+
+int main()
+{
+  bool failed = false;
+  cout << "=========== sim messages test ============="
+    << endl;
+
+  cout << "Object setup:" << endl;
+  base_object rocket_ball;
+  rocket_ball.mass = 100;
+  rocket_ball.bounding_sphere.center = triplet(0);
+  rocket_ball.bounding_sphere.radius = 0.5;
+  cout << rocket_ball.as_str() << endl;
+  
+  rocket_ball.add_pre(new gravity);
+  cout << rocket_ball.as_str() << endl;
+  
+  rocket_ball.add_pre(new rocket);
+  cout << rocket_ball.as_str() << endl;  
+  
+  cout << "Launch:" << endl;
+  int time = 0;
+  // initial burn.
+  for (time; time<5; time++)
+  {
+    cout << time*0.02 << " sec."<< endl;
+    rocket_ball.tick(time);
+    rocket_ball.apply(time,0.02);
+    cout << rocket_ball.as_str() << endl;
+  };
+  
+  // coast to peak altitude
+  while (rocket_ball.velocity.y > 0.0)
+  {
+    time++;
+    rocket_ball.tick(time);
+    rocket_ball.apply(time,0.02);    
+  };
+  cout << "Peak:" << time*0.02 << " sec."<< endl;
+  cout << rocket_ball.as_str() << endl;
+  failed = failed or (time != 41);
+
+  // Fall back down.
+  while (rocket_ball.location.y > 0.0)
+  {
+    time++;
+    rocket_ball.tick(time);
+    rocket_ball.apply(time,0.02);    
+  };
+  cout << "Impact:" << time*0.02<< " sec." << endl;
+  cout << rocket_ball.as_str() << endl;
+  failed = failed or (time != 80);
+
+  cout << "=========== sim_messages test complete ============="
+    << endl;
+  
+  return failed;
+};
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+


Follow ups