Go to Post My grades were horrid when I started, but thanks to the mentors and other students, I'm now a straight-A student. - Bax2996 [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rating: Thread Rating: 12 votes, 5.00 average. Display Modes
  #1   Spotlight this post!  
Unread 13-11-2013, 01:30
Hypnotoad's Avatar
Hypnotoad Hypnotoad is offline
Junior Assistant Secretary of Moon
no team
Team Role: Programmer
 
Join Date: Feb 2012
Rookie Year: 2011
Location: California
Posts: 114
Hypnotoad is infamous around these partsHypnotoad is infamous around these partsHypnotoad is infamous around these partsHypnotoad is infamous around these partsHypnotoad is infamous around these partsHypnotoad is infamous around these parts
Running pid loops in a separate thread.

I was wondering if anyone ran their pid loops separately from the main code in a different thread so as to get a higher refresh frequency. I fact, is multithreading even possible on the crio? I know that you can run separate loops in labview but I have read that the context switches eat the CPU. Is it possible in java?
__________________
The following sentence is true.
The preceding sentence is false.
This sentence is false.
This signature is false.
My teeth are false.
Reply With Quote
  #2   Spotlight this post!  
Unread 13-11-2013, 01:42
Anupam Goli's Avatar
Anupam Goli Anupam Goli is offline
PCH Q&A co-founder/Scouting Mentor
AKA: noops
FRC #1648 (G3 Robotics)
Team Role: Mentor
 
Join Date: Dec 2010
Rookie Year: 2008
Location: Atlanta, Georgia
Posts: 1,242
Anupam Goli has a reputation beyond reputeAnupam Goli has a reputation beyond reputeAnupam Goli has a reputation beyond reputeAnupam Goli has a reputation beyond reputeAnupam Goli has a reputation beyond reputeAnupam Goli has a reputation beyond reputeAnupam Goli has a reputation beyond reputeAnupam Goli has a reputation beyond reputeAnupam Goli has a reputation beyond reputeAnupam Goli has a reputation beyond reputeAnupam Goli has a reputation beyond repute
Re: Running pid loops in a separate thread.

Multithreading is possible on the cRIO, and in fact, multiple teams have utilized multithreading in their code. I know in C++ you can use pthreads to perform multithreading. I believe in the java api, you either have your class extend runnable or Threads. I'm not 100% sure on that, but you can check out the api to see what you can use. Just be careful with your stack and the memory. In Java, dealing with the stack and memory when multithreading is much simpler than in C, but you still have all of the usual caveats. In C++ you had to make sure certain variables were locked and unlocked when needed unless they were mutexed.
__________________
Team 1002: 2008-2012
Team 1648: 2012-2016
Georgia Tech Class of 2016
Reply With Quote
  #3   Spotlight this post!  
Unread 13-11-2013, 04:47
Ginto8's Avatar
Ginto8 Ginto8 is offline
Programming Lead
AKA: Joe Doyle
FRC #2729 (Storm)
Team Role: Programmer
 
Join Date: Oct 2010
Rookie Year: 2010
Location: Marlton, NJ
Posts: 174
Ginto8 is a glorious beacon of lightGinto8 is a glorious beacon of lightGinto8 is a glorious beacon of lightGinto8 is a glorious beacon of lightGinto8 is a glorious beacon of light
Re: Running pid loops in a separate thread.

In Java at least, threads are actually pretty easy to use. I usually prefer using a TimerTask than an explicit Thread though. In terms of PID, if you look at PIDController's source, that is exactly what it does -- it's already threaded.
__________________
I code stuff.
Reply With Quote
  #4   Spotlight this post!  
Unread 14-11-2013, 02:05
TravSatEE's Avatar
TravSatEE TravSatEE is offline
Spacecraft Engineer and more
FRC #2035 (Robo Rockin' Bots)
Team Role: Engineer
 
Join Date: Jan 2012
Rookie Year: 2002
Location: Monterey, CA
Posts: 26
TravSatEE is infamous around these partsTravSatEE is infamous around these parts
Re: Running pid loops in a separate thread.

Quote:
Originally Posted by Hypnotoad View Post
I was wondering if anyone ran their pid loops separately from the main code in a different thread so as to get a higher refresh frequency.
The WPILIB implementation for java already uses threads for each PID controller. (See PIDCommand). However, the threads still have to lock internally with the Squawk JVM, so that field communication can occur. I don't know that you can improve your sampling rate. Can you clarify why you think you need a higher sampling/control rate? Are you having a stability issue? Your parameter tuning likely needs to be adjusted, but no change to sampling rate should be necessary.

Quote:
Originally Posted by Hypnotoad View Post
I fact, is multithreading even possible on the crio? I know that you can run separate loops in labview but I have read that the context switches eat the CPU. Is it possible in java?
The cRIO has a PowerPC processor and does run a real time operating system. So threads do exist for this configuration. Context switching is too low level to consider for FRC robotics -- the overhead is insignificant. This is not the cause of your issues with the PID controller.
__________________
I have a doctoral degree in electrical engineering. My FIRST mentoring philosophy is to encourage student-led activities and create a level playing field among all teams. I believe this approach results in an exciting game, rather than emphasis on a handful of dominant teams.

FIRST FRC Teams that I have mentored: 612, 342, 2035, and 5104. FIRST FRC Teams that I have helped through build seasons: 4171, 4255, and 5171.
Reply With Quote
  #5   Spotlight this post!  
Unread 29-11-2013, 21:03
gixxy's Avatar
gixxy gixxy is offline
Programming and Arduino Mentor
AKA: Gustave Michel III
FRC #3946 (Tiger Robotics)
Team Role: Mentor
 
Join Date: Nov 2011
Rookie Year: 2012
Location: Ruston, LA
Posts: 207
gixxy is on a distinguished road
Re: Running pid loops in a separate thread.

As stated above fully possible. Team 3946 used it in 2013 for our RPi (RPi was doing image processing/goal tracking).

All of our code is available here: UltimateAscent

The specific classes you would probably want to look at are ThreadedPi, ThreadedBerryPi under Subsystems, and PrintThreadPiData under Commands.
__________________
Programmer - A creature known for converting Caffeine into Code.
Studying Computer Science @ Louisiana Tech University
Associate Consultant @ Fenway Group

2012-13: 3946 - Head of Programming, Electrical and Web
2014 - 3468 - Programming Mentor
2015 - Present - Lurker
Reply With Quote
  #6   Spotlight this post!  
Unread 30-11-2013, 15:26
Hypnotoad's Avatar
Hypnotoad Hypnotoad is offline
Junior Assistant Secretary of Moon
no team
Team Role: Programmer
 
Join Date: Feb 2012
Rookie Year: 2011
Location: California
Posts: 114
Hypnotoad is infamous around these partsHypnotoad is infamous around these partsHypnotoad is infamous around these partsHypnotoad is infamous around these partsHypnotoad is infamous around these partsHypnotoad is infamous around these parts
Re: Running pid loops in a separate thread.

Just came back here after working on some home projects. Thanks for all the help. I was hoping to get a higher sampling rate so that I could arc the robot's path in autonomous if i need to. The way things are right now, PID does not work for moving setpoints.
__________________
The following sentence is true.
The preceding sentence is false.
This sentence is false.
This signature is false.
My teeth are false.
Reply With Quote
  #7   Spotlight this post!  
Unread 30-11-2013, 18:37
TravSatEE's Avatar
TravSatEE TravSatEE is offline
Spacecraft Engineer and more
FRC #2035 (Robo Rockin' Bots)
Team Role: Engineer
 
Join Date: Jan 2012
Rookie Year: 2002
Location: Monterey, CA
Posts: 26
TravSatEE is infamous around these partsTravSatEE is infamous around these parts
Re: Running pid loops in a separate thread.

Quote:
Originally Posted by Hypnotoad View Post
I was hoping to get a higher sampling rate so that I could arc the robot's path in autonomous if i need to.
If you are a high school student and considering trajectory-following control, good work!

Quote:
Originally Posted by Hypnotoad View Post
The way things are right now, PID does not work for moving setpoints.
This statement is vague and hard to assist without more information. How often are you changing the setpoint? My assumption is too often. A rule of thumb in control is 4-10X faster system convergence than control signal changes. Since you are limited to a ~200 Hz update rate, your setpoint should be updated only a few times a second.

To help you understand this more, I refer to the top figure on the PID Controller page. In a stable PID Controller design, the coefficients Kp, Ki, Kd are chosen ("tuned") for the system dynamics. In general most people just guess and check until something works.

When looking at that figure, what you are doing is changing r(t). This introduces new dynamics into the e(t) signal. Thus, the given set of Kp, Ki, Kd parameters may not lead to a stable controller (because they worked with the original dynamics, and not the combination of original and new). I think this is what you are experiencing based on the "PID does not work for moving setpoints" comment. In general, PID controllers do allow for changing set points (this is why that diagram shows r(t) as a function of time and not just a constant-value r). One way is to change the three coefficients, but in your use here this isn't desirable (you can explore this more in college).

Another approach is to change the nature of the dynamics added, by which I mean to change r(t) less often. Remember the first goal of a control system is stability, not time-optimal control. Thus, introducing changes to r(t) at a slower rate should help you maintain stability and allow for trajectory following.

If you still have more stability issues, then you can also try to slow down the motor speeds. I know this is less desirable to you than faster sampling, but as you prove to yourself you can do it at slower speeds, you might then try for to increase the speed later.
__________________
I have a doctoral degree in electrical engineering. My FIRST mentoring philosophy is to encourage student-led activities and create a level playing field among all teams. I believe this approach results in an exciting game, rather than emphasis on a handful of dominant teams.

FIRST FRC Teams that I have mentored: 612, 342, 2035, and 5104. FIRST FRC Teams that I have helped through build seasons: 4171, 4255, and 5171.
Reply With Quote
  #8   Spotlight this post!  
Unread 30-11-2013, 20:14
ekapalka's Avatar
ekapalka ekapalka is offline
Registered User
FRC #3216
 
Join Date: Dec 2012
Location: Bermuda
Posts: 277
ekapalka has a spectacular aura aboutekapalka has a spectacular aura about
Re: Running pid loops in a separate thread.

...that sounds like bad news for my pre-season programming project... I've been working on creating my own PID library to use for concurrently maintaining and changing the speed of the mecanum wheels on our practice drive-train during teleop, but based on what you're saying, constantly changing the setpoint won't work... (It was the original intention of the library, but if it fails (which it looks like it should), it hasn't necessarily outlived it's functionality :D From what I hear, PID loops have lots of other potential uses).
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 10:38.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi