Author Topic: Noob Programming question for $50 robot source code modification  (Read 6014 times)

0 Members and 1 Guest are viewing this topic.

Offline Metal Slug 2Topic starter

  • Supreme Robot
  • *****
  • Posts: 333
  • Helpful? 11
Noob Programming question for $50 robot source code modification
« on: February 02, 2009, 11:19:27 PM »
I am waiting on one last thing to be shipped in for my $50 robot (futaba connectors for easily plugging in the sensors).  In the mean time i thought to myself, why dint i just mod the photovore code and just make the robot drive around, based on timing its movements.

The problem is I've only ever programmed in RobotC (for the Lego NXT Mindstorm robot kits) and dint know anything about programming microcontrollers otherwise in C.

What i am trying to do is basically program my $50 bot to move foreward for a set amount of time, then turn for a set amount of time etc.  how would i go about doing this?  based on the code Ive read and some examples from the AVR library, i think there is two possibilities that i have come up with to solve this problem.  however i don't know if they will work or not (i am simply using the same type of logic i used with RobotC, assuming there both C languages and have similar ways of coding things).  After for example putting in the two servo pulse value numbers, to make the servos go for a certain amount of time would i type this:

{//go straight
servo_left(25);
servo_right(44);
delay_cycles(115927); //not sure if my math is right, but this should go for 5 seconds....maybe?
}

or, with some other example header and .C files (timertest) from the AVR library, would this work?:

timerInit();//(this would be at the start of the program to start the timer)

{//go straight
servo_left(25);
servo_right(44);
timerPause(5000);//wait 5 sec.
}


if both of these work, great - which one would be better though?...if one of them works, well then i use that...if none of these ideas work, how else can i do this (assuming that this can be done...if it cant, then i just have to wait for sensor connectors.)

Thanks for the help. =)

Offline g

  • Beginner
  • *
  • Posts: 3
  • Helpful? 0
Re: Noob Programming question for $50 robot source code modification
« Reply #1 on: February 03, 2009, 02:09:00 AM »
I'm completely new to this too, but I have programming experience to tell you that won't quite work. It looks like this would give it a very small jolt forward and then stop for 5 seconds, and assuming the rest of the code is unmodified it would continue to do this every 5 seconds because it's in a loop. You just need to make your own loop that continually moves it forward for 5 seconds. Since the code's author thinks it's a good idea to delay 500 cycles each loop I'll keep it there and divide your number by it:

Code: [Select]
int loops=115927/500;

for (count = 1; count <= loops; count++)
{//go straight
servo_left(25);
servo_right(44);

delay_cycles(500);
}

This would go in between "LED_off" and "return 0" in place of the while loop that's there currently.

Offline Metal Slug 2Topic starter

  • Supreme Robot
  • *****
  • Posts: 333
  • Helpful? 11
Re: Noob Programming question for $50 robot source code modification
« Reply #2 on: February 03, 2009, 05:15:56 PM »
kool thanks...i was experimenting last night, but my programmer seems to not want to enter "programming mode" anymore.  my avrisp mkII programmer keeps saying " programming mode...FAILED" i adjusted the frequencies, and tried everything, but i cant get it to program my bot n e more.  the firmware is up to date also.  i even tried simply reprogramming the bot with the original Photovore code, but it didnt work either...

if i can get this to work ill definetly give youor idea a try, but for now im a sitting (and frustrated) duck.  :P


EDIT: haha! i found the problem with my programmer...somehow since the time i first programmed it with the Photovore source code up until now, the "upgrade" i had installed somehow got deleted...so i just for a last hope tried reinstalling it, and it works now.  just a question, is it OK to just 'unplug' the programmer, (like most flash drives) or should i go to the "safely remove hardware" thing before unplugging...could failue to do so have resulted in the resetting of my programmer (and a few hours of agonizing frustration)?

now i can try out your code =)


EDIT2:  So ive modified the photovore code with your code, but theres just one little problem.....everything compiles alright and stuff, but.....how do i download it onto my microcontroller?  The code is .C code, however when i want to program my bot, the programmer asks for a .hex file.  Do i have to convert the .C file into a .Hex  ...if so, how do i do this?

theres also one more thing i would like to know, your code has this:

for (count = 1; count <= loops; count++)

Does the "count++" add one to the count value when this loop is done looping?  If so, can i make another loop identical to the previous one with a     'count = 2' and reverse the servo speed numbers        so the bot will move backward for the same time it moved foreward?
« Last Edit: February 03, 2009, 06:36:54 PM by Metal Slug 2 »

Offline g

  • Beginner
  • *
  • Posts: 3
  • Helpful? 0
Re: Noob Programming question for $50 robot source code modification
« Reply #3 on: February 07, 2009, 10:44:28 PM »
I would have replied sooner but I didn't get any email notification for this thread.

For converting to hex and all that, it's explained in the guide already: http://www.societyofrobots.com/step_by_step_robot_step4.shtml

count++ is telling it to add one each time it loops, its only purpose is to keep track of how many times it has looped so far so it knows when to stop. So if you added another loop you wouldn't change anything (count=2 would make it start at 2 and skip the first time through).

Offline Metal Slug 2Topic starter

  • Supreme Robot
  • *****
  • Posts: 333
  • Helpful? 11
Re: Noob Programming question for $50 robot source code modification
« Reply #4 on: February 07, 2009, 11:56:25 PM »
i tried using your code, and i tried making a .hex thing from the code.  i cant seem to figure it out tho.  i dont know where to find the 'makefile' :-\ :-\

Offline g

  • Beginner
  • *
  • Posts: 3
  • Helpful? 0
Re: Noob Programming question for $50 robot source code modification
« Reply #5 on: February 08, 2009, 12:46:47 AM »
makefile is in the source .zip, you should have extracted all of them to the same place.

Offline Metal Slug 2Topic starter

  • Supreme Robot
  • *****
  • Posts: 333
  • Helpful? 11
Re: Noob Programming question for $50 robot source code modification
« Reply #6 on: February 08, 2009, 01:36:24 AM »
ahh ok, so just use the original makefile which came with the photovore code?

Offline Joesavage1

  • Robot Overlord
  • ****
  • Posts: 268
  • Helpful? 0
    • Dev-HQ - Learn C++, Visual Basic, HTML and More!
Re: Noob Programming question for $50 robot source code modification
« Reply #7 on: February 08, 2009, 01:37:20 AM »
Yes, the origional makefile does everything for you =D


Joe

Offline HePE

  • Jr. Member
  • **
  • Posts: 14
  • Helpful? 0
Re: Noob Programming question for $50 robot source code modification
« Reply #8 on: February 08, 2009, 02:04:31 AM »
Hi Metal slug 2

1) The .hex file will be the product of your code after building it. It will be stored on the same folder where you've extracted it all the things you got from photovore_v1.zip. The makefile will be on this same folder.

Once you have the correct files on place here's whta you do...
a.) click  "Rebuild All" in the "Build tab"
b.) verifyi that you have "Build succeeded with 0 Warnings..."
c.) since you have the MKII click on the AVR icon select the "AVRISP MKII" on the left  and USB on the right of the window that popped up.. click on "connect" 
d.)select atmega8 in the device field.
e.)set Programming mode to ISP check the two boxes (i.e. Erase and verify after programming)
f.) on the Flash Programming area select "Input Hex file" then browse for your Photovore_v1.hex by  clicking on the  ...  icon (this file as metioned earlier will be on the folder where you extracted the .zip file example c:\My_Robots\Photovore_v1\)
g.) now click on the program button on the Flash area (of course your MKII must be connected to your atmeag8 and the circuit powered-on at this point).

Hope this helps. If you still bump into troubles just keep on posting.

Also see this http://www.societyofrobots.com/robotforum/index.php?topic=6448.msg49019#msg49019


Cheers bro

Offline Joesavage1

  • Robot Overlord
  • ****
  • Posts: 268
  • Helpful? 0
    • Dev-HQ - Learn C++, Visual Basic, HTML and More!
Re: Noob Programming question for $50 robot source code modification
« Reply #9 on: February 08, 2009, 02:13:46 AM »
How does the Atmeg8 fit in the MkII, sorry for interuting just thought id ask?


Thanks

Joe

Offline Metal Slug 2Topic starter

  • Supreme Robot
  • *****
  • Posts: 333
  • Helpful? 11
Re: Noob Programming question for $50 robot source code modification
« Reply #10 on: February 08, 2009, 02:33:15 AM »
i think you have the wrong idea.  The ATMEGA8 is a MCU (micro controller unit).  If you take a look at the $50 robot, the big black chip(well, its small actually) is the ATMEGA8.  off to the side of the $50 robot PCB board you will see
TWO rows of 5 pins...these are what the cheaper programmer plug into.  however, the AVRISP MKII (the different, more expensive, but better programmer) has only a six pin programming head.  so the $50 robot used with the AVRISP MKII will have two rows of 3 pins to connect to the programmer.  when the last parts to my $50 bot arrive i will complete it and upload a pic to show you what i mean.

The ATMEGA8 doesn't connect to the programmer pin to pin, but rather through the $50 robot's pcb (printed circuit board).  You need to put the atmega8 into the slot for the MCU on the $50 pcb and then plug your programmer into the Pins designated for it.  If you program with the AVRISP MKII programmer, you will need to change the pin arrangement for the programmer to plug into from the original 10-pin connector (for use with the dongle programmer) to the 6-pin connector.  You can find out how to do this from alot of posts here.
« Last Edit: February 08, 2009, 02:35:32 AM by Metal Slug 2 »

Offline Metal Slug 2Topic starter

  • Supreme Robot
  • *****
  • Posts: 333
  • Helpful? 11
Re: Noob Programming question for $50 robot source code modification
« Reply #11 on: February 08, 2009, 05:17:59 AM »
Hi Metal slug 2

1) The .hex file will be the product of your code after building it. It will be stored on the same folder where you've extracted it all the things you got from photovore_v1.zip. The makefile will be on this same folder.

Once you have the correct files on place here's what you do...
a.) click  "Rebuild All" in the "Build tab"
b.) verify that you have "Build succeeded with 0 Warnings..."
c.) since you have the MKII click on the AVR icon select the "AVRISP MKII" on the left  and USB on the right of the window that popped up.. click on "connect" 
d.)select atmega8 in the device field.
e.)set Programming mode to ISP check the two boxes (i.e. Erase and verify after programming)
f.) on the Flash Programming area select "Input Hex file" then browse for your Photovore_v1.hex by  clicking on the  ...  icon (this file as metioned earlier will be on the folder where you extracted the .zip file example c:\My_Robots\Photovore_v1\)
g.) now click on the program button on the Flash area (of course your MKII must be connected to your atmeag8 and the circuit powered-on at this point).

Hope this helps. If you still bump into troubles just keep on posting.

Also see this http://www.societyofrobots.com/robotforum/index.php?topic=6448.msg49019#msg49019


Cheers bro

heres the code i put together:

Code: [Select]
/****************************************************************************
*
*   Copyright (c) 2007 www.societyofrobots.com
*   (please link back if you use this code!)
*
*   This program is free software; you can redistribute it and/or modify
*   it under the terms of the GNU General Public License version 2 as
*   published by the Free Software Foundation.
*
*   Alternatively, this software may be distributed under the terms of BSD
*   license.
*
* Photovore v1, March 10th, 2007
* Simple case-based method for a robot that chases light.
*
*
****************************************************************************/

//SoR Include
#include "SoR_Utils.h" //includes all the technical stuff


int main(void)
{
//declare variables here
//int i=250;//a 'whatever' variable
int sensor_left=0;//left photoresistor
int sensor_right=0;//right photoresistor
int threshold=8;//the larger this number, the more likely your robot will drive straight


/****************INITIALIZATIONS*******************/
//other stuff Im experimenting with for SoR
//uartInit();  // initialize the UART (serial port)
//uartSetBaudRate(9600);// set the baud rate of the UART for our debug/reporting output
//rprintfInit(uartSendByte);// initialize rprintf system

//timerInit(); // initialize the timer system

configure_ports(); // configure which ports are analog, digital, etc.
a2dInit(); // initialize analog to digital converter (ADC)
a2dSetPrescaler(ADC_PRESCALE_DIV32); // configure ADC scaling
a2dSetReference(ADC_REFERENCE_AVCC); // configure ADC reference voltage

//rprintf("Initialization Complete\r\n");
/**************************************************/


/*********ADD YOUR CODE BELOW THIS LINE **********/
LED_off();//turn LED on


while(1)
{
//store sensor data
int loops=115927/500;
int count =1;

for (count = 1; count <= loops; count++)
{//go straight
servo_left(44);
servo_right(44);


/* Servo Test Code
i=250;
while(i>0)
{
servo_left(40);
i--;
}

i=250;
while(i>0)
{
servo_left(24);
i--;
}
*/

//rprintf("Initialization Complete\r\n");

//output message to serial (use hyperterminal)
//print("Hello, World! Read My Analog: %u\r\n", sensor_0);

delay_cycles(500);//a small delay to prevent crazy oscillations
}
    }
/*********ADD YOUR CODE ABOVE THIS LINE **********/

return 0;
}


/*********************COMMAND LIST*************************

delay_cycles(cycles);
Delays - you can make your robot wait for a certain amount of time with this function.
Put the number of computational cycles to delay in the ().
23 cycles is about .992 milliseconds
to calculate: 23/.992*(time in milliseconds to delay) = cycles
Check servo datasheet where it says: 'Direction: Clockwise/Pulse Traveling 1500 to 1900usec'


servo_left(speed); and servo_right(speed);
Commands your servos to rotate at a certain speed.
Vary speed (which represents a delay in cycles) from 20 to 50.
Left is for port D0 and right is for port D1.


LED_on(); and LED_off();
Turns on and off your LED. The LED is on port D4.
By bringing port D4 low, you are turning on the LED.


variable=a2dConvert8bit(pin);
Reads analog pin. For example, set 'pin' to 5 to read PC5.
'variable' will store the value.

***********************************************************/


OK heres what i do, and its not working, maybe theres something I'm doing wrong(obviously):

1. open avr studio 4 and crate a new project: 'Test_Stuff'  and select AVRISP mkII programmer with the ATMEGA8 mcu.
2. copy everything (except the Photovore_v1.aws files) from the Photovore folder into the Test_Stuff folder.
3. copy the photovore_v1 source code to the Test_Stuff.c source file which was initially created
4. mod the source code with what  "g" posted earlier
5. now i click Project>configuration options and a box opens up.
6. i click 'use external makefile'
7. THIS IS WHERE IM CONFUSED:   when i look for the makefile, i find two: one in the Test_Stuff folder with all the other copied stuff from the photovore folder and another makefile found in the 'default' folder found in the Test_Stuff folder (Test_Stuff/Default)(why is that even there?).  i choose the one found in Test_Stuff the and click 'OK'.
8. i click Build>Rebuild All
9. THIS IS WHAT I DONT UNDERSTAND:  everything compiles OK, but on the 'messages' thing theres a red dot and a message saying:

gcc plug-in: Error: Object file not found on expected location C:\Users\Dustin\Desktop\Robots\AVR Studio Robotics\Test_Stuff\Test_Stuff.elf
Make sure your makefile specifies the output .elf file as Test_Stuff.elf

10. it says something about the makefile..so i open the makefile up and find this:

# Target file name (without extension).
TARGET = Photovore_v1

i notice that the targets name is Photovore_v1, yet my code is called Test_Stuff, so i change Target to this and save the Makefile:

# Target file name (without extension).
TARGET = Test_Stuff

11.  I redo step #7, nothing happens, i redo step #8
12. This time i get 4 errors and the previous message statement:

Test_Stuff.c:58: error: 'count' undeclared (first use in this function)
Test_Stuff.c:58: error: (Each undeclared identifier is reported only once
Test_Stuff.c:58: error: for each function it appears in.)
Test_Stuff.c:90: error: expected declaration or statement at end of input

13.  it seems count variable is undeclared, so i add a count variable
14. i rebuild and everything builds fine, however, under messages i still get this error:

gcc plug-in: Error: Object file not found on expected location C:\Users\Dustin\Desktop\Robots\AVR Studio Robotics\Test_Stuff\Test_Stuff.elf
Make sure your makefile specifies the output .elf file as Test_Stuff.elf

even after i renamed the makefile for Test_Stuff..........

15.  I ignore that message and experiment. I redo steps #7 and 8 then hit the little avr button to program the device.  this time i actually find a Test_Stuff.hex file :) I load it up and download it to my $50 bot.
16.  It works!!!!! ;D ;D ;D ...but theres only one problem...it runs forever...what I'm aiming at is a simple program which will move the servos for lets say 5 seconds in one direction, stop, then move in the opposite direction for 3 sec. then moth move in different directions for 2 sec. etc.  but i cant for the life of myself figure out how to do it... :-\ :-\ :'(


help would be very gladly appreciated because i simply can't get this thing figured out :-\ :-\ ??? ??? :-\ :-\

 :) :)
« Last Edit: February 08, 2009, 05:23:51 AM by Metal Slug 2 »

Offline Joesavage1

  • Robot Overlord
  • ****
  • Posts: 268
  • Helpful? 0
    • Dev-HQ - Learn C++, Visual Basic, HTML and More!
Re: Noob Programming question for $50 robot source code modification
« Reply #12 on: February 08, 2009, 08:11:39 AM »
use the copied one, not the defult

Offline Metal Slug 2Topic starter

  • Supreme Robot
  • *****
  • Posts: 333
  • Helpful? 11
Re: Noob Programming question for $50 robot source code modification
« Reply #13 on: February 08, 2009, 02:11:46 PM »
I did, the 'default' one wouldn't work for me.  But why woud there even be a copy of the makefile in the default folder in the Test_Stuff folder, when i only copied the file into the Test_Stuff folder?

Offline Joesavage1

  • Robot Overlord
  • ****
  • Posts: 268
  • Helpful? 0
    • Dev-HQ - Learn C++, Visual Basic, HTML and More!
Re: Noob Programming question for $50 robot source code modification
« Reply #14 on: February 10, 2009, 01:10:12 PM »
im not sure but i'd guess the program automaticly makes a defult makefile, use the one youve copied over.

 


data_list