Servo control not sooo bad but...
Ok, so on Friday night I began testing out the servo motor more. I got the motor to move both from a program and from a potentiometer.
After taking the code from the website to make the servo move to the left then to the right I wanted to add in some buttons to control the direction. This took some figuring out but eventually I understood that the pulsewidth that the PIC is sending out must stay between two constants: minPulse and maxPulse. While I'm not totally sure if this is correct but pretending the pulseWidth is a degree element 0 degrees to 200 degrees (more like 180) it was easier to see how the variable referred to position.
here's the code for the button servo action.
===========================================
INPUT portd.1
INPUT portd.0
OUTPUT portc.3
start:
pulseWidth VAR BYTE
' set up constants with the minimum and maximum pulsewidths
minPulse CON 50
maxPulse CON 250
' set up a constant with the time between pulses:
refreshPeriod CON 20
' set an initial pulsewidth:
pulseWidth = minPulse
main:
' change the angle for the next time around:
IF (pulseWidth <= maxPulse) AND (portd.1=1) THEN
pulseWidth = pulseWidth+1
GOSUB move
HIGH portd.2
ELSE
pulseWidth = pulseWidth
LOW portd.2
ENDIF
' change the angle for the next time around:
IF (pulseWidth >= minPulse) AND (portd.0=1) THEN
pulseWidth = pulseWidth-1
GOSUB move
HIGH portd.3
ELSE
pulseWidth = pulseWidth
LOW portd.3
ENDIF
GOTO main
move:
'take the output pin low so we can pulse it high
LOW PORTC.3
' pulse the pin
PULSOUT PORTC.3, pulseWidth
' pause for as long as needed:
PAUSE refreshPeriod
RETURN
===========================================
and a button servo picture.

I got the code for the servo to be controlled by a potentiometer and tried it out.
===========================================
' Define ADCIN parameters
DEFINE ADC_BITS 10 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS
sensorValue VAR WORD ' Create variable to store result
TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %10000010 ' Set PORTA analog and right justify result
PAUSE 500 ' Wait .5 second
pulseRange VAR WORD
pulseWidth VAR BYTE
' set up constants with the minimum and maximum pulsewidths
minPulse CON 50
maxPulse CON 250
' set up a constant with the time between pulses:
refreshPeriod CON 20
' set an initial pulsewidth:
pulseWidth = minPulse
pulseRange = maxPulse - minPulse
main:
ADCIN 0, sensorValue
'take the output pin low so we can pulse it high
LOW PORTC.3
' pulse the pin
PULSOUT PORTC.3, pulseWidth
' pause for as long as needed:
PAUSE refreshPeriod
pulseWidth = minPulse + (pulseRange * (sensorValue/10)) / 100
' change the angle for the next time around:
'IF pulseWidth > maxPulse Then
' pulseWidth = minPulse
'Else
' pulseWidth = pulseWidth + 1
'Endif
GOTO main
==========================================

I have another project from sustainable energy that I am working on that involves solar panels. I will post some information about that later on but think about charging a capacitor from a solar panel and then releasing it when it reaches a desired voltage. It makes a project very organic in the sense that it totally depends on the sun for life. I think these will make incredibly interesting opportunities for advocating alternative energies.
See you all in class.
After taking the code from the website to make the servo move to the left then to the right I wanted to add in some buttons to control the direction. This took some figuring out but eventually I understood that the pulsewidth that the PIC is sending out must stay between two constants: minPulse and maxPulse. While I'm not totally sure if this is correct but pretending the pulseWidth is a degree element 0 degrees to 200 degrees (more like 180) it was easier to see how the variable referred to position.
here's the code for the button servo action.
===========================================
INPUT portd.1
INPUT portd.0
OUTPUT portc.3
start:
pulseWidth VAR BYTE
' set up constants with the minimum and maximum pulsewidths
minPulse CON 50
maxPulse CON 250
' set up a constant with the time between pulses:
refreshPeriod CON 20
' set an initial pulsewidth:
pulseWidth = minPulse
main:
' change the angle for the next time around:
IF (pulseWidth <= maxPulse) AND (portd.1=1) THEN
pulseWidth = pulseWidth+1
GOSUB move
HIGH portd.2
ELSE
pulseWidth = pulseWidth
LOW portd.2
ENDIF
' change the angle for the next time around:
IF (pulseWidth >= minPulse) AND (portd.0=1) THEN
pulseWidth = pulseWidth-1
GOSUB move
HIGH portd.3
ELSE
pulseWidth = pulseWidth
LOW portd.3
ENDIF
GOTO main
move:
'take the output pin low so we can pulse it high
LOW PORTC.3
' pulse the pin
PULSOUT PORTC.3, pulseWidth
' pause for as long as needed:
PAUSE refreshPeriod
RETURN
===========================================
and a button servo picture.

I got the code for the servo to be controlled by a potentiometer and tried it out.
===========================================
' Define ADCIN parameters
DEFINE ADC_BITS 10 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS
sensorValue VAR WORD ' Create variable to store result
TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %10000010 ' Set PORTA analog and right justify result
PAUSE 500 ' Wait .5 second
pulseRange VAR WORD
pulseWidth VAR BYTE
' set up constants with the minimum and maximum pulsewidths
minPulse CON 50
maxPulse CON 250
' set up a constant with the time between pulses:
refreshPeriod CON 20
' set an initial pulsewidth:
pulseWidth = minPulse
pulseRange = maxPulse - minPulse
main:
ADCIN 0, sensorValue
'take the output pin low so we can pulse it high
LOW PORTC.3
' pulse the pin
PULSOUT PORTC.3, pulseWidth
' pause for as long as needed:
PAUSE refreshPeriod
pulseWidth = minPulse + (pulseRange * (sensorValue/10)) / 100
' change the angle for the next time around:
'IF pulseWidth > maxPulse Then
' pulseWidth = minPulse
'Else
' pulseWidth = pulseWidth + 1
'Endif
GOTO main
==========================================

I have another project from sustainable energy that I am working on that involves solar panels. I will post some information about that later on but think about charging a capacitor from a solar panel and then releasing it when it reaches a desired voltage. It makes a project very organic in the sense that it totally depends on the sun for life. I think these will make incredibly interesting opportunities for advocating alternative energies.
See you all in class.

0 Comments:
Post a Comment
<< Home