![]() |
Delay10KTCYx in C18
Hello everyone I'm back again,
First of I would like to thank all who has helped me out on the recent question, and yet I'm here to bug you guys again... Currently I'm using mplab v7.60 C18 v2.40 I've successfully built VexUserCode.mcw right now I would like to run my robot autonomously I'm under the "user_routines_fast.c" and I was trying to program my robot to go straight for 5 seconds and stop using the Delay10KTCYx function in "delay.h" I tried with the code below, how did I come up with the 5000? my chip is PIC18f8520 and I check the datasheets the Fosc is 40MHz so Delay10KTCYx (5000); // should give me a delay of 10000 x 5000 x 4/40M = 5 seconds and after 5 seconds I want it to stop so I added pwm02=127; pwm03=127; is there something wrong with my code or am I using the Delay10KTCYx wrong? Thank you for your time! Code:
void User_Autonomous_Code(void) |
Re: Delay10KTCYx in C18
Your code is in the middle of a loop that NEEDS to get data every 18.5ms and return back a I'm alive message. If you wait for 5 seconds the master CPU is going to think there is a problem and reset the slave.
What you want to do is count the number of times you've been through the loop and when it's been 5 seconds (5 sec / 18.5 ms) then set the PWM values. autoseconds = autoseconds + 185; If autoseconds > 5000 then pwm = 127. |
Re: Delay10KTCYx in C18
You've got lots of options we can help you with. Time can be based on counting the number of loops within the 18.5ms loop area or based on a hardware timer.
You can learn about state machines - what we typically use in cases like these. Code:
static int counter=0; //keep track of loops to use as a crude timer - static keeps it around from call to call and isn't necessary if you never leave the subroutine. |
Re: Delay10KTCYx in C18
I have it to work thank you!!
This is what I did Code:
Also MARK can you show me how to write the Wait subroutine? I can't thank you guys enough! You've been such great help! |
Re: Delay10KTCYx in C18
That looks good.
Yes, I was just showing an elaboration on what Foster told you. Instead of the Case statement you can use If statements like you did. The only way the loop will take longer than 18.5ms is if the code you add takes more than 18.5ms to execute and slows everything down so you don't get to the "if (statusflag.NEW_SPI_DATA)" statement in time. That would take an intense time-consuming internal loop, not the sets and checks you're doing. For a simple Wait just duplicate the structure you already recognize. You want to repeat all the work that routine does, so the system doesn't lockup while you wait. I just dashed this off and haven't checked it, but this is the general idea using what you've already learned. You'd add something like this before User_Autonomous_Code. Code:
void My_Wait(int milliseconds)pwm02 = 100; |
Re: Delay10KTCYx in C18
I had similar code for when I was doing MPLabs. I had a fancy one that would let you have multiple threads and time a few things at the same time.
Ahhhh the days of rolling your own components, the sweet smell of 1's and 0's . Anyway today the RobotC compiler does all of that for you. It also has tasks that you can spawn off to do specific functions and not wait in the main line. Marks 'case' code is a good example of how we would put state machines together and have them do the heavy lifting of what the robot code needed to do. A little work with Google will get you some good tutorials on state machines. But it looks like you are on the right track of getting your robot to move. |
Re: Delay10KTCYx in C18
Thank you so much! Mark and Foster ! I seem to get the idea of controlling the timer , but the 18.5 milisecond loop is really an enigma to me, like, how did they come up with the number 18.5ms (in the code that Mark provided me there is also an if loop and it was also stated /* 18.5ms loop area */ how did you know that?)and how will we know that the code that I've written had exceeded 18.5ms
But anyway right now I've moved on, I'm using the sensors! Using Limit Switch Sensors and Bumper Sensors and here is what I did the first time Code:
Code:
I've been growing so dependent on you guys...Thank you so much for your help... |
Re: Delay10KTCYx in C18
The problem is that the buttons go from approximately 0 to ~255 and it's the approximate part that is the issue.
Try: Code:
if (signal < 20){ Code:
if (signal > 235){The 18.5 ms has to do with how long it takes the control signals from the transmitter to get to the master CPU. The signals from the transmitters are PWM with a fixed length channel. On my wiki there is the following note that I've copied from someone named Tinkerman. Quote:
http://www.vexfan.com/viewtopic.php?t=227 |
Re: Delay10KTCYx in C18
In addition to what Foster covered...
if statement comparisons need double equal signs (==). A single equal sign doesn't compare, it changes the value. So if (signal=0)changes the value of signal to zero, then evaluates if(0)which will always be False. The correct way is: if (signal==0) {The 18.5ms is more of a design choice for how often transmission packets are sent. Through the years FIRST has had some variation in packet rates, for instance, the predecessor of the Vex controller used a packet rate of 17ms, and the larger version used for FRC at that time had a rate of 26.2ms. Whenever you use something like this as a crude timer you'll have to investigate and determine what transmission rate was used. |
Re: Delay10KTCYx in C18
Thank you guys so much! I have it working!! After reading I did some adjustments and it worked just as I wanted: When the button is pressed the robot will reverse and make a turn and go straight. Here is my code for that!
Code:
|
| All times are GMT -5. The time now is 12:57. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi