Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

PC interrupts

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-ARDUINO
View previous topic :: View next topic  
Author Message
drejansen

Bascom Member



Joined: 10 Jun 2006
Posts: 26
Location: Hoeksche Waard

netherlands.gif
PostPosted: Fri Oct 24, 2014 9:58 pm    Post subject: PC interrupts Reply with quote

English below.

Geacht forum,
ik heb een ernstig probleem met interrupts.
ik heb twee sensoren, die de omwentelingen van een wiel meten.
ik weet niet hoe dit aan te pakken, heb wel iets opgestart
Het gaat om het uitlezen van de sensoren die zijn aangesloten op de analoge poorten C.1 en C.2

ook start ik de hartslag die onafhankelijk van de overige taken blijft tikken.
deze hartslag bepaald hoe lang ik een knopje indruk en vervolgens wisselt hij telkens de interrupt van sensor 1 naar sensor 2
dus na elke hartslag is de andere sensor in de gelegenheid om een interrupt te genereren.
de robot rijdt niet zo snel dat er een puls wordt overgeslagen, dus ik mis geen pulsen
de hartslag timer kan sneller, maar dat is volgens mij niet nodig.

ik wil sensorpulsen tellen en aan de hand daarvan een robot een vooraf bepaalde weg laten afleggen.
om onduidelijke redenen lukt dat niet, ik kan pulsen tellen, en de PWM signalen zodanig afstellen dat elke motor op gelijke snelheid draait. bij een tweede run wijken de snelheden weer enorm af, ik weet niet hoe dat kan/komt.

uiteindelijk wil ik een regeling maken waarmee de sensoren aan de motoren de snelheid bepalen.
maar dat is van later zorg.

vooralsnog wil ik dat de motoren op gelijke snelheid draaien (rechtuit rijden) en op een bepaald moment een bocht maken.
die bocht wordt gemaakt door het PWM signaal van één van de motoren te minderen, net zo lang tot de bocht is gemaakt.
ook dat is van later zorg, want eerst gewoon rechtuit rijden.

het gaat hier om de Arduino Uno, met het Arduino motorshield.
ik ben dus gebonden aan de aansluitingen op dit bordje

er zijn drie knopjes, aangesloten op D.2 D.4 en D.7
een schakelaar naar massa, er is in de knop zelf een pull-up weerstand opgenomen, zodat dit op het bord niet meer nodig is.

de sensoren zijn aangesloten op de analoge ingangen C.2 en C.3 die worden gebruikt als PC interrupt 10 en 11
als je allebei de ingangen open zet dan krijg je van allebei de sensoren, door elkaar, een interrupt.
door in de hartslag timer in het register te wisselen tussen 4 en 8 worden afwisselend naar C.2 of C.3 gekeken.

er is iets met flanken waarop de interrupt reageert, maar daar kan ik geen vinger achter krijgen.
mogelijk is daar iets mee te doen.
ik wil twee variabelen sensl en sensr uitlezen.

dan is er nog de pauze, ik moet na de schakelaartjes in het hoofdprogramma 20 ms wachten, zo niet dan werken de schakelaars niet.
heb jij hier een verklaring voor?

hoe kan ik dat het beste aanpakken?

door knop -1- langer dan 20 hartslagen ingedrukt te houden (een paar seconden) wordt de bedoelde opdracht gestart
motor links draait met PWM 900 en motor rechts met PWM 965 dan draaien ze even snel
vervolgens krijg ik van de sensoren de waarden als de linker sensor 50 pulsen heeft gegeven.
dit werkt, maar ergens klopt er iets niet, dat is mijn probleem.
er is iets mis met het tellen van de pulsen

knop -1- kort = stoppen
knop -1- lang = starten meetcyclus hier zit mijn probleem, een goede meting uitvoeren.
knop -2- kort = achteruit
knop -2- lang = vooruit
knop -3- kort = stoppen
knop -3- lang = beide motoren op PWM 800


onderaan mijn gedachten spinsel.

==================

Hi,
I have a serious problem with interrupts.
I have two sensors, which measure the revolutions of a wheel.
I do not know how to do this right, I have started something
It involves the reading of the sensors that are connected to the analogue ports C.1 and C.2

Also I start the heartbeat that keeps ticking. independently of the other tasks
this heartrate determines how long I push a button and it switches each interrupt from sensor 1 to sensor 2
after each heartbeat, the other sensor in can make an interrupt.
heartbeat timer can be faster, but that is not necessary. I think

I want to count sensor pulses and that decides the path of the robot.
for unknown reason, fails that I can count pulses, and the PWM signals adjusted such that each motor at the same speed. in a second run the speeds of the motors are very different.
I do not understand why.

eventually I want to make an system that regulate the engines speed acquiring to the sensors.
but that's for later.

yet I want the motors run at the same speed (drive straight ahead) and make a bend at any given time.
that bend is made by lowering the PWM signal of one of the motors, just as long until the turn is made. But that is also for later, because first just drive straight ahead.

this is the Arduino Uno, and the Arduino motorshield.
I am therefore bound to the connectors on this board

there are three buttons, connected to D.2 D4 and D7
a switch to ground, the pull-up resistor is in the button, so this is no longer needed on the board.

The sensors are connected to the analog inputs C.2 and C.3 used as PC interrupt 10 and 11
if you open both entrances then you get both of the sensors together, an interrupt.
by heart rate change between 4 and 8 timer in the register are alternately looked at C.2 and C.3.

there is something which flanks the interrupt response, but I do not understand how to do this. Maby it is possible to do anything with this.
I want to read the two variables sensl and sensr.

Then there's the pause of 20 ms, I need after the switches in the main program, if not then the switches do not work.
do you have an explanation for this?

how can I best tackle this problem?

by keeping button one for longer than 20 heartbeats (a few seconds) pressed the measure program starts.
motor left running with PWM 900 and PWM motor right with 965, then they rotate the same speed.
Then I get the values of the sensors when the left sensor has. 50 pulses
this works, but somewhere something is wrong, that's my problem.
something is wrong with counting the pulses

short = normal push long = hold for a few secconds
button -1- short press = stop
button -1- long press = start measuring cycle here is my problem, perform a proper measurement.
button -2- short press = Reverse
button -2- long press = forward
button -3- sort = stop both motors
button -3- long = motors run on PWM 800


the program:

======================================

$regfile = "m328pdef.dat" 'de micro controller
$crystal = 16000000 'kristalfrequentie
$baud = 19200 'communicatie snelheid
Print "{012}" 'scherm schoonmaken
Config Timer1 = Pwm , Pwm = 10 , Prescale = 1 , Compare A Pwm = Clear Down , Compare B Pwm = Clear Down
'timer 1 zit op de rem, die is via een nor op de andere H-brug zijde aangesloten

Config Timer2 = Timer , Prescale = 1024 'hartslag timer
Enable Timer2
Enable Interrupts

On Ovf2 Hartslag_isr 'interrupt routine hartslag
On Pcint1 Sensor_isr 'interrupt routine odometrie sensoren
Portc = &B00001100 'pull up c 2&3 inschakelen
Set Pcifr.pcif1 'PC interrupt voor sensoren
Enable Pcint1 'maak PC interrupt mogelijk

'Motor L
Config Pinb.5 = Output 'richting L 0 = vooruit
Reset Portb.5 'bij opstarten vooruit
Config Pinb.3 = Output 'enable L
Set Portb.3 'geef de motor vrij

'Motor R
Config Pinb.4 = Output 'richting R 0 = vooruit
Reset Portb.4 'bij opstarten vooruit
Config Pind.3 = Output 'enable R PWM 2B
Set Portd.3 'geef de motor vrij

Knop1 Alias Pind.2 'ingang poort knopje 1
Knop2 Alias Pind.4 'ingang poort knopje 2
Knop3 Alias Pind.7 'ingang poort knopje 3
Snelhr Alias Pwm1a 'motor links
Snelhl Alias Pwm1b 'motor rechts
Richtingr Alias Portb.4
Richtingl Alias Portb.5

Declare Sub Knopeenkort 'wat te doen bij knop 1
Declare Sub Knopeenlang 'knop lang ingedrukt
Declare Sub Knoptweekort 'wat te doen bij knop 2
Declare Sub Knoptweelang 'knop lang ingedrukt
Declare Sub Knopdriekort 'wat te doen bij knop 3
Declare Sub Knopdrielang 'knop lang ingedrukt

Dim Knp1 As Byte 'knopje 1 indruktijd
Dim Knp2 As Byte 'knopje 2 indruktijd
Dim Knp3 As Byte 'knopje 3 indruktijd
Dim Knp1oud As Byte 'knopje 1 oude indruktijd
Dim Knp2oud As Byte 'knopje 2 oude indruktijd
Dim Knp3oud As Byte 'knopje 3 oude indruktijd
'oude indruktijd is nodig om te kunnen bepalen of de knop nog steeds is ingedrukt.
Dim Sensl As Word 'motorsensor links
Dim Sensloud As Word 'motorsensor links oud
Dim Sensr As Word 'motorsensor rechts
Dim Sensroud As Word 'motorsensor rechts oud
'ook hier oud en nieuw om het verschil te kunnen bepalen.

'====================================hoofdprogramma
Do
' knopjes
If Knp1 > 3 Then 'om valse pulsen uit te sluiten
If Knp1oud = Knp1 Then 'knop is losgelaten
If Knp1 < 20 Then Knopeenkort 'uitvoeren kort ingedrukt
If Knp1 >= 20 Then Knopeenlang 'uitvoeren lang ingedrukt
Knp1 = 0 'resetten knopstand
End If
Knp1oud = Knp1 'wachten tot knop is losgelaten
End If

If Knp2 > 3 Then 'knop -2-
If Knp2oud = Knp2 Then
If Knp2 < 20 Then Knoptweekort
If Knp2 >= 20 Then Knoptweelang
Knp2 = 0
End If
Knp2oud = Knp2
End If

If Knp3 > 3 Then 'knop -3-
If Knp3oud = Knp3 Then
If Knp3 < 20 Then Knopdriekort
If Knp3 >= 20 Then Knopdrielang
Knp3 = 0
End If
Knp3oud = Knp3
End If
Waitms 20
'noodzakelijke wachttijd, minimaal 20 ms anders werken de knopjes niet.
Loop
End

'============================== hartslag interrupt
Hartslag_isr:
If Knop1 = 0 Then Knp1 = Knp1 + 1 'knoppen uitlezen en indruktijd noteren
If Knop2 = 0 Then Knp2 = Knp2 + 1
If Knop3 = 0 Then Knp3 = Knp3 + 1

If Pcmsk1 = 4 Then '8 = links 4 = rechts sensoren
Pcmsk1 = 8 'kijk naar linker sensor
Else
Pcmsk1 = 4 'kijk naar rechter sensor
End If
Return

'=============================== sensor interrupts
Sensor_isr:
If Pcmsk1 = 8 Then 'hier wordt de interrupt aan sensor links toegewezen
Sensl = Sensl + 1 'is er een interrupt, dan wordt de teller opgehoogd
Sensloud = Sensl 'voor vergelijk, hier doe ik nog even niets mee
End If
If Pcmsk1 = 4 Then 'hier wordt de rechter sensor aan de interrupt gehangen
Sensr = Sensr + 1 'bij een interrupt wordt rechts opgehoogd
Sensroud = Sensr 'ook hier moet nog over worden nagedacht
End If
Return


'==============================knoppen uitvoering
'knoppen uitvoering
Knopeenkort: 'motoren stil zetten
Snelhl = 0
Snelhr = 0
Return

Knopeenlang: 'motoren test en sensor tempo
Do
Snelhl = 900 'snelheid linker motor
Snelhr = 965 'snelheid rechts
Loop Until Sensl > 50 'na 50 pulsen op de linker sensor
Snelhl = 0 'motor stop
Snelhr = 0 'motor stop
Print Sensl ; " " ; Sensr 'print sensorwaarden
Return

Knoptweekort: 'richting achteruit
Richtingr = 1
Richtingl = 1
Return

Knoptweelang: 'richting vooruit
Richtingr = 0
Richtingl = 0
Return

Knopdriekort: 'stil zetten
Snelhr = 0
Snelhl = 0
Return

Knopdrielang: 'motoren snelheid 800 PWM
Snelhr = 800
Snelhl = 800
Return

End
===============================================

greetings, Dré Jansen

_________________
Pluk het moment!
een hele dag lukt toch niet.
 
Groeten, Dré Jansen
 
 
Back to top
View user's profile
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 1161
Location: France

france.gif
PostPosted: Sat Oct 25, 2014 7:06 pm    Post subject: Reply with quote

hello,
I didn't speak Dutch, sorry

but I try to read you program
with Basic Bascom I put the label isr after the END line (see the help)
----------
in the isr I let only a flag :

Myisr1:
flagmotor =1
return

and in the main loop
If Flagmotor= 1 then
---
---
Flagmotor=0 'don't forget this line !
endif

like that the µP has time to follow the change.
-----------
also try to use select case
it use less time than If then ...
-----------
and It seems you mix declare sub and gosub, please read the help about it

You call a sub and you finish it with End sub
you call a label from a gosub and you go up with return

read also the help about Config Submode = New and try to use this new mode

Bon courage
JP Wink
Back to top
View user's profile Visit poster's website
drejansen

Bascom Member



Joined: 10 Jun 2006
Posts: 26
Location: Hoeksche Waard

netherlands.gif
PostPosted: Sun Oct 26, 2014 11:08 pm    Post subject: PC interrupts Reply with quote

Hi Duval JP,

Thanks for your answer,
I will follow your advice, but I am a beginner, so the most of it I do not understand.
Hopefully it will clear to me during the reading of the help file.
Best regards,

Dré

_________________
Pluk het moment!
een hele dag lukt toch niet.
 
Groeten, Dré Jansen
 
 
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5921
Location: Holland

blank.gif
PostPosted: Tue Oct 28, 2014 9:09 pm    Post subject: Reply with quote

hi Dre,

the interrupts you use are the PCINT interrupts. These create an interrupt at every change on the pin. no matter if the pin is in input or output mode. The PCINT is a great addition to the normal INT0/INT1 etc. interrupt sources. But there is 1 catch : you need to check which pin caused the interrupt in case you have multiple interrupts.
In your code you switch between the 2 interrupts by changing the mask (PCMSK1). This is a nice way to know what caused the interrupt. But it is not so nice that you can only server 1 source this way.
So my advise would be : enable both interrupt sources and in the sensir_isr , determine which pin(s) caused the interrupt.

Code:
Sensor_isr:
     If pinc.2 = 0 Then                              'hier wordt de interrupt aan sensor links toegewezen
      Sensl = Sensl + 1                                'is er een interrupt, dan wordt de teller opgehoogd
      Sensloud = Sensl                                 'voor vergelijk, hier doe ik nog even niets mee
    End If
    If Pinc.3 = 0 Then                              'hier wordt de rechter sensor aan de interrupt gehangen
      Sensr = Sensr + 1                              'bij een interrupt wordt rechts opgehoogd
      Sensroud = Sensr                               'ook hier moet nog over worden nagedacht
   End If
Return


I did not check if pinc.2 or pinc.3 is used for left/right.

now you will not miss info from the sensor.

_________________
Mark
Back to top
View user's profile Visit poster's website
drejansen

Bascom Member



Joined: 10 Jun 2006
Posts: 26
Location: Hoeksche Waard

netherlands.gif
PostPosted: Wed Oct 29, 2014 10:23 pm    Post subject: interrupts Reply with quote

Hi Mark,

Thanks for the response, but it does not work properly.
When I program as you suggested, then I get very much pulses.
I turn the wheel by hand, so it should give just a few pulses.

First I tried direct interrupts, bud they also does not work properly.
What do I wrong?

Best regards,
Dré

_________________
Pluk het moment!
een hele dag lukt toch niet.
 
Groeten, Dré Jansen
 
 
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-ARDUINO All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum