View Full Version : SetPWM and our IR controls
taggartbg
11-01-2008, 16:40
Hey everybody,
So I got the IR board programmed, got it all wired up to interface with the bot, coded it all out, and nothing happens. I currently only have one of the pins on the board (pin 5) running into the Digital In/Out (signal pin 1). I'm trying to get this working on a two-motor tank-drive bot with the '07 microcontroller. Here is my IR code:
void OperatorController (void )
{
unsigned char ir1;
unsigned char ir2;
unsigned char ir3;
unsigned char ir4;
while ( 1 ) //I'm pretty sure I need this while loop, yes?
{
Tank2 ( 1 , 2 , 2 , 2 , 1 , 2 , 0 , 1);
ir1 = GetDigitalInput ( 1 ); //this should return "1" if it is getting signal, yes?
ir2 = GetDigitalInput ( 2 );
ir3 = GetDigitalInput ( 3 );
ir4 = GetDigitalInput ( 4 );
if ( ir1 == 1) //turn left at half speed
{
SetPWM ( 1 , 65 );
SetPWM ( 2 , 192 );
continue; //do I need this?
}
else if ( ir2 == 1) //straight at half speed
{
SetPWM ( 1 , 192 );
SetPWM ( 2 , 192 );
continue; //do I need this?
}
else if ( ir3 == 1) //turn right at half speed
{
SetPWM ( 1 , 192 );
SetPWM ( 2 , 65 );
continue; //do I need this?
}
else if ( ir4 == 1) //backwards at half speed
{
SetPWM ( 1 , 65 );
SetPWM ( 2 , 65 );
continue; //do I need this?
}
else //all stop
{
SetPWM ( 1 , 127 );
SetPWM ( 2 , 127 );
continue; //again, do I need this?
}
}
}
After a few attempts and nothing happing on the bot, I decided to try this simple code to start picking peice by peice what works and what doesn't:
void OperatorController (void )
{
while ( 1 )
{
SetPWM ( 1 , 192 );
SetPWM ( 2 , 192 );
}
}
In theory, that should make the motors start moving forward at half speed yes? Well it just sits there doing nothing.
I was hoping someone could identify what exactly I'm doing wrong, as I am getting zero results from my bot.
Thanks,
/Taggart, team 1712
ham90mack
11-01-2008, 18:02
Whoa there! You should (under most circumstances) NEVER have a loop in the robot code! Especially an infinite one. The only place that should have an infinite loop is in the main method. What you should do is remove the infinite while loop in this function and put a call to this inside the Process_Data_From_Master_uP in user_routines.c between the calls to the Getdata and Putdata functions. After doing that, you should be able to debug your code.
And you don't need the continue statements there...
Wait, if you are using EasyC (and it seems that you are), then the call to this function should be placed wherever the data processing function is (once again, without an infinite while loop inside this function).
taggartbg
11-01-2008, 23:03
Ah, I saw an infinite while in some similar sample code; however, I guess that was a bad idea. So thanks :)
And just to clarify (i'm kind of new to this), the data processing function is the main function? If not, then i'm a bit confused.
And I am using easyC Pro for this.
Branden Ghena
11-01-2008, 23:20
I would check that the wheels are actually hooked up to pwms 1 and 2 (don't laugh I've done this), and whether the code is actually running, i.e. the the robot is in operator mode and is enabled.
You could also try a simpler loop
void OperatorControl (void )
while(1)
{
printf("hello");
}
If you hook your computer up to the robot, and leave it attached when you run it, this should show something (make sure the terminal window isn't in GD mode)
Yes, the data processing function is the operator control mode where you put this code, and it is called automatically from Main(); so you should be good.
P.S. Also, I program in EasyC and have never had any problems with infinite loops for testing purposes.
laultima
12-01-2008, 02:32
Im pretty sure you only need the while(1) loop if your programming for the VEX controller, which has all the operator code inside of that loop. For programming the Innovation First RC you don't need the loop.
And on the electronics side of things, make sure the PWMs are the right ways in (black is the ground on both the RC and the spike/victor)
Hope this helps.
Edit: If you're still stuck, try taking out the continues out. Im not sure if theyre the problem, but you dont need them for if statements.
ham90mack
12-01-2008, 10:27
In Kevin's FRC code, before processing the data you must call the Getdata function first in order to get the inputs from the controller and place them within a structure. Then after processing the data, you must call Putdata in order to put your outputs to the controller from the output data structure. Maybe the SetPWM does the assembly calls to put the data directly to the controller instead of putting it into a structure that is not directly linked to the actual outputs... I don't know. I have never used the EasyC coding libraries before so I don't know how it appears to do things like that.
Vanquish
12-01-2008, 10:57
I'm also new to programming and I am using 2007 default code. I have never done an autonomous before so any help would be appreciated.
Would something ilke this work?
void User_Autonomous_Code(void)
{
/* Initialize all PWMs and Relays when entering Autonomous mode, or else it
will be stuck with the last values mapped from the joysticks. Remember,
even when Disabled it is reading inputs from the Operator Interface.
*/
pwm01 = pwm02 = pwm03 = pwm04 = pwm05 = pwm06 = pwm07 = pwm08 = 127;
pwm09 = pwm10 = pwm11 = pwm12 = pwm13 = pwm14 = pwm15 = pwm16 = 127;
relay1_fwd = relay1_rev = relay2_fwd = relay2_rev = 0;
relay3_fwd = relay3_rev = relay4_fwd = relay4_rev = 0;
relay5_fwd = relay5_rev = relay6_fwd = relay6_rev = 0;
relay7_fwd = relay7_rev = relay8_fwd = relay8_rev = 0;
digital_io_01 = digital_io_02 = digital_io_03 = digital_io_04 = INPUT;
while (autonomous_mode) /* DO NOT CHANGE! */
{
if (statusflag.NEW_SPI_DATA) /* 26.2ms loop area */
{
Getdata(&rxdata); /* DO NOT DELETE, or you will be stuck here forever! */
/* Add your own autonomous code here. */
if(digital_io_01==1)
{
SetPWM ( 1 , 65 );
SetPWM ( 2 , 192 );
}
Generate_Pwms(pwm13,pwm14,pwm15,pwm16);
Putdata(&txdata); /* DO NOT DELETE, or you will get no PWM outputs! */
}
}
}
ham90mack
12-01-2008, 11:23
It appears that that may work, except there is no such function as SetPWM in Kevin's code. Instead, use:
pwm01 = 65;
pwm02 = 192;
So your code appears to do one thing: if the first programmed IR signal is sent, drive forward forever (or spin forever, depending on how your motors are mounted). That sounds a little dangerous for a first test unless your robot is off the ground. I suggest having a signal to stop the robot as well.
Vanquish
12-01-2008, 11:25
It appears that that may work, except there is no such function as SetPWM in Kevin's code. Instead, use:
pwm01 = 65;
pwm02 = 192;
So your code appears to do one thing: if the first programmed IR signal is sent, drive forward forever (or spin forever, depending on how your motors are mounted). That sounds a little dangerous for a first test unless your robot is off the ground. I suggest having a signal to stop the robot as well.
Yea, I realize that I just wanted to know if the format looked alright, thanks
taggartbg, This may sound obvious, but make sure the PWM cable is connected FROM the victor TO the RC on the PWM ports (ports 1 and 2).
Could be you connected it to a spike, and to the best of my knowledge, you can't put half speed when the motor is connected to a spike.
As said by others, you don't need the continue statement.
And in EasyC pro you do need an infinite loop but put it in the MAIN function (I'm doing it that way).
It's not good to put an infinite while loop in your own functions.
Just put a while(1) loop in MAIN function, and then drag and drop your OperatorController function into the loop and it should work properly.
taggartbg
12-01-2008, 15:10
Hey guys, thanks for the help, I think i'm getting there; however, I'm still having some struggles.
I'm begining to think that I'm just doing something really simple wrong. Here is the process I undergo to test my code.
1) Code in easyC Pro in the OperatorControl function (I've also tried in Autonomous)
2) Power up robot
3) Hold down "prog" until the "Program Status" LED turns a solid orange
4) In easyC, I hit Build & Download
5) Once it is loaded, I power down the bot
6) When I power it back up, the code should automatically execute, yes?
Branden Ghena
12-01-2008, 15:17
Do you have a operator interface hooked up?
Make sure you put your OperatorControl function inside the main function.
And I don't think you have to power down your robot, it should automaticly work well.
Make sure you are not programming in competition mode (when it's divided to 3 functions). 'Cause that way I don't think it can work (I think it only works when you are in a competition [when the competition port on the OI is connected]).
Just to the following:
1) Open a regular new EasyC pro project.
2) Make your OperatorControl function (in the user functions).
3) Drag and drop the OperatorControl function you just made into a while(1) loop in the main function.
4) Power up the robot.
5) Hold down "prog" until "Program Status" LED turns orange.
6) Press Build and Download in EasyC pro.
7) Now it should work:)
I suggest you first build and download a simple program (like putting your motor in half speed) and check if it works well.
Again, check if the PWM cables are connected to the victor that is connected to the motor you want to activate.
Hope it helped.
Team 2217
laultima
12-01-2008, 15:29
Do you have a operator interface hooked up?
you do need the OI connected to the RC either by radio or tether before the code will run
taggartbg
12-01-2008, 15:33
AHH! I've been creating competition projects :o...I'll post back when I get a chance to create a standalone and try it out.
Thanks again.
Branden Ghena
12-01-2008, 15:35
A competition program will work if you plug a switch into the competition port that enables the robot.
A competition program will work if you plug a switch into the competition port that enables the robot.
Oh sorry, forgot about that..
But I'm guessing taggartbg didn't do that either (correct me if I'm wrong).
taggartbg
14-01-2008, 18:35
Hey everybody,
Thanks for all you're help, our code is up and functional now, thank you :)
/Taggart
vBulletin® v3.6.4, Copyright ©2000-2017, Jelsoft Enterprises Ltd.