Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 3436

Troubleshooting • [Help!] Accurate signal frequency control at C++

$
0
0
I drive a stepper motor by writing code in C++ on a Raspberry Pi to generate pulses. However, when I run the code to drive the motor, I observe that the generated pulses are causing intermittent data spikes, resulting in jerky movements and noise from the motor.

What I want to achieve is the generation of a signal with a constant period of 50 microseconds. Since I have confirmed that there are no jerky movements when driving the motor with an Arduino's PWM signal under the same mechanical and electrical conditions, I think creating a signal with a constant period is quite challenging. I would greatly appreciate any suggestions for improvement.

The current code being executed is as follows:

Code:

#include <iostream>#include <pigpio.h>#include <unistd.h>#include <fstream>#include <chrono>#include <thread>#include <vector>void set_gpio_mode(int gpio_pin1, int gpio_pin2){gpioSetMode(gpio_pin1, PI_OUTPUT);    gpioSetMode(gpio_pin2, PI_OUTPUT);}void turn_high(int pin){gpioWrite(pin,1);}void turn_low(int pin){gpioWrite(pin,0);}int main(int argc, char* argv[]) {if (gpioInitialise() < 0){std::cerr << "Failure pigpio initialization" << std::endl;return 1;} int pin1 = std::stoi(argv[1]);int pin2 = std::stoi(argv[2]);int INTERVAL_MICROSECONDS = std::stoi(argv[3]);int count=0;std::vector<int> integers;    for (int i = 1; i < argc; ++i) {        integers.push_back(std::stoi(argv[i]));    }std::cout << "Array size: " << integers.size() << std::endl;    std::cout << "Array component: " << std::endl;    for (int i : integers) {        std::cout << i << " ";    }int turn_flag_M1 = 0;    int turn_flag_M2 = 0;set_gpio_mode(pin1,pin2);turn_low(pin1);turn_low(pin2);std::vector<std::chrono::duration<double>> period;int index=3;int iterate = integers[index];int tempindex = index;std::cout << "index: " << index << std::endl;std::cout << "iterate: " << iterate << std::endl;while (true) {iterate --;count++;    auto start = std::chrono::high_resolution_clock::now();if (turn_flag_M1) {turn_low(pin1);turn_flag_M1 = 0;}if (turn_flag_M2) {turn_low(pin2);turn_flag_M2 = 0; }if (tempindex != index) {if (index > integers.size()){break;}iterate = integers[index];tempindex = index;}while (true){auto end = std::chrono::high_resolution_clock::now();if (end - start > std::chrono::microseconds(INTERVAL_MICROSECONDS)*0.5) break;}std::cout << "iterate: " << iterate << std::endl;std::cout << "index: " << index << std::endl;if (iterate==0){turn_high(pin1);turn_high(pin2);turn_flag_M1 = 1;turn_flag_M2 = 1;index++;}        while (true){auto end = std::chrono::high_resolution_clock::now();std::chrono::duration<double> elapsed_seconds = end - start;if (elapsed_seconds > std::chrono::microseconds(INTERVAL_MICROSECONDS)){period.push_back(elapsed_seconds);// std::cout << sizeof(period) << std::endl;break ;};}    }std::ofstream outputFile("time_differences.txt");    if (outputFile.is_open()) {        for (int i = 0; i < count; ++i) {            outputFile << period[i].count() << std::endl;std::cout << i << std::endl;        }        outputFile.close();        std::cout << "Time difference was saved successfully." << std::endl;    } else {        std::cerr << "File cannot be opened." << std::endl;    }std::cerr << "Finish" << std::endl;gpioTerminate();return 0;}

Please check below result graph. X axis is iteration of above code and the Y axis is taken time for the iteration.
result.png

Statistics: Posted by samcoad — Thu Feb 01, 2024 5:35 am — Replies 1 — Views 75



Viewing all articles
Browse latest Browse all 3436

Trending Articles