« FINALLY - PIC of BX | Main | PIC with Transistors »
December 04, 2004
here's some code that works with beginings of flocking
hey gals, i actually just modified the code a very very small bit - carly you'll see some new variables and ways that direction is handled. but I actually just popped the h-bridge right on the existing circuit with your LEDs and switched the wires. It worked! e voila. I added 3 transistors to govern direction - right left and center, even though center doesnt seem to matter much, we might just use center to light a blinky? anyway, the code is below.
also - important note - i got rid of the Ultrasonics altogether since they were so problematic, just for simplicity. Perhaps once we get the transistors workign well, we can add the US back in. I just felt like it was holding us up and we are running out of time. Since the IR can basically do the basic avoidance behavior alone, why not just go with it and add the coherence factor already via transistors. Perhaps later we can isolate the US on their own circuit and just get them to work well on their own, THEN add them back in. Anyway, thats my vote. Good night.
'**************************************************************** '* Name : robot_motorc.BAS * '* Author : translaton of Christina Goodness BX Code, Carlyn Maw '* Notice : Copyright (c) 2004 * '* : All Rights Reserved * '* Date : 4/16/2004 * '* Version : 1.0 * '* Notes : * '* : * '****************************************************************DEFINE OSC 4
' ----------------------------------------------------- PORT SET-UP
'set port b to output:TRISB = %00000000
TRISD = %00000000'serial
TRISC = %10000000
tx var portc.6 'out
rx var portc.7 ' in'analog
' 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 uSTRISA = %11111111 ' Set PORTA to all input
ADCON1 = %10000010 ' Set PORTA analog and right justify' ----------------------------------------------------- CONSTANTS
'serial
inv9600 con 16468 ' baudmode (9600 8-N-1 inverted)
basicSpeed con 100 ' used in IR detection
' ----------------------------------------------------- VARIABLES'-- ir and ultrasonic vars (words or ints I'm not sure...)
getIR var WORD 'getIR is the IR reading
readMotion var WORD 'normalized IR reading
readMotion2 var WORD 'normalized IR readinggetSonic var WORD 'getSonic is the Ultrasonic reading
rightTransistor VAR BYTE 'getLight is Transistor the on the right
leftTransistor VAR BYTE 'getleftlight is the tranistor on the left
centerTransistor VAR BYTE 'getcenterlight is the center transistor' ----------------------------------------------------- FAKEDATA
'getSonic = 375
' ----------------------------------------------------- START MAIN
'startup sequence
main:
HIGH PORTD.2
Pause 100
LOW PORTD.2
'get valuesADCIN 0, getIR ' get the ADC value of Ultrasonic reading from pin A1
ADCIN 1, lefttransistor 'GET THE TRANSISTOR READING on the right
ADCIN 2, centerTRANSISTOR 'GET THE TRANSISTOR READING ON THE LEFT
ADCIN 3, rightTRANSISTOR 'GET THE READING ON THE CENTER TRANSISTOR
'IR goes from 5 ish (spikes up to 12ish)
' 3 inches white 480-520
' 6 inches white 360-380
' 12 inches white 190's
' 2 feet white 100's
' 3 feet white 70's'readMotion2 = ((1024 - getSonic) / 8)
readMotion = ((1024 - getIR) / 8)
'translates the values to be 127 to 60
'from the range of 12 inches to 3 inches'debug sensor infomation
'for somereason this seems to lock up for a while
SEROUT2 tx, inv9600, ["ir is ", dec readMotion, 13,10, "RIGHTLIGHT IS ", dec RIGHTTRANSISTOR, 13,10, "LEFTLIGHT IS ", dec LEFTTRANSISTOR, 13,10, "CENTERLIGHT IS ", dec CENTERTRANSISTOR, 13,10]IF (READMOTION < 60) THEN RIGHTBACKWARD
IF (READMOTION >= 60 ) AND (READMOTION <= 70 ) THEN LEFTBACKWARD
IF (readMotion < basicSpeed) Then fullBackward
If (readMotion = basicSpeed) Then fullStop
If (readMotion > basicSpeed) Then fullForward
IF (righttransistor > 200) THEN rightbackward
IF (lefttransistor > 200) THEN leftbackward
IF (CENTERTRANSISTOR > 200) THEN FULLFORWARD'check out HPWM and todd's PWM code
'http://stage.itp.nyu.edu/~tvh204/code/simplepwm.html
pause 1000goto main
fullForward:
PORTB = %01010000
goto mainfullBackward:
PORTB = %10100000
goto mainfullStop:
PORTB = %00000000
goto mainleftForward:
PORTB = %01000000
goto mainleftBackward:
PORTB = %10000000
goto mainrightForward:
PORTB = %00010000
goto mainrightBackward:
PORTB = %00100000
goto main
standStillLeft:
PORTB = %01100000
goto mainstandStillRight:
PORTB = %10010000
goto main
Posted by christina at December 4, 2004 10:48 PM