Rotate DC Gear Motor by degrees

Supporting Java SE version 7 and up
Post Reply
izibizi666
Phidgetsian
Posts: 5
Joined: Wed May 13, 2020 5:46 am
Contact:

Rotate DC Gear Motor by degrees

Post by izibizi666 »

Hi.

I have the DC Gear Motor 24V/5.1Kg-cm/588RPM 4.25:1 DC Gear Motor (version 3269_3) and the encoder HKT22. Now I want to create a Java application to rotate the motor by given degrees. The user can enter e.g. 60 degrees or e.g. -60 degrees.

I create my own example code and the motor is rotating for a given time (e.g. 5000 ms).

But I´m not sure how can I manage it to rotate the motor by given degrees. I think I have to calculate something.

In my later application I would have every second a other degree and I want do move the motor every second e.g. -10 degrees or +25 degrees.

I want to do something like autosteering. The motor should rotate the steering wheel.

With which classes can I manage this?


Code: Select all


DCMotor dcMotor0 = new DCMotor();
Encoder encoder0 = new Encoder();

dcMotor0.open(1000);
encoder0.open(5000);         
            
dcMotor0.setTargetVelocity(0.2);

encoder0.addPositionChangeListener(new EncoderPositionChangeListener() 
  {
    public void onPositionChange(EncoderPositionChangeEvent e) 
					
					
    System.out.println("PositionChange: " + e.getPositionChange());
    System.out.println("TimeChange: " + e.getTimeChange());					 
    System.out.println("IndexTriggered: " + e.getIndexTriggered());
    System.out.println("----------");
	
  }
  });

			
Thread.sleep(5000);
dcMotor0.close();
encoder0.close();
User avatar
mparadis
Site Admin
Posts: 660
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Rotate DC Gear Motor by degrees

Post by mparadis »

Which motor controller are you using? If you're using one of our newer ones like the DCC1000-DCC1003 series, you can use the MotorPositionController class instead of the DCMotor class.

In the video, it mentions setting the rescale factor- this is how you can change the target position so that it is measured in degrees. Our video on rescale factor uses a stepper motor as an example, so instead here's an excerpt from the DCC1000 user guide explaining how to calculate rescale for a DC motor and encoder:
There are three pieces of information to consider when setting a rescale factor to change your units into degrees or rotations:

[*]Your encoder's CPR (counts per rotation)
[*]Your encoder interface's resolution
[*]Your motor's gear ratio

First, check your encoder's datasheet for the CPR. It's usually 360 or 300. This is the number of quadrature cycles the encoder will send out for one full rotation.

Next, you need your encoder interface's resolution. The encoder port on the DCC1000 has a x4 resolution, meaning it reads in 4 pulses per quadrature cycle (see the Encoder Primer for a more in-depth explanation).

Next, you need to find out the gear ratio in your motor's datasheet. Note: If you plan on having your motor run for many rotations in a row, try to find the exact gear ratio, expressed as a fraction. Using the rounded value will result in accumulating errors the more you rotate.

Once you have these numbers, you can calculate the rescale factor:

Rescale(degrees) = 360 / (CPR x EncoderRes x GearRatio)

For example, if you wanted to have your motor's position measured in degrees and your encoder had 300 CPR and your motor had a 50 801⁄895 : 1 gearbox, you would set your rescale factor to 360 / 300*4*(50+(801/895)), or 0.005894.
You can test out your rescale factor using the Phidget Control Panel example (the program featured in the video) first, and then once you've confirmed that it's working you can check the API documentation to see what methods you'll need to use in order to implement the MotorPositionController in your own program.
izibizi666
Phidgetsian
Posts: 5
Joined: Wed May 13, 2020 5:46 am
Contact:

Re: Rotate DC Gear Motor by degrees

Post by izibizi666 »

Hi,

I´m using the PhidgetMotorControl 1-Motor (version 1065_1B) with heat sink.

Thank you and best regards.
User avatar
mparadis
Site Admin
Posts: 660
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Rotate DC Gear Motor by degrees

Post by mparadis »

The 1065 doesn't support the MotorPositionController object, but you can still implement your own basic control loop using the DCMotor and Encoder objects. Here's how I would go about it:

- find out how many encoder ticks will correspond to 1 degree of rotation on your motor (use the information I quoted in my post above to determine this)

- make a global variable to keep track of the target encoder position

- In the onPositionChange function, if the current position is less than the target position, set a positive DCMotor duty cycle. If the current position is greater than the target position, set a negative duty cycle.

- Once you get this working, you can tweak it so that it moves faster if it's far away from the target

- You can also add a "deadband" i.e. instead of a single target position, there is a small acceptable range of positions that count as your target. This will prevent the motor from buzzing back and forth when it hits the target.

For a more in-depth look at making a full PID control loop, you can also have a look at this article.
izibizi666
Phidgetsian
Posts: 5
Joined: Wed May 13, 2020 5:46 am
Contact:

Re: Rotate DC Gear Motor by degrees

Post by izibizi666 »

Thank you for your answer.

I tried to implement the example in my JAVA project but I could not manage it. I think the mentioned example is not complete because I can´t see the usage of different variables (e.g. feedbacklast) or the implementation of the method distance360(input,feedback).

To set the positive/negative DCMotor duty cycle you mean to set the velocity? Within the java api I have only the method setTargetVelocity. I think I have to use this method?

Sorry for my silly questions but I´m very new in this environment.

Is there a JAVA example where I can see how the logic has to be implemented?
User avatar
mparadis
Site Admin
Posts: 660
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Rotate DC Gear Motor by degrees

Post by mparadis »

The project that I linked has a download for the complete visual studio project where you can see the code for some of those other functions. That being said, it's written using the Phidget21 libraries rather than the newer Phidget22 so there will be some differences in the API that could be confusing (mostly having to do with how Phidgets are opened and addressed).

Yes, duty cycle is the same as velocity. We don't have any java exampled for this specific application, but once you understand the control loop logic it's mostly going to be if statements and loops, nothing too language-specific. Let me know if you have any other questions about how the control loop works.

Edit: Be sure to check out the "Referencing Other Phidgets from Events" section of this page to see how change the DCMotor velocity from inside the Encoder event in Java.
izibizi666
Phidgetsian
Posts: 5
Joined: Wed May 13, 2020 5:46 am
Contact:

Re: Rotate DC Gear Motor by degrees

Post by izibizi666 »

@mparadis: Thank you for your answer. I think I managed it.

But now I have another question.
With my application I can count the ticks of the motor. But when I restart the application the tick counter is set to 0. So If I have e.g. a car and the steering wheel is turned 10 degrees to the left I have a tick counter of 0 when I start my application. But the tick counter should have the current vlaue of the motor position. I think it is not possible to store the tick counter in a database or file because if the motor or steering wheel is turned during the application is not running then the tick counter is not correct.

Is there a possibility or method which always return the current position of the motor?
izibizi666
Phidgetsian
Posts: 5
Joined: Wed May 13, 2020 5:46 am
Contact:

Re: Rotate DC Gear Motor by degrees

Post by izibizi666 »

I still not solved this issue. Has someone a answer for me. :wink:
User avatar
mparadis
Site Admin
Posts: 660
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Rotate DC Gear Motor by degrees

Post by mparadis »

You need to have some way to determining the absolute position when your program starts. This could involve placing a physical limit switch somewhere on the wheel that is pushed when the wheel hits its minimum or maximum rotation, or when it crosses the middle position. If the motor moves on startup until it hits one of these switches, you can determine absolute position and then use the incremental position from the encoder until the next power cycle. If physical switches are not feasible for this solution, you could use a magnetic sensor or magnetic reed switch and a magnet at your position location.
Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests