0

I am controlling a few servos, trying to record the commanded values, then play them back. In my code I add the commanded values to a list when I hit a button on my gamepad, then playback the list when I hit a second button. For some reason, when I print out the playback list, I get a list with the correct timestamp values, but the servo values are a list of repeating values (see output below). Any advice? I am sure I'm mission something obvious.

        servo_recording = []
        # Writes at 100Hz
        if (time.clock() - timestamp) >= 0.01:
            ServoWrite(kit, servo_angles)
            timestamp = time.clock()
            if record_servos:
                test = [servo_angles, timestamp]
                print(test)
                servo_recording.append(test)
            if triangle:
                # playback
                for item in servo_recording:
                    print(item)
                triangle = False

output during recording

[[90, 90.41176470588235, 92.76470588235296, 45, 90, 90, 45, 90, 90], 2.861973]
[[90, 89.94117647058823, 93.23529411764707, 45, 90, 90, 45, 90, 90], 2.87363]
[[90, 89.94117647058823, 93.23529411764707, 45, 90, 90, 45, 90, 90], 2.885585]
[[90, 88.38483529411766, 94.08575294117648, 45, 90, 90, 45, 90, 90], 2.897626]
[[90, 87.01929411764706, 95.11101176470592, 45, 90, 90, 45, 90, 90], 2.909304]
[[90, 86.02173529411763, 96.44885294117651, 45, 90, 90, 45, 90, 90], 2.921297]
[[90, 84.82023529411762, 97.6503529411765, 45, 90, 90, 45, 90, 90], 2.933315]
[[90, 83.62853529411765, 98.84205294117649, 45, 90, 90, 45, 90, 90], 2.945016]
[[90, 82.10079411764707, 99.66391176470589, 45, 90, 90, 45, 90, 90], 2.956663]
[[90, 80.66099411764702, 100.57911176470586, 45, 90, 90, 45, 90, 90], 2.968649]
[[90, 78.26919411764702, 100.57911176470587, 45, 90, 90, 45, 90, 90], 2.980399]
[[90, 76.43834705882351, 101.09106470588237, 45, 90, 90, 45, 90, 90], 2.992102]
[[90, 75.27044705882352, 102.25896470588236, 45, 90, 90, 45, 90, 90], 3.004126]
[[90, 74.06344705882351, 103.46596470588237, 45, 90, 90, 45, 90, 90], 3.015876]

output during playback

[[90, 91.58823529411765, 90.1764705882353, 45, 90, 90, 45, 90, 90], 2.861973]
[[90, 91.58823529411765, 90.1764705882353, 45, 90, 90, 45, 90, 90], 2.87363]
[[90, 91.58823529411765, 90.1764705882353, 45, 90, 90, 45, 90, 90], 2.885585]
[[90, 91.58823529411765, 90.1764705882353, 45, 90, 90, 45, 90, 90], 2.897626]
[[90, 91.58823529411765, 90.1764705882353, 45, 90, 90, 45, 90, 90], 2.909304]
[[90, 91.58823529411765, 90.1764705882353, 45, 90, 90, 45, 90, 90], 2.921297]
[[90, 91.58823529411765, 90.1764705882353, 45, 90, 90, 45, 90, 90], 2.933315]
[[90, 91.58823529411765, 90.1764705882353, 45, 90, 90, 45, 90, 90], 2.945016]
[[90, 91.58823529411765, 90.1764705882353, 45, 90, 90, 45, 90, 90], 2.956663]
[[90, 91.58823529411765, 90.1764705882353, 45, 90, 90, 45, 90, 90], 2.968649]
[[90, 91.58823529411765, 90.1764705882353, 45, 90, 90, 45, 90, 90], 2.980399]
[[90, 91.58823529411765, 90.1764705882353, 45, 90, 90, 45, 90, 90], 2.992102]
[[90, 91.58823529411765, 90.1764705882353, 45, 90, 90, 45, 90, 90], 3.004126]
[[90, 91.58823529411765, 90.1764705882353, 45, 90, 90, 45, 90, 90], 3.015876]

Any tips? Thanks!

Manuel
  • 1
  • 1
  • Each time through the loop, `servo_angles` is the *same actual object*, and it can only have one value at a given time; each time you print it during recording, it has a different value (because you just updated its contents), but at the end you print the same value multiple times through the multiple *references* (not copies) accumulated in `servo_recording`. If you have control over `ServoWrite`, it would be better to make it return a new list value rather than updating an existing one, rather than doing the copying yourself. – Karl Knechtel Sep 21 '20 at 02:43
  • And just like that, it worked. Thanks @KarlKnechtel! I did have access to ServoWrite, so I updated it to make it return a copy. You're the man! – Manuel Sep 21 '20 at 02:55

0 Answers0