'THE MAXX PROJECT - BASIC STAMP CODE - ERIC STRIDE & JAVIN ELLIFF 'CSE 466 - FALL, 2001 - UNIVERSITY OF WASHINGTON 'Note: due to heavy time contraints, this code has not been "cleaned up", some functions should 'probably be reduced or eliminated. The push button, and the remote control code should definately 'be reviewed and heavily tested. ' Constant and Variable Definitions - object avoidance & collision detection '----------------------------------------------------------------------------------- object_on_left var bit ' If this is a 0 then an object was seen on the left object_on_right var bit ' If this is a 0 then an object was seen on the right servo_left con 15 ' left servo on P15 servo_right con 14 ' right servo on P14 whisker_left var in9 ' left whisker on P9 whisker_right var in4 ' right whisker on P54 front_ir con 1 counter var word ' loop counter variable 'Constants and Variables - Remote Control------------------------------------------- ' Input l_IR_det_pin con 10 ' Left IR detector. r_IR_det_pin con 8 ' Right IR detector. ' Time center con 750 ' Center pulse width. l_fine_tune con 0 ' Fine tune center - left servo. r_fine_tune con 0 ' Fine tune center - right servo. pause_time con 20 ' Pause between pulses. sp_duration con 2000 ' Speaker duration - 3 seconds. sp_freq con 3000 ' Speaker frequency - 2 kHz IR_LED_freq con 38500 ' IR LED modulation frequency. neg_edge con 0 ' For 0-1-0 config on "pulsin". ' Variables ' I/O registers. l_IR_detect var in8 ' Reads right IR detector output. r_IR_detect var in0 ' Reads left IR detector output. ' RAM variables for data storage & calcs. data_remote var nib ' Stores remote data, replaces "direction". speed var word ' Pos => forward; neg => backward. turn var word ' Pos => right; neg => left. l_pulse var word ' Stores pulse width to left servo. r_pulse var word ' Stores pulse width to right servo. start_remote var word ' Stores the start bit. input_remote_0 var word ' Stores bit-0. input_remote_1 var word ' Stores bit-1. 'MODE & MUSIC VARIABLES------------------------------------------------------------------- mode var word but1 var bit indx var byte note var word dura var word 'MUSIC CONSTANTS************************************************************ 'Conversions of Notes to frequencies - to make songs easier to program C con 65 'Ridiculously low notes Db con 69 D con 73 Eb con 77 E con 82 F con 87 Gb con 92 G con 97 Ab con 103 A con 110 Bb con 117 BE con 124 C1 con 131 'Very low notes Db1 con 139 D1 con 147 Eb1 con 154 E1 con 165 F1 con 175 Gb1 con 185 G1 con 195 Ab1 con 207 A1 con 220 Bb1 con 234 BE1 con 248 C2 con 262 'Low notes Db2 con 278 D2 con 294 Eb2 con 307 E2 con 329 F2 con 350 Gb2 con 370 G2 con 391 Ab2 con 414 A2 con 439 Bb2 con 467 BE2 con 495 C3 con 521 'Middle 'C' Db3 con 554 D3 con 588 Eb3 con 623 E3 con 658 F3 con 694 Gb3 con 737 G3 con 781 Ab3 con 829 A3 con 877 Bb3 con 928 BE3 con 980 C4 con 1042 'High notes Db4 con 1102 D4 con 1163 Eb4 con 1240 E4 con 1316 F4 con 1389 Gb4 con 1476 G4 con 1563 Ab4 con 1658 A4 con 1754 Bb4 con 1856 BE4 con 1960 C5 con 2084 'Very high notes Db5 con 2204 D5 con 2326 Eb5 con 2480 E5 con 2632 F5 con 2778 Gb5 con 2952 G5 con 3126 Ab5 con 3316 A5 con 3508 Bb5 con 3712 BE5 con 3920 C6 con 4168 'Rediculously high notes Db6 con 4408 D6 con 4652 Eb6 con 4960 E6 con 5264 F6 con 5556 Gb6 con 5904 G6 con 6252 Ab6 con 6632 A6 con 7016 Bb6 con 7424 BE6 con 7840 R con $FF remote_led con 6 od_led con 3 '================THIS ALLOWS US TO DETERMINE WHEN THE BATTERIES ARE DIEING==== pause 2000 'Wait 2 seconds before starting mode = 2 but1 = 0 note = 0 dura = 0 indx = 0 '================THE MAIN ROUTINE============================ main: ' Main routine button 7,0,255,0,but1, 1, Pick if mode = 0 then object_dectection if mode = 1 then remote_control if mode = 2 then music goto main '==================PICK MODE ROUTINE======================= Pick: mode = mode + 1 if mode > 2 then reset_mode goto Main reset_mode: mode = 0 goto Main '================OBJECT DETECTION ROUTINE==================== object_dectection: low remote_led high od_led freqout front_ir, 1, 38500 'This flashes the left IR LED for a thousandth of a second object_on_left = in11 'This checks to see if the Left sensor saw anything freqout front_ir, 1, 38500 'This flashes the right IR LED for a thousandth of a second object_on_right = in0 'This checks to see if the right sensor saw anything if object_on_right = 0 AND object_on_left = 0 then backup 'Both sensors saw something if whisker_left = 0 and whisker_right = 0 then whisker_backup 'Both whiskers are making contact if object_on_left = 0 then turn_right 'The sensors saw something to the left if whisker_left = 0 then whisker_turn_right 'The whisker is making contact on the left, so backup and turn right if object_on_right = 0 then turn_left 'The sensors saw something to the right if whisker_right = 0 then whisker_turn_left 'The whisker on the right is making contact, so backup and turn left goto go_forward 'The sensors didn't see anything, so drive forward '================MOVEMENT ROUTINES==================== backup: pulsout servo_left,650 pulsout servo_right,850 'return goto main ' Go back to the main program turn_left: pulsout servo_left,650 pulsout servo_right,650 'return goto main ' Go back to the main program turn_right: pulsout servo_left,850 pulsout servo_right,850 'return goto main ' Go back to the main program go_forward: pulsout servo_left,850 pulsout servo_right,650 pause 10 'return goto main ' Go back to the main program '-------------------HANDLE WHISKER CONTACT--------------------------------- whisker_backup: ' backwards if both switches close for counter = 0 to 100 pulsout servo_left,650 pulsout servo_right,850 next return goto go_forward whisker_turn_left: ' turn left if right switch closes gosub whisker_backup for counter = 0 to 50 pulsout servo_left,650 pulsout servo_right,650 next goto go_forward whisker_turn_right: ' turn right if left switch closes gosub whisker_backup for counter = 0 to 50 pulsout servo_left,850 pulsout servo_right,850 next goto go_forward '====================REMOTE CONTROL ROUTINES============================== remote_control: low od_led high remote_led gosub check_remote gosub navigate gosub servo_pulse goto main '--------------------------------------------------------------------------------- check_remote: for counter = 0 to 3 pulsin l_IR_det_pin,neg_edge,start_remote pulsin l_IR_det_pin,neg_edge,input_remote_0 pulsin l_IR_det_pin,neg_edge,input_remote_1 if start_remote > 1000 then got_message next goto exit_check_remote got_message data_remote.bit0=input_remote_0.bit9 data_remote.bit1=input_remote_1.bit9 exit_check_remote: return '--------------------------------------------------------------------------------- navigate: ' Set "speed" & "turn" using "direciton". branch data_remote,[forward,backward,right,left] backward: speed = -100: turn = 0: goto exit_navigate left: speed = 0: turn = 100: goto exit_navigate right: speed = 0: turn = -100: goto exit_navigate forward: speed = 100: turn = 0: goto exit_navigate exit_navigate: return '--------------------------------------------------------------------------------- servo_pulse: ' Calculate & send pulses, then pause. l_pulse = speed + turn + center + l_fine_tune ' Calculate r_pulse = - speed + turn + center + r_fine_tune pulsout servo_left,l_pulse ' Send pulses. pulsout servo_right,r_pulse ' pause pause_time ' Pause. return '=================MUSIC INFORMATION/ROUTINES===================================== music: high remote_led high od_led gosub GetSong if note = 0 then delay if note = R then rest freqout 2, dura, note goto nxt goto Main delay: note = 0 dura = 0 indx = 0 pause 200 goto Main nxt: indx = indx + 1 goto Main rest: pause dura goto nxt GetSong gosub Song0 return Song0 'Star Wars lookup indx,[ G4, D5, C5,BE4, A4, G5, D5, C5,BE4, A4, G5, D5, C5,BE4, C5, A4,0],note lookup indx,[800,800,150,150,150,800,800,150,150,150,800,800,150,150,150,800],dura return