void operator()( void )
    {

        Stepper stepper;
        const int o = stepper.order()+1; //order of the error is order of approximation + 1

        const state_type x0 = {{ 0.0 , 1.0 }};
        state_type x1;
        const double t = 0.0;
        /* do a first step with dt=0.1 to get an estimate on the prefactor of the error dx = f * dt^(order+1) */
        double dt = 0.5;
        stepper.do_step( osc() , x0 , t , x1 , dt );
        const double f = 2.0 * std::abs( sin(dt) - x1[0] ) / std::pow( dt , o ); // upper bound

        std::cout << o << " , " << f << std::endl;

        /* as long as we have errors above machine precision */
        while( f*std::pow( dt , o ) > 1E-16 )
        {
            // reset stepper which require resetting (fsal steppers)
            resetter< typename Stepper::stepper_category >::reset( stepper );

            stepper.do_step( osc() , x0 , t , x1 , dt );
            std::cout << "Testing dt=" << dt << std::endl;
            BOOST_CHECK_LT( std::abs( sin(dt) - x1[0] ) , f*std::pow( dt , o ) );
            dt *= 0.5;
        }
    }
void run( Stepper &stepper , const size_t num_of_steps = 20000000 , const double dt = 1E-10 )
{
    const size_t loops = 20;

    accumulator_type acc;
    timer_type timer;

    srand( 12312354 );

    // transient
    //stepper.reset_init_cond( );
    //for( size_t i = 0 ; i < num_of_steps ; ++i )
    //    stepper.do_step( dt );

    for( size_t n=0 ; n<loops+1 ; ++n )
    {
        stepper.reset_init_cond( );

        timer.restart();
        for( size_t i = 0 ; i < num_of_steps ; ++i )
            stepper.do_step( dt );
        if( n>0 )
        {   // take first run as transient
            acc(timer.elapsed());
            clog.precision(8);
            clog.width(10);
            clog << acc << " " << stepper.state(0) << endl;
        }
    }
    cout << acc << endl;
}
Example #3
0
void check_stepper( Stepper &stepper )
{
    typedef Stepper stepper_type;
    typedef typename stepper_type::state_type state_type;
    typedef typename stepper_type::value_type value_type;
    typedef typename stepper_type::deriv_type deriv_type;
    typedef typename stepper_type::time_type time_type;
    typedef typename stepper_type::order_type order_type;
    typedef typename stepper_type::algebra_type algebra_type;
    typedef typename stepper_type::operations_type operations_type;

    const time_type t( 0.0 * si::second );
    time_type dt( 0.1 * si::second );
    state_type x( 1.0 * si::meter , 0.0 * si::meter_per_second );

    // test call method one
    stepper.do_step( oscillator , x , t , dt );

    // test call method two
    stepper.do_step( oscillator , x , t , x , dt );

    // test call method three
    deriv_type dxdt;
    oscillator( x , dxdt , t );
    stepper.do_step( oscillator , x , dxdt , t , dt );

    // test call method four
    oscillator( x , dxdt , t );
    stepper.do_step( oscillator , x , dxdt , t , x , dt );
}
Example #4
0
    void operator()( void )
    {
   
        Stepper stepper;
        const int o = stepper.order()+1; //order of the error is order of approximation + 1

        const state_type q0 = {{ 0.0 }};
        const state_type p0 = {{ 1.0 }};
        state_type q1,p1;
        std::pair< state_type , state_type >x1( q1 , p1 );
        const double t = 0.0;
        /* do a first step with dt=0.1 to get an estimate on the prefactor of the error dx = f * dt^(order+1) */
        double dt = 0.5;
        stepper.do_step( osc() , std::make_pair( q0 , p0 ) , t , x1 , dt );
        const double f = 2.0 * std::abs( sin(dt) - x1.first[0] ) / std::pow( dt , o );

        std::cout << o << " , " << f << std::endl;

        /* as long as we have errors above machine precision */
        while( f*std::pow( dt , o ) > 1E-16 )
        {
            stepper.do_step( osc() , std::make_pair( q0 , p0 ) , t , x1 , dt );
            std::cout << "Testing dt=" << dt << std::endl;
            BOOST_CHECK_SMALL( std::abs( sin(dt) - x1.first[0] ) , f*std::pow( dt , o ) );
            dt *= 0.5;
        }
    }
void check_error_stepper_concept( Stepper &stepper , System system , typename Stepper::state_type &x , typename Stepper::state_type &xerr )
{
    typedef Stepper stepper_type;
    typedef typename stepper_type::deriv_type container_type;
    typedef typename stepper_type::order_type order_type;
    typedef typename stepper_type::time_type time_type;

    stepper.do_step( system , x , static_cast<time_type>(0.0) , static_cast<time_type>(0.1) );
    stepper.do_step( system , x , static_cast<time_type>(0.0) , static_cast<time_type>(0.1) , xerr );
}
void check_error_stepper_concept( Stepper &stepper , System system , typename Stepper::state_type &x , typename Stepper::state_type &xerr )
{
    typedef Stepper stepper_type;
    typedef typename stepper_type::deriv_type container_type;
    typedef typename stepper_type::order_type order_type;
    typedef typename stepper_type::time_type time_type;

    stepper.do_step( system , typename boost::add_reference< container_type>::type( x ), 0.0 , 0.1 );
    stepper.do_step( system , typename boost::add_reference< container_type>::type( x ), 0.0 , 0.1 ,  typename boost::add_reference< container_type>::type( xerr ) );

}
Example #7
0
void addStepper(){
	_MOTOR1_EN = 0;
	_MOTOR2_EN = 0;
	_STEPPER_EN = 1;
	pinMode(SLIDESW1_D1,INPUT);
	pinMode(SLIDESW1_D2,INPUT);
	pinMode(TACTILESW1, INPUT);
	pinMode(TACTILESW2, INPUT);
	stepper.attachStepper(stepsPerRevolution, STEPPER_D1, STEPPER_D2,
			STEPPER_D3, STEPPER_D4);
	stepper.setSpeed(60);
	actionAdd(controlStepper, 1);
}
    void operator()( void )
    {
        double t = 0;
        const double dt = 0.1;

        state_type x = 0;

        Stepper stepper;
        InitStepper init_stepper;
        stepper.initialize( init_stepper, rhs, x, t, dt ); 

        // ab-stepper needs order-1 init steps: t and x should be (order-1)*dt
        BOOST_CHECK_CLOSE( t , (stepper.order()-1)*dt , 1E-16 );
        BOOST_CHECK_CLOSE( x, ( stepper.order() - 1 ) * dt, 2E-14 );
    }
size_t integrate_adaptive(
        Stepper stepper , System system , State &start_state ,
        Time &start_time , Time end_time , Time &dt ,
        Observer observer , controlled_stepper_tag
)
{
    typename omplext_odeint::unwrap_reference< Observer >::type &obs = observer;

    const size_t max_attempts = 1000;
    const char *error_string = "Integrate adaptive : Maximal number of iterations reached. A step size could not be found.";
    size_t count = 0;
    while( less_with_sign( start_time , end_time , dt ) )
    {
        obs( start_state , start_time );
        if( less_with_sign( end_time , start_time + dt , dt ) )
        {
            dt = end_time - start_time;
        }

        size_t trials = 0;
        controlled_step_result res = success;
        do
        {
            res = stepper.try_step( system , start_state , start_time , dt );
            ++trials;
        }
        while( ( res == fail ) && ( trials < max_attempts ) );
        if( trials == max_attempts ) throw std::overflow_error( error_string );

        ++count;
    }
    obs( start_state , start_time );
    return count;
}
Example #10
0
 //This is a basic operation
 bool process(Direction direction, Stepper &sm)
 {
     switch (direction)
     {
     case Forward:
         sm.changeState(state + step);
         break;
     case Backward:
         sm.changeState(state - step);
         break;
     default:
         return false;
         break;
     }
     return true;
 }
void setup()
{

  Serial.begin(9600);
  stepper.setSpeed(30);

}
Example #12
0
void loop()
{
      val=encoder/faktor;
 
      if (val < minimum)
            {val=minimum;
             encoder=minimum*faktor;}
    
      if (val > maximum)
            {val=maximum;
             encoder=maximum*faktor;}

  if (val !=val_old)
   { 
    // Serial.print (encoder);
    // Serial.print (" ");
    // Serial.print (val);
    // Serial.print (" ");
     
      val_old=val;
      
         sprintf(buffer,"%3d",val);
         digitalWrite(stepper_enable, LOW);
        
         lcd.setCursor(10, 2);
         lcd.print(buffer);
       
       //lcd.cursorTo(1, 10); 
      // lcd.printIn(buffer);
      
        stepper.step(100);

   } 


digitalWrite(stepper_enable, HIGH);
          if (abs (val - stepper_position) >10)
          { lcd.setCursor(19, 3);
            lcd.blink();}
         
          stepper.step(val - stepper_position);
         
          lcd.noBlink();
   
digitalWrite(stepper_enable, LOW);
stepper_position=val;
}
Example #13
0
/*!
Returns propagated state and max relative errors in
kinetic energy and magnetic moment of particle.
*/
template<class Stepper> std::tuple<state_t, double, double> trace_trajectory(
	state_t state,
	const double time_step
) {
	using std::fabs;
	using std::max;

	constexpr double propagation_time = 424.1; // seconds, 1/100 of doi above

	Particle_Propagator propagator(proton_charge_mass_ratio);

	const double
		initial_energy = 0.5 * proton_mass * state[1].squaredNorm(),
		initial_magnetic_moment
			= proton_mass
			* std::pow(state[1][1], 2)
			/ (2 * get_earth_dipole(state[0]).norm());

	Stepper stepper;
	double nrj_error = 0, mom_error = 0;
	for (double time = 0; time < propagation_time; time += time_step) {
		stepper.do_step(propagator, state, time, time_step);

		const Eigen::Vector3d
			v(state[1]),
			b(get_earth_dipole(state[0])),
			v_perp_b(v - (v.dot(b) / b.squaredNorm()) * b);

		const double
			current_energy
				= 0.5 * proton_mass * state[1].squaredNorm(),
			current_magnetic_moment
				= proton_mass * v_perp_b.squaredNorm() / (2 * b.norm());

		nrj_error = max(
			nrj_error,
			fabs(current_energy - initial_energy) / initial_energy
		);
		mom_error = max(
			mom_error,
			fabs(current_magnetic_moment - initial_magnetic_moment)
				/ initial_magnetic_moment
		);
	}

	return std::make_tuple(state, nrj_error, mom_error);
}
Example #14
0
void check_dense_output_stepper( Stepper &stepper )
{
    typedef Stepper stepper_type;
    typedef typename stepper_type::state_type state_type;
    typedef typename stepper_type::value_type value_type;
    typedef typename stepper_type::deriv_type deriv_type;
    typedef typename stepper_type::time_type time_type;
//    typedef typename stepper_type::order_type order_type;

    time_type t( 0.0 * si::second );
    time_type dt( 0.1 * si::second );
    state_type x( 1.0 * si::meter , 0.0 * si::meter_per_second ) , x2;

    stepper.initialize( x , t , dt );
    stepper.do_step( oscillator );
    stepper.calc_state( dt / 2.0 , x2 );
}
Example #15
0
void check_stepper_concept( Stepper &stepper , System system , typename Stepper::deriv_type &x )
{
    typedef Stepper stepper_type;
    typedef typename stepper_type::deriv_type container_type;
    typedef typename stepper_type::order_type order_type;
    typedef typename stepper_type::time_type time_type;

    stepper.do_step( system , x , 0.0 , 0.1 );
}
Example #16
0
void loop()
{
      val=encoder/faktor;
 
      if (val < minimum)
            {val=minimum;
             encoder=minimum*faktor;}
    
      if (val > maximum)
            {val=maximum;
             encoder=maximum*faktor;}

  if (val !=val_old)
   { 
     sprintf(buffer,"%3d",val);    
     digitalWrite(stepper_enable, LOW);
     lcd.setCursor(10, 2);
     lcd.print(buffer);
     val_old=val;
     
     digitalWrite(stepper_enable, HIGH);

     steps=val - stepper_position;
     stepper.step(1);
     stepper_position=val;
    } 
    else
    {digitalWrite(stepper_enable, LOW);}





   

   

      
     digitalWrite(stepper_enable, HIGH);
     stepper.step(1);

}
Example #17
0
    void operator()( void )
    {
        /* We have to specify the desired precision in advance! */
        mpf_set_default_prec( precision );

        mpf_t eps_ , unity;
        mpf_init( eps_ ); mpf_init( unity );
        mpf_set_d( unity , 1.0 );
        mpf_div_2exp( eps_ , unity , precision-1 ); // 2^(-precision+1) : smallest number that can be represented with used precision
        value_type eps( eps_ );

        Stepper stepper;
        state_type x;
        x = 0.0;

        stepper.do_step( constant_system , x , 0.0 , 0.1 );

        BOOST_MESSAGE( eps );
        BOOST_CHECK_MESSAGE( abs( x - value_type( 0.1 , precision ) ) < eps , x - 0.1 );
    }
void loop()
{

  val = analogRead(1);

  if (val > forward_threshold) {
    stepper.step(val - forward_threshold); 
  }
  else if (val < reverse_threshold) {
    stepper.step(-(reverse_threshold - val));
  }
  else{
    
  }

  Serial.print("val: ");
  Serial.print(val);
  Serial.println("          ");

}
Example #19
0
 void loop() { 


   n = digitalRead(encoder0PinA);
   if ((encoder0PinALast == LOW) && (n == HIGH)  ) {
     if ((digitalRead(encoder0PinB) == LOW) && (encoder0Pos > encoder_min)) {
        

         digitalWrite(stepper_enable, HIGH);
         encoder0Pos--;
         stepper.step(-1);
    

     
   } else {
   
 if  (encoder0Pos < encoder_max)    {
         digitalWrite(stepper_enable, HIGH);
         encoder0Pos++;
         stepper.step(1);
 }
   
     }


    Serial.print (encoder0Pos);
    Serial.print ("/");
    digitalWrite(stepper_enable, LOW);

   lcd.setCursor(0, 2);
   lcd.print("Position:");
   sprintf(buffer,"%4d",encoder0Pos);
   lcd.setCursor(10, 2);
   lcd.print(buffer);
        
  } 
   encoder0PinALast = n;
   
   
   
 } 
void check_controlled_stepper_concept( Stepper &stepper , System system , typename Stepper::state_type &x )
{
    typedef Stepper stepper_type;
    typedef typename stepper_type::deriv_type container_type;
    //typedef typename stepper_type::order_type order_type;  controlled_error_stepper don't necessarily have a order (burlish-stoer)
    typedef typename stepper_type::time_type time_type;

    time_type t = 0.0 , dt = 0.1;
    controlled_step_result step_result = stepper.try_step( system , x , t , dt );

    BOOST_CHECK_MESSAGE( step_result == success , "step result: " << step_result ); // error = 0 for constant system -> step size is always too small
}
Example #21
0
void controlStepper()
{	//Serial.println("StepperCtrl");
	//stepper.setSpeed(pot1.getValue()/10);
	if (tactileSw1.read()){
		Serial.println("sw1");
		if(slideSw1.readPin1())
			stepper.step(1);
		else {if (slideSw1.readPin2())
			stepper.step(-1);}
		delay(100);
	}
	else if (tactileSw2.read()){
			Serial.println("sw2");
			if(slideSw1.readPin1())
				stepper.step(10);
			else {if (slideSw1.readPin2())
				stepper.step(-10);}
			delay(100);
	}

}
Example #22
0
    void operator()( void )
    {
        mpf_set_default_prec( precision );

        mpf_t eps_ , unity;
        mpf_init( eps_ ); mpf_init( unity );
        mpf_set_d( unity , 1.0 );
        mpf_div_2exp( eps_ , unity , precision-1 ); // 2^(-precision+1) : smallest number that can be represented with used precision
        value_type eps( eps_ );

        Stepper stepper;
        state_type x;
        x = 0.0;

        value_type t(0.0);
        value_type dt(0.1);

        stepper.try_step( constant_system , x , t , dt );

        BOOST_MESSAGE( eps );
        BOOST_CHECK_MESSAGE( abs( x - value_type( 0.1 , precision ) ) < eps , x - 0.1 );
    }
    typename Stepper::time_type integrate_const_steps(
	Stepper &stepper ,
	DynamicalSystem &system ,
	typename Stepper::container_type &state ,
	typename Stepper::time_type start_time ,
	typename Stepper::time_type dt ,
	size_t num_of_steps ,
	Observer &observer
	)
    {
        stepper.adjust_size( state );
	size_t iteration = 0;
        while( iteration < num_of_steps )
	{
	    observer( start_time , state , system );
            stepper.do_step( system , state , start_time , dt );
            start_time += dt;
	    ++iteration;
        }
	observer( start_time , state , system );

	return start_time;
    }
Example #24
0
void check_controlled_stepper( Stepper &stepper )
{
    typedef Stepper stepper_type;
    typedef typename stepper_type::state_type state_type;
    typedef typename stepper_type::value_type value_type;
    typedef typename stepper_type::deriv_type deriv_type;
    typedef typename stepper_type::time_type time_type;

    time_type t( 0.0 * si::second );
    time_type dt( 0.1 * si::second );
    state_type x( 1.0 * si::meter , 0.0 * si::meter_per_second );

    // test call method one
    stepper.try_step( oscillator , x , t , dt );
}
void setup()
{

  // begin the serial communication
  Serial.begin(9600);

  pinMode(m1, OUTPUT);
  pinMode(m2, OUTPUT);
  pinMode(m3, OUTPUT);
  pinMode(m4, OUTPUT);
  pinMode(led, OUTPUT);

  // set the speed of the motor to 30 RPMs
  stepper.setSpeed(30);
}
size_t integrate_adaptive(
        Stepper stepper , System system , State &start_state ,
        Time start_time , Time end_time , Time dt ,
        Observer observer , stepper_tag
)
{
    size_t steps = static_cast< size_t >( (end_time-start_time)/dt );
    Time end = detail::integrate_n_steps( stepper , system , start_state , start_time ,
                                          dt , steps , observer , stepper_tag() );
    if( less_with_sign( end , end_time , dt ) )
    {   //make a last step to end exactly at end_time
        stepper.do_step( system , start_state , end , end_time - end );
        steps++;
        typename omplext_odeint::unwrap_reference< Observer >::type &obs = observer;
        obs( start_state , end_time );
    }
    return steps;
}
Example #27
0
size_t integrate_adaptive(
        Stepper stepper , System system , State &start_state ,
        Time start_time , Time end_time , Time dt ,
        Observer observer , stepper_tag
)
{
    size_t steps = boost::numeric::odeint::detail::integrate_const( 
                       stepper , system , start_state , start_time , 
                       end_time , dt , observer , stepper_tag() );
    if( steps*dt < end_time )
    {   //make a last step to end exactly at end_time
        stepper.do_step( system , start_state , steps*dt , end_time-steps*dt );
        steps++;
        typename boost::unwrap_reference< Observer >::type &obs = observer;
        obs( start_state , end_time );
    }
    return steps;
}
Example #28
0
    void operator()( void )
    {
        Stepper stepper;
        initializing_stepper init_stepper;
        const int o = stepper.order()+1; //order of the error is order of approximation + 1

        const state_type x0 = {{ 0.0 , 1.0 }};
        state_type x1 = x0;
        double t = 0.0;
        double dt = 0.2;
        // initialization, does a number of steps already to fill internal buffer, t is increased
        // we use the rk78 as initializing stepper
        stepper.initialize( boost::ref(init_stepper) , osc() , x1 , t , dt );
        double A = std::sqrt( x1[0]*x1[0] + x1[1]*x1[1] );
        double phi = std::asin(x1[0]/A) - t;
        // do a number of steps to fill the buffer with results from adams bashforth
        for( size_t n=0 ; n < stepper.steps ; ++n )
        {
            stepper.do_step( osc() , x1 , t , dt );
            t += dt;
        }
        // now we do the actual step
        stepper.do_step( osc() , x1 , t , dt );
        // only examine the error of the adams-bashforth step, not the initialization
        const double f = 2.0 * std::abs( A*sin(t+dt+phi) - x1[0] ) / std::pow( dt , o ); // upper bound
        
        std::cout << o << " , " << f << std::endl;

        /* as long as we have errors above machine precision */
        while( f*std::pow( dt , o ) > 1E-16 )
        {
            x1 = x0;
            t = 0.0;
            stepper.initialize( boost::ref(init_stepper) , osc() , x1 , t , dt );
            A = std::sqrt( x1[0]*x1[0] + x1[1]*x1[1] );
            phi = std::asin(x1[0]/A) - t;
            // now we do the actual step
            stepper.do_step( osc() , x1 , t , dt );
            // only examine the error of the adams-bashforth step, not the initialization
            std::cout << "Testing dt=" << dt << " , " << std::abs( A*sin(t+dt+phi) - x1[0] ) << std::endl;
            BOOST_CHECK_LT( std::abs( A*sin(t+dt+phi) - x1[0] ) , f*std::pow( dt , o ) );
            dt *= 0.5;
        }
    }
Example #29
0
 void setup() { 
 
   
   pinMode (encoder0PinA,INPUT);
   pinMode (encoder0PinB,INPUT);
   pinMode (stepper_enable, OUTPUT);
   Serial.begin (9600);
   stepper.setSpeed(250);
   digitalWrite(stepper_enable, HIGH);
   
   lcd.begin(20, 4);
   lcd.clear();
   lcd.print("Stepper, Vers. 1.0");
  
  
  
  
  
 
 } 
Time integrate_n_steps(
        Stepper stepper , System system , State &start_state ,
        Time start_time , Time dt , size_t num_of_steps ,
        Observer observer , stepper_tag )
{
    typename odeint::unwrap_reference< Observer >::type &obs = observer;

    Time time = start_time;

    for( size_t step = 0; step < num_of_steps ; ++step )
    {
        obs( start_state , time );
        stepper.do_step( system , start_state , time , dt );
        // direct computation of the time avoids error propagation happening when using time += dt
        // we need clumsy type analysis to get boost units working here
        time = start_time + static_cast< typename unit_value_type<Time>::type >( step+1 ) * dt;
    }
    obs( start_state , time );

    return time;
}