CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

constrain.h library

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
wangine



Joined: 07 Jul 2009
Posts: 98
Location: Curtea de Arges, Romania

View user's profile Send private message Send e-mail Yahoo Messenger

constrain.h library
PostPosted: Wed Aug 10, 2011 1:46 pm     Reply with quote

Again a problem with structures.
The truth is that I'm not good at very little structure.
I wanted to use the library for constrain.h limits but without success.
I also found in the CCS or C30 depending only on Arduino.
I do not know how to do work in CCS.
I prefer to work and loan features that not only has to give the compiler.
Is there any way it could use in CCS?

Code:
constrain(K*(pTerm + iTerm + dTerm), -255, 255);


Code:
/*
||
|| @file Constrain.h
|| @version 1.2
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Provide an easy way of making constrained variables
|| #
||
*/

#ifndef CONSTRAIN_H
#define CONSTRAIN_H

//#include "WProgram.h"

template< typename T >
struct Constrained {
    Constrained( T v , T mn , T mx ) : value(v),minimum(mn),maximum(mx) {}
    T value; //hold the current value of the constrained variable
    T minimum; //minimum value for this variable
    T maximum; //maximum value for this variable
    T &operator=(T set){
        if (set<minimum){
            value = minimum;
        }else if (set>maximum){
            value = maximum;
        }else if (set<=maximum && set>=minimum){
            value = set;
        }
        return value;
    }
    void operator=( Constrained& right ){
        value = right.value;
        minimum = right.minimum;
        maximum = right.maximum;
    }
    Constrained &operator++(){
        if (value+1<=maximum){ value++; }
        return *this;
    }
    Constrained operator++( int ){
        if (value+1<=maximum){ value++; }
    }
    Constrained &operator--(){
        if (value-1>=minimum){ value--; }
        return *this;
    }
    Constrained operator--( int ){
        if (value-1>=minimum){ value--; }
    }
    const Constrained &operator+=( T val){
        value += val;
        value = constrain( value, minimum, maximum );
        return *this;
    }
    const Constrained &operator-=( T val){
        value -= val;
        value = constrain( value, minimum, maximum );
        return *this;
    }
   bool operator<( const T i ) const{
      return value < i;
   }
   bool operator<=( const T i ) const{
      return !(value > i);
   }
   bool operator>( const T i ) const{
      return value > i;
   }
   bool operator>=( const T i ) const{
      return !(value < i);
   }
   
   bool operator==( const T i ) const{
      return value == i;
   }

   operator T() { return value; }
   operator int() { return value; }
   operator byte() { return value; }
   operator char() { return value; }
   operator long() { return value; }
}; //end Constrained

#endif

/*
|| @changelog
|| | 1.2 2009-05-01 - Alexander Brevig : Added boolean comarison operators
|| | 1.1 2009-05-01 - Alexander Brevig : Added conversion operators
|| | 1.0 2009-04-21 - Alexander Brevig : Initial Release
|| #
*/

Thank you very much
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Thu Aug 11, 2011 3:11 am     Reply with quote

This is a C++ header. It uses templates and operator overloading which are not in C. There is no way to do this in CCS which is a C compiler, not a C++ compiler.

Sorry Sad

RF_Developer
wangine



Joined: 07 Jul 2009
Posts: 98
Location: Curtea de Arges, Romania

View user's profile Send private message Send e-mail Yahoo Messenger

PostPosted: Thu Aug 11, 2011 10:40 am     Reply with quote

wow
And do not think is any way to convert this function in C?
I do not know C++ and are a little on the outside.
Thanks
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Fri Aug 12, 2011 2:01 am     Reply with quote

"Provide an easy way of making constrained variables "

It is possible to get similar results, but it wouldn't be anywhere as "easy".
Templates are a powerful C++ feature and allow generic code to be defined that works on any compatible type and in this instance with any parameters (the constraint limits). The C++ compiler sorts out all the code you need. In C you have to write all the variations of the generic code yourself.


In C, you would have to use ordinary variables of the types you need and a set of functions to replace the operators. You couldn't just write c = a + b;, instead you'd have to do something like c = Constrained_Add(a, b) and have different versions of Constrained_Add for every combination of variable types and constraints you wanted to use. Though CCS does have overloaded functions that make that simpler, and deal with the alternative types more easily. That wouldn't take care of the different contraints however. Overloads are not C, they are "borrowed" from C++.

Anyway, its possible, but a lot of work, and no longer "easy" Crying or Very sad

The alternative is to think about why this template is needed, and how its used in the application, and write alternative code to perform the specific functionality you actually need.

RF_Developer
wangine



Joined: 07 Jul 2009
Posts: 98
Location: Curtea de Arges, Romania

View user's profile Send private message Send e-mail Yahoo Messenger

PostPosted: Fri Aug 12, 2011 10:00 am     Reply with quote

Thanks for the answer first.
I think I made below is more or less enough for what I need me.
I still want to know if indeed those who did well and returns like the other.
We put extra "step" considering that me and you reach the maximum decrement variable.
I want an opinion from you.

Code:

float constrain(float val, float min, float max, float step)
   {

        if (val<min){
            val = min;
        }else if (val > max){
            val = max;
        }else if (val <= max && val >= min){
//            val = con.value;
         break;
        }
//~~~~~~~~~~~~~~~~~~~~~~
        if (val+1 <=max ){ val += step; }

        if (val-1 >=min ){ val -= step; }   
   

return val;
   }


Code:

 x =  constrain(value, -2080, 2080, 1.5f);

And I have a question
example
Code:
float x = 1.503f
means that the operations of these variables are the maximum of three decimal places?
were wrong?

Thanks again
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group