Bifocal Model: Circuits

No preview image

1 collaborator

23606_672257653113_207361_37716054_4088633_n Nicole Hallinen (Author)

Tags

(This model has yet to be categorized with any tags)
Model group Stanford-BBA-Spr10 | Visible to everyone | Changeable by everyone
Model was written in NetLogo 4.1 • Viewed 170 times • Downloaded 4 times • Run 2 times
Download the 'Bifocal Model: Circuits' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


WHAT IS IT?

This section could give a general understanding of what the model is trying to show or explain.

HOW IT WORKS

This section could explain what rules the agents use to create the overall behavior of the model.

HOW TO USE IT

This section could explain how to use the model, including a description of each of the items in the interface tab.

THINGS TO NOTICE

This section could give some ideas of things for the user to notice while running the model.

THINGS TO TRY

This section could give some ideas of things for the user to try to do (move sliders, switches, etc.) with the model.

EXTENDING THE MODEL

This section could give some ideas of things to add or change in the procedures tab to make the model more complicated, detailed, accurate, etc.

NETLOGO FEATURES

This section could point out any especially interesting or unusual features of NetLogo that the model makes use of, particularly in the Procedures tab. It might also point out places where workarounds were needed because of missing features.

RELATED MODELS

This section could give the names of models in the NetLogo Models Library or elsewhere which are of related interest.

CREDITS AND REFERENCES

This section could contain a reference to the model's URL on the web if it has one, as well as any other necessary credits or references.

Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

extensions [gogo]
globals
[
  raw-inner-width
  raw-inner-height
  raw-outer-width
  raw-outer-height
  raw-parallel-width
  raw-parallel-height
  new-px
  new-py
  speed
  leftE
  rightE
  topE
  bottomE
  TotalR
  r1
  r2
  Resistance1
  Resistence2
  serial-port
  BatteryVoltage
]
breed [ electrons electron ]

to gogo-ping
  carefully
  [ if not gogo:ping
    [ user-message "Unable to ping GoGo Board." ] ]
  [ user-message error-message ]
end 

to setup
  set serial-port user-one-of "Select a port:" gogo:ports
  gogo:open serial-port
  repeat 5
  [ if not gogo:ping
    [ user-message "The GoGo Board is not responding." ] ]
  gogo:talk-to-output-ports [ "a" "b" "c" "d" ]
  
  
  
  ca
  set speed 0.5 
  setup-battery
  
  if Parallel
  [
  setup-parallel-bulbs
    
  set-default-shape electrons "circle"
  
  set raw-parallel-width  70
  set raw-parallel-height 30
  
  set raw-outer-width  80
  set raw-outer-height 80
 
  
  make-outside-box
  make-parallel-boxes
  set-default-shape electrons "circle"
  ]
  if not Parallel
  [
    setup-series-bulbs
  set-default-shape electrons "circle"
  
  set raw-inner-width  70
  set raw-inner-height 70
  
  set raw-outer-width  80
  set raw-outer-height 80
  
  
  make-outside-box
  make-inside-box
  set-default-shape electrons "circle"
  ]
end 

to setup-battery
  crt 1
  [set color red
  set shape "i beam"
  set size 20
  set speed 0
  setxy 0 -75]
end 

to setup-parallel-bulbs
  crt 1
  [set color yellow
  set shape "ufo top"
  set size 25
  set label "Lightbulb 2"
  set speed 0
  setxy 0 75]
  
  crt 1
  [set color yellow
  set shape "ufo top"
  set size 25  
  set label "Lightbulb 1"
  set speed 0
  setxy 0 5]
  
  
  crt 1
  [set color green
  set shape "crate"
  set size 25  
  set label "Resistance 1"
  set speed 0
  setxy -40 5]
  
  crt 1
  [set color green
  set shape "crate"
  set size 25  
  set label "Resistance 2"
  set speed 0
  setxy -40 75]
end 

to setup-series-bulbs
  crt 1
  [set color yellow
  set shape "ufo top"
  set size 25
  set label "Lightbulb 1"
  set speed 0
  setxy -75 0]
  
  crt 1
  [set color yellow
  set shape "ufo top"
  set size 25
  set label "Lightbulb 2"
  set speed 0
  setxy 75 0]
  
  crt 1
  [set color green
  set shape "crate"
  set size 25  
  set label "Resistance 1"
  set speed 0
  setxy -40 75]
  
  crt 1
  [set color green
  set shape "crate"
  set size 25  
  set label "Resistance 2"
  set speed 0
  setxy 40 75]
end 

to make-outside-box
  ask patches with [((abs pxcor = raw-outer-width) and (abs pycor <= raw-outer-height)) or
                    ((abs pycor = raw-outer-height) and (abs pxcor <= raw-outer-width))]
    [ set pcolor blue ]
end 

to make-inside-box
  ask patches with [((abs pxcor = raw-inner-width) and (abs pycor <= raw-inner-height)) or
                    ((abs pycor = raw-inner-height) and (abs pxcor <= raw-inner-width))]
    [ set pcolor blue ]
end 

to make-parallel-boxes
  ask patches with [((abs pxcor = raw-parallel-width) and (pycor <= raw-parallel-height + 40)) and
                    ((abs pxcor = raw-parallel-width) and (pycor >=  (-(raw-parallel-height) + 40))) or
                    ((pycor = raw-parallel-height + 40) and (abs pxcor <= raw-parallel-width)) or
                    ((pycor = (-(raw-parallel-height) + 40)) and (abs pxcor <= raw-parallel-width))]
  [set pcolor blue]
    
   ask patches with [((abs pxcor = raw-parallel-width) and (pycor <= raw-parallel-height - 30)) and
                    ((abs pxcor = raw-parallel-width) and (pycor >=  (-(raw-parallel-height) - 40))) or
                    ((pycor = raw-parallel-height - 30) and (abs pxcor <= raw-parallel-width)) or
                    ((pycor = (-(raw-parallel-height) - 40)) and (abs pxcor <= raw-parallel-width))]
   
  [set pcolor blue]
end 

to make-electrons
  if Parallel
  [
 ;;   let leftE1 BatteryVoltage / 5 * (100 - TotalR)
    let leftE2 BatteryVoltage / 5 * (100 - Resistence2)
    let middleE BatteryVoltage / 5 * (100 - Resistance1)
    let rightE2 leftE2
    set topE leftE2
    set bottomE (leftE2 + middleE)
    let leftE1 bottomE
    let rightE1 leftE1
;;    set TotalR ((Resistance1 * Resistence2) / (Resistance1 + Resistence2))
  create-electrons topE [
    random-position-top-wire
    set color green ]
  create-electrons bottomE [
    random-position-bottom-wire
    set color yellow
  ]
  
  create-electrons middleE [
    random-position-middle-wire
    set color green
  ]
    
  create-electrons leftE1 [
    random-position-leftE1-wire
    set color yellow
  ]
  create-electrons leftE2 [
    random-position-leftE2-wire
    set color green
  ]
  create-electrons rightE1 [
    random-position-rightE1-wire
    set color yellow
  ]
  create-electrons rightE2 [
    random-position-rightE2-wire
    set color green
  ]
  ]
  
  if not Parallel
  [
    set TotalR (Resistance1 + Resistence2)
    set leftE BatteryVoltage / 5 * (200 - TotalR)
    set rightE BatteryVoltage / 5 * (200 - TotalR)
    set topE BatteryVoltage / 5 * (200 - TotalR)
    set bottomE BatteryVoltage / 5 * (200 - TotalR)
  
  create-electrons leftE [
    random-position-left-wire
    set color green
  ]
  create-electrons rightE [
    random-position-right-wire
    set color green
  ]
  create-electrons topE [
    random-position-top-wire
    set color green
  ]
  create-electrons bottomE [
    random-position-bottom-wire
    set color green
  ]
  ]
end 

to random-position-left-wire
  if (((1 - raw-outer-width)  + random-float (raw-outer-width - 2)) < raw-inner-width)
  [
    setxy ((1 - raw-outer-width)  + random-float 8)
    ((1 - raw-outer-height) + random-float (2 * raw-outer-height - 2))
  ]
end 

to random-position-leftE1-wire
  if (((1 - raw-outer-width)  + random-float (raw-outer-width - 2)) < raw-inner-width)
  [
    setxy ((1 - raw-outer-width)  + random-float 8)
    ((1 - raw-outer-height) + random-float (raw-outer-height - 2))
  ]
end 

to random-position-leftE2-wire
  if (((1 - raw-outer-width)  + random-float (raw-outer-width - 2)) < raw-inner-width)
  [
    setxy ((1 - raw-outer-width)  + random-float 8) random-float (raw-outer-height - 2)
;;    ((1 - raw-outer-height) + random-float (raw-outer-height - 2))
  ]
end 

to random-position-right-wire
    setxy (71 + random-float 8)
    ((1 - raw-outer-height) + random-float (2 * raw-outer-height - 2))
end 

to random-position-rightE1-wire
    setxy (71 + random-float 8)
    ((1 - raw-outer-height) + random-float (raw-outer-height - 2))
end 

to random-position-rightE2-wire
    setxy (71 + random-float 8)
;;    ((1 - raw-outer-height) + random-float (raw-outer-height - 2))
    random-float (raw-outer-height - 2)
end 

to random-position-top-wire
    setxy ((1 - raw-outer-width) + random-float (2 * raw-outer-width - 2))
    (71 + random-float 8)
end 

to random-position-bottom-wire
    setxy ((1 - raw-outer-width) + random-float (2 * raw-outer-width - 2))
    (- 71 - random-float 8)
end 

to random-position-middle-wire
    setxy ((1 - raw-outer-width) + random-float (2 * raw-outer-width - 2))
    (1 + random-float 8)
end 

to go
  convert-resistance
  set BatteryVoltage ((gogo:sensor 5) / 40)
  if gogo:sensor 5 < 50
  [
    set BatteryVoltage 0.5
  ]
  ask electrons
  [ die]
  make-electrons
  ;;calculate-speed
  ask electrons [
    bounce ]
  if Parallel [
  if (Resistance1 >= 0) and (Resistance1 >= 19)
  [set r1 45 ]
  if (Resistance1 >= 20) and (Resistance1 >= 39)
  [set r1 46 ]
  if (Resistance1 >= 40) and (Resistance1 >= 59)
  [set r1 47 ]
  if (Resistance1 >= 60) and (Resistance1 >= 79)
  [set r1 48 ]
  if (Resistance1 >= 80) and (Resistance1 >= 100)
  [set r1 49 ]
  if (Resistence2 >= 0) and (Resistence2 >= 19)
  [set r2 45 ]
  if (Resistence2 >= 20) and (Resistence2 >= 39)
  [set r2 46 ]
  if (Resistence2 >= 40) and (Resistence2 >= 59)
  [set r2 47 ]
  if (Resistence2 >= 60) and (Resistence2 >= 79)
  [set r2 48 ]
  if (Resistence2 >= 80) and (Resistence2 >= 100)
  [set r2 49 ]
  
  ask turtles [
  if bulb1? [ set color r1 ]
  if bulb2? [ set color r2 ]
  ]
  ]
;;  ask electrons [    move ]
end 

to convert-resistance
   set Resistance1 (100 - ((gogo:sensor 1) - 23) / 10)
   set Resistence2 (100 - ((gogo:sensor 6) - 23) / 10)
end 

to-report bulb1?
  report who = 1
end 

to-report bulb2?
  report who = 2
end 

to calculate-speed
  set speed ((100 - Resistance1) / 500)
end 

to bounce
  if ((abs xcor < 80) and (abs xcor > 70)) ;; left or right
  [
    ;; get the coordinates of the patch we'll be on if we go forward 1
  let new-patch patch-ahead 1
  set new-px [pxcor] of new-patch
  set new-py [pycor] of new-patch
  ; if we're not about to hit a wall (yellow patch) or piston (orange patch)
  ; we don't need to do any further checks
  ; check: hitting left or right wall?
  if (abs new-px = 70) or (abs new-px = 80)
    ; if so, reflect heading around x axis
    [
      ;;  if the particle is hitting a vertical wall, only the horizontal component of the speed
      ;;  vector can change.  The change in velocity for this component is 2 * the speed of the particle,
      ;; due to the reversing of direction of travel from the collision with the wall
      set heading (- heading) ]
  ; check: hitting top or bottom wall? (Should never hit top, but this would handle it.)
  if (abs new-py = 80)
  [
    ;;  if the particle is hitting a horizontal wall, only the vertical component of the speed
    ;;  vector can change.  The change in velocity for this component is 2 * the speed of the particle,
    ;; due to the reversing of direction of travel from the collision with the wall
    set heading (180 - heading)
  ]
  fd speed
  ]
 if (abs xcor < 70)  ;; top or bottom or middle wire
  [
    ;; get the coordinates of the patch we'll be on if we go forward 1
  let new-patch patch-ahead 1
  set new-px [pxcor] of new-patch
  set new-py [pycor] of new-patch
  ; if we're not about to hit a wall (yellow patch) or piston (orange patch)
  ; we don't need to do any further checks
  ; check: hitting left or right wall?
  if (abs new-px = 69) 
    ; if so, reflect heading around x axis
    [
      ;;  if the particle is hitting a vertical wall, only the horizontal component of the speed
      ;;  vector can change.  The change in velocity for this component is 2 * the speed of the particle,
      ;; due to the reversing of direction of travel from the collision with the wall
      set heading (- heading) ]
  ; check: hitting top or bottom wall? (Should never hit top, but this would handle it.)
  if (abs new-py = 70) or (abs new-py = 80)
  [
    ;;  if the particle is hitting a horizontal wall, only the vertical component of the speed
    ;;  vector can change.  The change in velocity for this component is 2 * the speed of the particle,
    ;; due to the reversing of direction of travel from the collision with the wall
    set heading (180 - heading)
  ]
  if (abs new-py = 0) or (abs new-py = 10)
  [
    ;;  if the particle is hitting a horizontal wall, only the vertical component of the speed
    ;;  vector can change.  The change in velocity for this component is 2 * the speed of the particle,
    ;; due to the reversing of direction of travel from the collision with the wall
    set heading (180 - heading)
  ]
  fd speed
  ]
end 

to move  ;; particle procedure
   jump 1
end 

There is only one version of this model, created almost 9 years ago by Nicole Hallinen.

Attached files

No files

This model does not have any ancestors.

This model does not have any descendants.