0

I'm quite a beginner in C++, for a robotic project, I'm creating a program to control several motors, and also get their speed.

For getting the speed, I have a "code wheel" installed on the motors which gives a 0/1 signal to a pin of the raspberry depending on the motor speed.

The function receives the number of segments the code wheel got (I have different ones) and should return the motor speed. This activate through an interruption with wiringPiISR to avoid having a loop constantly checking on it.

As there is several motors to be controlled, I need this code to be repeated as many times as there is motors. Therefore, I created a class file with the function in it :

#include <iostream> //for Cout to display stuff
#include <chrono>   //library for counting time
#include <thread>   //for multithreading
#include <vector>
#include <wiringPi.h>   //Raspberry pi GPIO Library
#include "RPMCounter01.h"

RPMCounter::RPMCounter(){
    //int CodeWheelSegments = 0;
    int MNbSegments = 0, MNbSegmentsSeen = 0, MResult = 0;  //counter for the total number of segments
    //int MNbSegmentsSeen = 0;  //counter for the number of segments that have been seen
    //int MResult = 0;  //Result of the speed measurement the motort should turn at 220 rpm 
    std::chrono::duration<double> TotalResult;  //define TotalResult as nanoseconds in chrono library initialized at 0
    std::chrono::steady_clock::time_point BeginMeasurement; //chrono variable representing the beginning of the measurement of a motor speed
    double Freq = 0;    //frequency of the motor
}

void RPMCounter::FreqCounter(int CodeWheelSegments){
    using namespace std::chrono;

    duration<double> InstaResult = steady_clock::now() - BeginMeasurement;  //result of the time needed between 2 segments
    MNbSegments +=1;    //increasing the segment counter
    BeginMeasurement = steady_clock::now(); //start of a new cycle
    //std::cout << "BeginMeasurement = " << BeginMeasurement << '\n';
    if (InstaResult < milliseconds{100}){   //counting the time of impulsions if it's not bugged
        TotalResult = TotalResult + InstaResult;
        MNbSegmentsSeen += 1;
    }
    if (MNbSegments >= CodeWheelSegments){  //when the wheel did one turn
        //std::cout << "TotalResult = " << TotalResult.count() << '\n';
        Freq=TotalResult.count();   
        Freq = 60/((Freq/MNbSegmentsSeen)*CodeWheelSegments);   //calculating the frequency of the motor = 1/(Total time for all seen segments / Nb of segments see) * nb of segments
        std::cout << "Frequency = " << Freq << " RPM" << '\n';      //DIsplays the frequency of the motor
        //return Freq;
        MNbSegments = 0;            //reset all the parameters
        MNbSegmentsSeen = 0;
        TotalResult = 0s;
        //BeginMeasurement = steady_clock::now();
    }
}

the header file

#ifndef DEF_RPMCounter
#define DEF_RPMCounter

class RPMCounter{
    public:
        RPMCounter();   //Constructeur
        void FreqCounter(int CodeWheelSegments);
    private:
        std::chrono::duration<double> TotalResult;  //define TotalResult as nanoseconds in chrono library initialized at 0
        std::chrono::steady_clock::time_point BeginMeasurement; //chrono variable representing the beginning of the measurement of a motor speed
        int CodeWheelSegments;
        int MNbSegments;    //counter for the total number of segments
        int MNbSegmentsSeen;    //counter for the number of segments that have been seen
        int MResult;    //Result of the speed measurement the motort should turn at 220 rpm 
        double Freq;    //frequency of the motor
};
#endif

In theory, this should work.

I'm calling it through these lines :

//int wiringPiISR (int pin, int edgeType,  void (*function)(void)) ;
    RPMCounter RPMPouet;    //create an object of type RPMCounter
    wiringPiISR (Motor0In, INT_EDGE_RISING, RPMPouet.FreqCounter(36)); //create the interruption on the GPIO input nb "Motor0In" in case of rising edge for counting RPM

which returns an error:

RobotControl03.cpp:133:69: error: invalid use of void expression
     wiringPiISR (Motor0In, INT_EDGE_RISING, RPMPouet.FreqCounter(36)); //create the interruption on the GPIO input nb "Motor0In" in case of rising edge for counting RPM

What am I doing wrong in this function call?

I tried several different ways, without success and the documentation about this wiringPiISR is quite inexistant...

Thanks by advance !

zepeu
  • 81
  • 2
  • 16
  • I suggest you add information about the error. – EvilTeach Mar 09 '20 at 15:32
  • Did you see the documentation of `wiringPiISR` [here](http://wiringpi.com/reference/priority-interrupts-and-threads/)? If so, what part of this is unclear to you? – Cubic Mar 09 '20 at 15:35
  • @EvilTeach Done, sorry :/ – zepeu Mar 09 '20 at 17:59
  • @Cubic Well, basically, how can I call an object in a class in another .cpp file to act for the interruption? – zepeu Mar 09 '20 at 18:01
  • Best guess is that RPMPouet.FreqCounter does not return a value, and so can't be used in the wiringPiISR call. – EvilTeach Mar 09 '20 at 18:39
  • @EvilTeach Look at about the end, there is this line : `//return Freq;` I was forced to comment it as it created more errors :( – zepeu Mar 09 '20 at 20:19

0 Answers0