Go to Post I have postulated a theory that it is due to a resonance between gravity waves and the color purple. - petek [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
 
Thread Tools Rating: Thread Rating: 14 votes, 5.00 average. Display Modes
  #1   Spotlight this post!  
Unread 04-10-2015, 10:18 PM
faust1706's Avatar
faust1706 faust1706 is offline
Registered User
FRC #1706 (Ratchet Rockers)
Team Role: College Student
 
Join Date: Apr 2012
Rookie Year: 2011
Location: St Louis
Posts: 498
faust1706 is infamous around these partsfaust1706 is infamous around these parts
Tired of tuning pesty PID loops for hours? Try Neural Networks!

Per a few requests, and my personal objective to have FRC as a whole have more focus trying new things when it comes to software, I decided to make a generic Neural network code that can be easily adjusted to suit whatever control needs you desire.

How does it work?

1. Simply define your network topology as a vector of integers. For example, <2, 4, 3, 2> will create a network with 2 input nodes, 2 layers of hidden nodes with 4 and 3 nodes respectively, and 2 output nodes.

2. Get your data. I set it up to read data from a file, as you generally train a network off data after the fact instead of in real time. An example of a compatible data file is included in the git linked at the end of this post.

3. Send the data through the neural network until the weights of the nodes converge.

4. ????

5. Profit.

Let's use an example: You have an omni-wheeled robot that wants to center itself in a hallway such that it is the same distance to each wall. You put 2 range finders on each side of the robot to arrange for this.

What should your architecture be? This is a little bit of a tricky problem. You can have either 1 or 2 inputs. Let's stick with the 1 input route. The input would be the difference of the two range finders. The output you care about is the 4 motor values, but the output that the neural network is trying to achieve is a single value: zero. A possible architecture could be <1, 4, 4, 1>. Your second hidden layer is actually your motor values.

So how do you use this computational engine you just created? You can do it two ways, in real time, or gather random data and train it on that. I personally find it much more enjoyable to watch the robot learn to center itself. What you'd need to do is set up your robot anywhere (even in the center of the hallway!). Get the sensor data, feed it forward into your neural network, get your motor values, apply them for a duration of x ms, get the new sensor data (which is the output of the network), then train it on that value. Repeat until your weights converge. Once the network converges, save the state of it (as in the node weights). Now you have a robot that taught itself how to center itself in a hallway.

I'm not going to go under the hood of neural networks as this post is already rather lengthy.

Code: https://github.com/faust1706/Neural-Network

I accidentally pushed built code to the repo, my mistake.

Something really cool that can be done with this is have your battery voltage as an input. The network will generalize to work at any functioning voltage.
__________________
"You're a gentleman," they used to say to him. "You shouldn't have gone murdering people with a hatchet; that's no occupation for a gentleman."

Last edited by faust1706 : 04-10-2015 at 10:22 PM.
Reply With Quote
  #2   Spotlight this post!  
Unread 04-11-2015, 12:04 AM
connor.worley's Avatar
connor.worley connor.worley is offline
Registered User
FRC #0973 (Greybots)
Team Role: Mentor
 
Join Date: Mar 2011
Rookie Year: 2010
Location: Berkeley/San Diego
Posts: 598
connor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond repute
Re: Tired of tuning pesty PID loops for hours? Try Neural Networks!

Would it be possible to take a video of the realtime learning in action?
__________________
Team 973 (2016-???)
Team 5499 (2015-2016)
Team 254 (2014-2015)

Team 1538 (2011-2014)
2014 Driver (25W 17L 1T)
日本語でOK
Reply With Quote
  #3   Spotlight this post!  
Unread 04-11-2015, 12:25 AM
faust1706's Avatar
faust1706 faust1706 is offline
Registered User
FRC #1706 (Ratchet Rockers)
Team Role: College Student
 
Join Date: Apr 2012
Rookie Year: 2011
Location: St Louis
Posts: 498
faust1706 is infamous around these partsfaust1706 is infamous around these parts
Re: Tired of tuning pesty PID loops for hours? Try Neural Networks!

I'm don't have access to a simple navigation robot anymore...it got scrapped for parts. I'll see if I can put something together one day in the lab.
__________________
"You're a gentleman," they used to say to him. "You shouldn't have gone murdering people with a hatchet; that's no occupation for a gentleman."
Reply With Quote
  #4   Spotlight this post!  
Unread 04-11-2015, 03:17 AM
RyanCahoon's Avatar
RyanCahoon RyanCahoon is offline
Disassembling my prior presumptions
FRC #0766 (M-A Bears)
 
Join Date: Dec 2007
Rookie Year: 2007
Location: Mountain View
Posts: 688
RyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond repute
Re: Tired of tuning pesty PID loops for hours? Try Neural Networks!

Have you tried learning parameters for a PID controller?

Any thoughts on using recurrent neural networks to add some memory to your controller?
__________________
FRC 2046, 2007-2008, Student member
FRC 1708, 2009-2012, College mentor; 2013-2014, Mentor
FRC 766, 2015-, Mentor
Reply With Quote
  #5   Spotlight this post!  
Unread 04-11-2015, 01:19 PM
faust1706's Avatar
faust1706 faust1706 is offline
Registered User
FRC #1706 (Ratchet Rockers)
Team Role: College Student
 
Join Date: Apr 2012
Rookie Year: 2011
Location: St Louis
Posts: 498
faust1706 is infamous around these partsfaust1706 is infamous around these parts
Re: Tired of tuning pesty PID loops for hours? Try Neural Networks!

The goal of releasing this code was that teams would have an easier time with machine control as well as show them that there is more than one way to do things, and a lot of times the first solution you have (the PID controller given to each team), isn't always the best.

By learning parameters, are you talking about setting them yourself, or a PID controller that finds parameters itself? The latter already exists, but it is more complex than a neural network in my opinion.

RNNs do wonders, but I wanted to keep things simple for an introductory post. This would be a great topic for a team to explore for themselves, as well as Elman Networks and others of the like.
__________________
"You're a gentleman," they used to say to him. "You shouldn't have gone murdering people with a hatchet; that's no occupation for a gentleman."
Reply With Quote
  #6   Spotlight this post!  
Unread 04-12-2015, 07:00 PM
JohnSmooth42 JohnSmooth42 is offline
Is writing code
FRC #0375 (The Robotic Plague)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2012
Location: New York
Posts: 27
JohnSmooth42 is an unknown quantity at this point
Re: Tired of tuning pesty PID loops for hours? Try Neural Networks!

Saw you mention this on r/FRC, I'm definitely eager to check this out!

EDIT: could you post a more detailed example implementation?

Last edited by JohnSmooth42 : 04-12-2015 at 07:15 PM.
Reply With Quote
  #7   Spotlight this post!  
Unread 04-12-2015, 08:56 PM
faust1706's Avatar
faust1706 faust1706 is offline
Registered User
FRC #1706 (Ratchet Rockers)
Team Role: College Student
 
Join Date: Apr 2012
Rookie Year: 2011
Location: St Louis
Posts: 498
faust1706 is infamous around these partsfaust1706 is infamous around these parts
Re: Tired of tuning pesty PID loops for hours? Try Neural Networks!

Of course. It'd be easier for me to do so if you give me an example that you want me to go through, do you have any in mind?
__________________
"You're a gentleman," they used to say to him. "You shouldn't have gone murdering people with a hatchet; that's no occupation for a gentleman."
Reply With Quote
  #8   Spotlight this post!  
Unread 04-12-2015, 09:27 PM
JohnSmooth42 JohnSmooth42 is offline
Is writing code
FRC #0375 (The Robotic Plague)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2012
Location: New York
Posts: 27
JohnSmooth42 is an unknown quantity at this point
Re: Tired of tuning pesty PID loops for hours? Try Neural Networks!

An example of this in actual robot code (e.g. teaching it how to center itself like you mentioned) would help a lot. Also if there's any way I can help contribute to this I would be glad to help.
Reply With Quote
  #9   Spotlight this post!  
Unread 04-13-2015, 12:35 AM
faust1706's Avatar
faust1706 faust1706 is offline
Registered User
FRC #1706 (Ratchet Rockers)
Team Role: College Student
 
Join Date: Apr 2012
Rookie Year: 2011
Location: St Louis
Posts: 498
faust1706 is infamous around these partsfaust1706 is infamous around these parts
Re: Tired of tuning pesty PID loops for hours? Try Neural Networks!

I added a case for real time training called realtime. It is not integrated with anything, but it is laid out to be embedded easily. There are plenty of comments which should allow for fast integration of any system, so long as you can give this code the sensor data and send the motor values to wherever they are needed. I feel it'd be easier to add this into whatever that you want to train instead of sending values over serial or something, but that's just me.

The code is a little messy, I apologize. The messy part is the training from a file, I'll work on it when I have more time.

If you need help integrating it into FRC code, don't hesitate to ask me.

Edit* also worked on the readme a bit. Still unsure of how specific I want it, but it's a start.
__________________
"You're a gentleman," they used to say to him. "You shouldn't have gone murdering people with a hatchet; that's no occupation for a gentleman."

Last edited by faust1706 : 04-13-2015 at 01:58 AM.
Reply With Quote
  #10   Spotlight this post!  
Unread 04-13-2015, 08:05 PM
JohnSmooth42 JohnSmooth42 is offline
Is writing code
FRC #0375 (The Robotic Plague)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2012
Location: New York
Posts: 27
JohnSmooth42 is an unknown quantity at this point
Re: Tired of tuning pesty PID loops for hours? Try Neural Networks!

It looks very nice, I forked it and I'm trying to make an implementation of a basic neural network for use with encoders.
Reply With Quote
  #11   Spotlight this post!  
Unread 04-13-2015, 08:21 PM
Ben Wolsieffer Ben Wolsieffer is offline
Dartmouth 2020
AKA: lopsided98
FRC #2084 (Robots by the C)
Team Role: Alumni
 
Join Date: Jan 2011
Rookie Year: 2011
Location: Manchester, MA (Hanover, NH)
Posts: 520
Ben Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud of
Re: Tired of tuning pesty PID loops for hours? Try Neural Networks!

This looks really cool, and I'm planning to try to hack on it over April vacation (when I finally have some time). Since our team uses Java, I'm going to try to port it, which will hopefully help me understand the code better.

I have no previous experience with neural networks, so I'll probably have a lot of questions in the future, but for now I was wondering if there is some way to decide how to structure the hidden layers. In your example, you use 4 nodes, one for each wheel and two layers. What is the reason for this?

Thanks.
__________________



2016 North Shore District - Semifinalists and Excellence in Engineering Award
2015 Northeastern University District - Semifinalists and Creativity Award
2014 Granite State District - Semifinalists and Innovation in Control Award
2012 Boston Regional - Finalists
Reply With Quote
  #12   Spotlight this post!  
Unread 04-13-2015, 10:38 PM
JohnSmooth42 JohnSmooth42 is offline
Is writing code
FRC #0375 (The Robotic Plague)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2012
Location: New York
Posts: 27
JohnSmooth42 is an unknown quantity at this point
Re: Tired of tuning pesty PID loops for hours? Try Neural Networks!

Here is my implementation of your library for use with encoders on a robot.

https://github.com/DannyMoses/Neural..._implementaion

It's definitely not very polished(I basically copied your example). Am I doing it right? I don't have a robot to test this on right now so I would value any feedback. I also just want to say that I think this is probably the future of FRC programming right here.
Reply With Quote
  #13   Spotlight this post!  
Unread 05-08-2015, 11:35 PM
Ben Wolsieffer Ben Wolsieffer is offline
Dartmouth 2020
AKA: lopsided98
FRC #2084 (Robots by the C)
Team Role: Alumni
 
Join Date: Jan 2011
Rookie Year: 2011
Location: Manchester, MA (Hanover, NH)
Posts: 520
Ben Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud ofBen Wolsieffer has much to be proud of
I have gotten the saving and loading functionality working on my Java port. I modified your file format a little to make my parser simpler. I think most of my problems are caused by the robot code.
__________________



2016 North Shore District - Semifinalists and Excellence in Engineering Award
2015 Northeastern University District - Semifinalists and Creativity Award
2014 Granite State District - Semifinalists and Innovation in Control Award
2012 Boston Regional - Finalists
Reply With Quote
  #14   Spotlight this post!  
Unread 05-09-2015, 12:20 AM
faust1706's Avatar
faust1706 faust1706 is offline
Registered User
FRC #1706 (Ratchet Rockers)
Team Role: College Student
 
Join Date: Apr 2012
Rookie Year: 2011
Location: St Louis
Posts: 498
faust1706 is infamous around these partsfaust1706 is infamous around these parts
Re: Tired of tuning pesty PID loops for hours? Try Neural Networks!

That's good to hear for me at least...It might be that there is too much influence from new data (alpha), something to look into, but it is unlikely if it is <.20. A simple cheat would be to dampen the motor values outputted by the net if it comes down to that.

1706 is investigating into swerve this summer. I can't wait to see what @cmastudios is able to do with it along with our swerve trajectory planner code.
__________________
"You're a gentleman," they used to say to him. "You shouldn't have gone murdering people with a hatchet; that's no occupation for a gentleman."
Reply With Quote
  #15   Spotlight this post!  
Unread 09-22-2015, 02:50 PM
ShotgunNinja's Avatar
ShotgunNinja ShotgunNinja is offline
Programming Mentor, FRC 4247
AKA: Nicholas Iannone
FRC #4247 (CougarBOTS)
Team Role: Mentor
 
Join Date: Jan 2008
Rookie Year: 2006
Location: Kenosha, WI
Posts: 158
ShotgunNinja is a jewel in the roughShotgunNinja is a jewel in the roughShotgunNinja is a jewel in the roughShotgunNinja is a jewel in the rough
Re: Tired of tuning pesty PID loops for hours? Try Neural Networks!

Wow, I really want to have a chance to mess with all of this.
__________________
Team #2970 Alum
Team #1652 Alum
2006: School Mascot for Team 1652
2007-2008: Programmer for Team 1652 (Robot Mafia)
2009: Programmer on Team 1652 (Robot Mafia), Programmer on Team 2970 (eSchool eBots)
2010-2016: Volunteer, Wisconsin Regional
2017: Programming Mentor, FRC 4247 (CougarBOTS, Obama SCTE, Milwaukee, WI)
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 09:26 AM.

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