collapse

Author Topic: Robot Rebels Firstbot Code  (Read 3006 times)

ossipee

  • Member
  • *
  • o
  • Posts: 86
Robot Rebels Firstbot Code
« on: April 24, 2016, 07:20:06 PM »
Ok your not gonna be leave this but ossipee wrote some code designed for a persons first robot in anticipation for Robot Rebels developing a first robot kit, I think it might be popular to have one, and bring folks to our site. It uses a motor driver as opposed to modifying servos my thought was that makes a simpler build for a newbie. You guys that actually code, I would like you to take a peek and correct my comments or shorten/simplify the code or anything else we can do to make it more user understandable.
Code is here cause I could not get it to post properly

http://pastebin.com/Ac9Q7biH
« Last Edit: April 24, 2016, 07:33:16 PM by ossipee »

birdmun

  • Member
  • ****
  • Posts: 17
Re: Robot Rebels Firstbot Code
« Reply #1 on: April 26, 2016, 05:28:53 AM »
line 16 should mention the units in the comment. 100 cm? m? mi? km? mm?
maybe after line 17 add a comment that reiterates a newb needs to adjust the following pins to work with their setup
The NewPing lib will actually spit out a distance measurement in inches or cm. You could comment out line 27 and line 36.
You may consider adding a MIN value for minimum motor speed, as a motor will not move at 1 or 5 or probably even 20.
You might consider renaming your motor pins LeftMotor and RightMotor rather than lMotor and rMotor.
Add a debug flag to the beginning of the code. 1 for debug on. 0 for debug off. When you aren't debugging, disable all the printing.
Printing slows down the program.
You might want to #define Center, MAXLeft and MAXRight servo numbers at the start. (Magic numbers are Bad)
Also, Line 60, 124, 130, 138, 140, 145, 150 ... Magic numbers haven't gotten better yet. :D #define or make them CONST at the start.
Less trouble if they need to be changed later.
Using the NewPing library you can get rid of your scan() function completely. The code is already written. You may as well use it. :)
Line 147 could be a simple else. No need for the if condition. Once you have run through everything else, the only other option is to turn right.

Caught bdk in shoutbox this morning
bdk6: with const the compiler has a datatype declared instead of having to assume one.  It can work its magic better.  Modern compilers do lots of magic
Today at 05:24:24 AM

In other words for magic number replacement/definition use CONST instead of #define.

consider looking over this code for the motor control http://pastebin.com/EXGjMNrz

I'll wait for other smart people to chime in now. :)

bdk6

  • Guest
Re: Robot Rebels Firstbot Code
« Reply #2 on: April 26, 2016, 08:01:27 AM »
Along with what birdmun said I have some comments.  Just a couple quickies now, but more will follow when I have more time.
First, as bird said adding the units to MaxDistance is good.  But even better than in the comments, how about in the name so it's always there?  MaxDistanceCM or MaxDistanceInch or MaxDistanceMeters

C uses "functions" to do work.  C is odd in that it doesn't have "normal" subroutines or procedures to do work without returning a value.  Functions return values.  When C was almost 20 years old the standards committee tried to fix that by adding a "void" data type.  It's really odd.  If you change the Scan() function to return the value instead of writing to a global variable, the code will be much cleaner and understandable (as well as efficient!)    You can change line 113 to
unsigned int scan()
and lines 117 and 118 to
delay(500);
return  Time/US_ROUNDTRIP_CM;
Then either do away with the distance variable completely and replace line 57 with
FrontDistance = scan();
or  put this line in front of line 57:
distance = scan();
Since you use the distance variable again later, the second option is the quick and dirty way of doing it.

Anyway, my 2 cents worth.  Maybe I'll drop a couple more pennies later. You are  doing good stuf here.

merser

  • Member
  • *
  • Posts: 24
Re: Robot Rebels Firstbot Code
« Reply #3 on: May 02, 2016, 04:40:48 AM »
Just a couple things I noticed that hasn't been mentioned.
In the comments at the bottom it says half a second but the delay is actually 100 or 200 milliseconds.
The variable duration isn't used.
Also the motor speed is set in global variables lSpeed and rSpeed but never used and also the motorSpeed function is never used. Not sure if they were two different ideas or are an unfinished work.
I like Birdmun's recommendation about turning serial on only in debug mode.
What I am wondering is where is the serial.begin?

 

* Search


* Recent Topics

2D Side Scroller Cyberpunk themed by Killer Angel
[November 12, 2019, 07:56:15 AM]


The unnamed (yet) quatruped spider project by tinhead
[November 09, 2019, 05:58:16 AM]


Ebot SL by erco
[October 31, 2019, 04:27:36 PM]


Discord anyone? by tinhead
[October 20, 2019, 05:35:59 AM]


MKS Gen L 1.0 by lllloydo
[October 06, 2019, 01:24:26 AM]


Saturn pics anyone by viswesh
[September 28, 2019, 11:55:53 PM]


Hello Robot Rebels by Passixs
[September 15, 2019, 10:19:10 PM]


All Ya'll. And me. by James
[September 09, 2019, 10:35:19 PM]


MSD - Motorized solder dispenser (Work in progress) by James
[September 09, 2019, 08:45:29 PM]


It may be dangerous to work in the virtual realm by ZeroMax
[September 05, 2019, 05:55:16 PM]


"1984 Nixie Time" by Gareth
[August 25, 2019, 12:21:58 PM]


Just wanted to say Hi. by ErwinCanto
[August 18, 2019, 10:11:28 PM]


SDR (Software Defined Radio) by Gareth
[August 14, 2019, 01:12:21 PM]


PLX-DAQ by AmandaG
[August 06, 2019, 01:58:42 PM]


DIY Yellow Servo by AmandaG
[August 06, 2019, 01:08:09 PM]

* Recent Posts

Re: 2D Side Scroller Cyberpunk themed by Killer Angel
[November 12, 2019, 07:56:15 AM]


Re: The unnamed (yet) quatruped spider project by tinhead
[November 09, 2019, 05:58:16 AM]


Re: The unnamed (yet) quatruped spider project by Nemesis
[November 09, 2019, 03:29:02 AM]


Re: Ebot SL by erco
[October 31, 2019, 04:27:36 PM]


Re: 2D Side Scroller Cyberpunk themed by Killer Angel
[October 31, 2019, 09:57:48 AM]


Re: 2D Side Scroller Cyberpunk themed by Killer Angel
[October 31, 2019, 06:01:53 AM]


Re: 2D Side Scroller Cyberpunk themed by maelh
[October 30, 2019, 03:06:28 AM]


Discord anyone? by tinhead
[October 20, 2019, 05:35:59 AM]


The unnamed (yet) quatruped spider project by tinhead
[October 20, 2019, 05:28:14 AM]


Re: 2D Side Scroller Cyberpunk themed by Killer Angel
[October 17, 2019, 09:44:06 AM]


Re: 2D Side Scroller Cyberpunk themed by Killer Angel
[October 17, 2019, 08:16:11 AM]


Re: 2D Side Scroller Cyberpunk themed by Killer Angel
[October 14, 2019, 08:52:20 AM]


2D Side Scroller Cyberpunk themed by Killer Angel
[October 14, 2019, 03:32:40 AM]


Re: MKS Gen L 1.0 by lllloydo
[October 06, 2019, 01:24:26 AM]


Re: Saturn pics anyone by viswesh
[September 28, 2019, 11:55:53 PM]