Jump to content
The mkiv Supra Owners Club

Microcontroller programming, a little help!


heartworm

Recommended Posts

Got programme to do for university, not programmed a microcontroller in 4 years, anyone able to have a wee look at my program and give some advice on how to make it work correctly!

 

its a robot, two motors and it follows a line, easy. The motor to make it turn is sticking thats why its got a little more time on than the one to turn left, but I need to increase its on time more?

 

Any advice on my program at all is appreciated :D

 

It is working and following the line, just that I need to raise the speed on the right motor, and makae it initially go slower under the section of go_f so there isnt a sudden burst of speed

Link to comment
Share on other sites

I've only ever used C with PICs, and don't have that much experience. I have done motor stuff using PWM, pretty basic though really.

 

Ramping the speed when the motor starts should be really easy. Just a loop incrementing speed on each iteration.

 

I was going to get back into PICs in an attempt to make a digital speedo for the Supra.

Link to comment
Share on other sites

I've only ever used C with PICs, and don't have that much experience. I have done motor stuff using PWM, pretty basic though really.

 

Ramping the speed when the motor starts should be really easy. Just a loop incrementing speed on each iteration.

 

I was going to get back into PICs in an attempt to make a digital speedo for the Supra.

 

I used C last time I was programming them too,

 

was going to use a loop anda inc command, but due to the cheap nature of university PICs the command book doesn't have inc in it.

 

When I set the output to 255 though its much, Much slower than just setting the pins on, but 255 is maximum so it shoul dbe the same speed.

Link to comment
Share on other sites

main:

 

 

 

if input1 is on then go_r ; forward;

if input2 is on then go_f ; left

if input6 is on then go_l ; right

goto go_s ; stop as no line nearby

 

go_f:

let pins = %10100000 ; go forward

goto main

 

 

go_l:

let speedR = 200 ; maximum speed

let speedL = 200 ; maximum speed

gosub set_speed ; set the speed

let pins = %00100000 ; go left

goto main

 

go_r:

let speedR = 10000 ; maximum speed

let speedL = 10000 ; maximum speed

gosub set_speed ; set the speed

let pins = %10000000 ; go right

goto main

 

 

 

Is it your code commenting which is screwey or is this wrong?

Link to comment
Share on other sites

main:

 

 

 

if input1 is on then go_r ; forward;

if input2 is on then go_f ; left

if input6 is on then go_l ; right

goto go_s ; stop as no line nearby

 

go_f:

let pins = %10100000 ; go forward

goto main

 

 

go_l:

let speedR = 200 ; maximum speed

let speedL = 200 ; maximum speed

gosub set_speed ; set the speed

let pins = %00100000 ; go left

goto main

 

go_r:

let speedR = 10000 ; maximum speed

let speedL = 10000 ; maximum speed

gosub set_speed ; set the speed

let pins = %10000000 ; go right

goto main

 

 

 

Is it your code commenting which is screwey or is this wrong?

 

ahem, sorry, yes the comments are wrong, to make the cables less messy I changed what cable went to each of the pins,

 

should really be

 

 

if input1 is on then go_r ; right;

if input2 is on then go_f ; forward

if input6 is on then go_l ; left

goto go_s ; stop as no line nearby

Link to comment
Share on other sites

Does it have to be written in basic?

 

Here's some code from my last PIC project. It decreases motor speed (PWM) when a switch is held down.

 

 
void dc_bru_dec()
{
  if(led_state==1)                       //Test If the power flag is True
  {
     delay_ms(10);                       //debounce
     setup_ccp2(CCP_PWM);                 //setup PWM
     setup_timer_2(T2_DIV_BY_4, 255, 1);        //setup timer, Freq, reset & int
     while((bru_pwm_val>0) & (!input(bru_dec))) //if not Zero% & switch is held down, decrement duty cycle
     {
        set_pwm2_duty(bru_pwm_val);          //Set the duty cycle
        printf ("\n\r\%u",bru_pwm_val);        //Serial Output 
        bru_pwm_val--;                   //Decrement Duty Cycle
     }
  }
}

Link to comment
Share on other sites

my speed thing doesnt work, but it can make it round a track now without any sharp corners,

 

 

init: pause 100 ; motor controller start up pause

main:

 

 

 

if input1 is on then go_r ; forward;

if input2 is on then go_f ; left

if input6 is on then go_l ; right

goto go_s ; stop as no line nearby

 

go_f:

let pins = %10100000 ; go forward

goto main

 

 

go_l:

let pins = %00100000 ; go left

goto main

 

go_r:

let pins = %10000000 ; go right

goto main

 

 

go_s:

let pins = %00000000 ; stop - not over line!

goto main

 

 

 

 

 

 

works, not fantastic though,

 

Im thinking that for go_s it should do something like

 

 

stop

reverse

pause 0.1 secs

check input 1,

check input 2.

check input 6.

go_s

 

 

its dinner time first though

Link to comment
Share on other sites

symbol speedR = b1

symbol speedL = b2

 

init: pause 100 ; motor controller start up pause

main:

 

 

 

if input1 is on then go_r ; forward;

if input2 is on then go_f ; left

if input6 is on then go_l ; right

goto go_s ; stop as no line nearby

 

go_f:

let pins = %10100000 ; go forward

goto main

 

 

go_l:

let pins = %00100000 ; go left

goto main

 

go_r:

let pins = %10000000 ; go right

goto main

 

 

go_s:

let pins = %00000000 ; stop

let speedL = 60 ; set slow speed

let speedR = 60 ; set slow speed

gosub set_speed ; set the speed

let pins = %01010000 ; reverse

pause 100 ; wait .5 seconds

let pins = %00000000 ; stop

if input1 is on then go_r ; right

if input2 is on then go_f ; forward

if input6 is on then go_l ; left

goto go_s ; loop

 

set_speed:

let pins = %00110000 ‘ set left speed

pulsout 6,speedL ‘ send a pulse of length in ‘speedL’

pause 10 ‘ short delay

let pins = %11000000 ‘ set right speed

pulsout 4,speedR ‘ send a pulse of length in ‘speedR’

pause 10 ‘ short delay

return

 

 

This works, when it overruns it reverses, picks up the sensor again and follows, good on the smalle rbends, takes a few runs at the sharpest corner

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. You might also be interested in our Guidelines, Privacy Policy and Terms of Use.