Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In the PWM sample file, how would you change the direction? #46

Closed
MotorCityCobra opened this issue Jan 28, 2022 · 5 comments
Closed

In the PWM sample file, how would you change the direction? #46

MotorCityCobra opened this issue Jan 28, 2022 · 5 comments

Comments

@MotorCityCobra
Copy link

MotorCityCobra commented Jan 28, 2022

The script makes my motor go clockwise. How would I make it go counterclockwise?


#include <iostream>
// for delay function.
#include <chrono>
#include <map>
#include <string>
#include <thread>

// for signal handling
#include <signal.h>

#include <JetsonGPIO.h>

using namespace std;

inline void delay(int s) { this_thread::sleep_for(chrono::seconds(s)); }

static bool end_this_program = false;

void signalHandler(int s) { end_this_program = true; }

int main()
{
    // Pin Definitions
    int output_pin = 18;

    // When CTRL+C pressed, signalHandler will be called
    signal(SIGINT, signalHandler);

    // Pin Setup.
    // Board pin-numbering scheme
    GPIO::setmode(GPIO::BOARD);

    // set pin as an output pin with optional initial state of HIGH
    GPIO::setup(output_pin, GPIO::OUT, GPIO::HIGH);
    GPIO::PWM p(output_pin, 200);
    auto val = 75.0;
    p.start(val);

    cout << "PWM running. Press CTRL+C to exit." << endl;
    for (int i = 0; i < 4; i++){
        delay(1);
        cout << 1;
    }

    p.stop();
    GPIO::cleanup();

    return 0;
}













@pjueon
Copy link
Owner

pjueon commented Jan 28, 2022

The library only generates PWM signal.
How your motor moves completely depends on the motor you're using.

First check your motor(what kind of motor, model, etc) and check the data sheet of it.

@MotorCityCobra
Copy link
Author

I thought it might be something to do with the 'high' and 'low'.
I'm pretty sure it is a software option to change the direction of the motor. On a Raspberry Pi and Arduino IDE I can change the direction with code.

@pjueon
Copy link
Owner

pjueon commented Jan 28, 2022

There's no direction in pwm signal. Just frequency and duty cycle.
https://www.analogictips.com/pulse-width-modulation-pwm/

Can you give more details?

What Jetson device are you using?
What kind of motor are you using? (Servo, dc, step, etc)
Motor model?
How do you setup the circuit?

@MotorCityCobra
Copy link
Author

MotorCityCobra commented Jan 28, 2022

You're probably the most responsive repo owner on GitHub.

Nvidia Jetson AGX Xavier.
Nema 17 motor.
Driver is a Usongshine Stepper Motor Driver TB6600 4A 9-42V

I was just thinking about how I might need to change something with the driver's wiring.
I don't know how to wire it up. Right now I just have a wire in pull+ to the PWM pin and the pull- to the ground pin.

@MotorCityCobra
Copy link
Author

MotorCityCobra commented Jan 28, 2022

Okay, I got the motor running clockwise and then counterclockwise with the code below.
I also wired the driver differently. For the signal pins on the driver, all the + sockets are connected to each other and connected to my Jetson's 5V output.
The signal's dir - is connected to my pin 15.
The signal's pul - is connected to my pin 18.


#include <iostream>
// for delay function.
#include <chrono>
#include <map>
#include <string>
#include <thread>

// for signal handling
#include <signal.h>

#include <JetsonGPIO.h>

using namespace std;

inline void delay(int s) { this_thread::sleep_for(chrono::seconds(s)); }

static bool end_this_program = false;

void signalHandler(int s) { end_this_program = true; }

int main()
{
    // Pin Definitions
    int output_pin = 18;
    int direction_pin = 15;

    // When CTRL+C pressed, signalHandler will be called
    signal(SIGINT, signalHandler);

    // Pin Setup.
    // Board pin-numbering scheme
    GPIO::setmode(GPIO::BOARD);

    // set pin as an output pin with optional initial state of HIGH

    GPIO::setup(output_pin, GPIO::OUT, GPIO::HIGH);
    GPIO::PWM p(output_pin, 200); 
    GPIO::setup(direction_pin, GPIO::OUT, GPIO::LOW);
    GPIO::PWM e(direction_pin, 200);

    auto val = 75.0;
    e.start(val);
    p.start(val);

    cout << "PWM running. Press CTRL+C to exit." << endl;
    for (int i = 0; i < 4; i++){
        delay(1);
        cout << 1;
    }
    e.stop();
    
    for (int i = 0; i < 4; i++){
        delay(1);
        cout << 1;
    }

    
    p.stop();

    GPIO::cleanup();

    return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants