Discovery Module RC Incomplete (py)

# This template was written by David M. Harrison, Dept.
# of Physics, Univ. of Toronto in February 2008.

# Load the graphics library
from visual.graph import *

# Load the controls library for the toggle switch
from visual.controls import *

# The voltage of the battery, the charge on the plates of the
# capacitor, the value of the capacitance, and the value of the
# resistance.

V = 6.0
q = 0.
C = 0.20
R = 5.0

# the timestep

dt = 0.01

# Create a list of (time, voltage) values. The voltages
# are all zero, and the times are all negative.

num_values = 10 * int(float(1/dt))
cv_values = []
for i in range(num_values):
    cv_values.append( ((i - num_values)*dt, 0))

# These are the possible states of the switch in the circuit.

charge = 1
discharge = 0

# Initially the switch is set to discharge the capacitor.

switchvalue = discharge

# Below we create a toggle switch. This code changes the value of
# the switch in the circuit when the toggle is clicked.
# Computer geeks calls this a "callback".

def toggleswitch():
    # In callbacks, to reference variables in the main
    # part of the program they must be declared global.
    global switchvalue, charge, discharge

    if tggl.value == 1:
        switchvalue = charge
    else:
        switchvalue = discharge

# Create the controls window and put the toggle switch in it.

ctrl = controls(title = "Control the Switch", width = 250, height = 250 )
tggl = toggle(text0 = "Discharge", text1 = "Charge",
              action=lambda: toggleswitch() )

######
# Create the window for the circuit picture. This window has no
# functionality, but provides feedback as to the state of the circuit.
# Doing graphics with text like this is painful, but ...
#####

circuit_window = display(title = 'The Circuit', width = 600, height = 300,
                         x = 300, y = 0 )

battery = box(display = circuit_window, color = (0.8,0.8,0.8), length = 100,
              width = 50, height = 100, pos = (-200, 0, 0))
# Put a label on the battery. Note that in the construct:
#      text = 'Battery %1.2f V' %V
# the value of the voltage set above is formatted into the label.
# Similarly for the labels for the capacitor and resistor below, the
# values are not hard-coded but use the previously set values.
bat_label = label(display = circuit_window,pos = (-210, 0, 0),
                  text = 'Battery %1.2f V' %V)

capacitor = cylinder(display = circuit_window, axis = (0,0,20), radius = 20,
            color = color.yellow, pos = (245, 0, 0))
cap_label = label(display = circuit_window,pos = (175, 0,0),
                  text = 'Capacitor %1.2f F' %C)

resistor = cylinder(display = circuit_window, axis = (50, 0, 0), radius = 15,
                    color = color.cyan, pos = (0, -100, 0))
r_label = label(display = circuit_window,pos = (25, -65, 0),
                text = 'Resistor %1.2f Ohms' %R)

bottom_wire = cylinder(display = circuit_window, axis = (450, 0, 0), radius = 2,
                       pos = (-200, -100, 0))
left_wire = cylinder(display = circuit_window, axis = (0, 200, 0), radius = 2,
                     pos = (-200, -100, 0))
right_wire = cylinder(display = circuit_window, axis = (0, 150, 0), radius = 2,
                      pos = (250, -100, 0))

# This wire goes to the switch. Put a dot on the end of it.
center_wire = cylinder(display = circuit_window, axis = (300, 0, 0), radius = 2,
                       pos = (-50, 50, 0))
s1 = sphere(display = circuit_window, radius = 5, pos = (-50, 50, 0) )

# The wire from the bottom_wire up to the switch with a dot on the end of it
w1 = cylinder(display = circuit_window, axis = (0, 100, 0), radius = 2,
              pos = (-100, -100, 0) )
s2 = sphere(display = circuit_window, radius = 5, pos = (-100, 0, 0) )

# The wire from the left to the switch with a dot on it
w2 = cylinder(display = circuit_window, axis = (100, 0, 0), radius = 2,
              pos = (-200, 100, 0))
s3 = sphere(display = circuit_window, radius = 5, pos = (-100, 100, 0) )

# Now the switch. The axis corresponds to a length of the switch of 75,
# at a 45 degree angle to the horizontal
switch = cylinder( display = circuit_window, axis = (-53, -53, 0), radius = 2,
                   pos = (-50, 50, 0) )

#####
# End of drawing the circuit
#####

#####
# Create the graphics window and put a plot of cv_valuse in it
#####

graph = gdisplay(title = 'Voltage across the capacitor', width = 600, height = 300, x = 0,
                 y = 320, ytitle = 'V (V)', xtitle = 't (s)', ymax = V)
# The name "myplot" is used below to update the graph.
myplot = gdots(display = graph, pos = cv_values, color = color.red)

#####
# End of setting up the graphics window
#####

while 1 == 1:

    # the rate of the animation in frames per second
    rate(1/dt)

    # This causes VPython to poll the toggle switch
    ctrl.interact()

    if switchvalue == charge:

        # Rotate the switch
        switch.axis = (-53, 53, 0)

        # Calculate the change in the charge on the plates

        dq = XXX * dt

    else: # The capacitor must be discharging

        # Rotate the switch
        switch.axis = (-53, -53, 0)

        # Calculate the change in the charge on the plates

        dq = YYY * dt

    q = q + dq

    # The voltage across the capacitor

    cv = q/C

    # Now we update the graph. We only change the voltage values,
    # replacing the i-th value with the i+1 one, and putting the
    # current value of the the voltage into the last position

    for i in range(num_values - 1):
        myplot.dots[i].pos[1] = myplot.dots[i + 1].pos[1]
    myplot.dots[num_values - 1].pos[1] = cv