legoEv3/ev3dev2/control/__pycache__/GyroBalancer.cpython-35.pyc



h^[J@sbdZddlZddlZddlZddlZddlZddlZddlZddlm    Z    ddl
mZddlm
Z
mZmZddlmZmZddlmZddlmZejeZejd    Zd
ZeeZd
ZeeZdZedd
Z e eZ!dZ"dZ#Gddde$Z%Gddde&Z'dS)aModule for a robot that stands on two wheels and uses a gyro sensor.

The robot (eg. BALANC3R) will to keep its balance and move in response to
the remote control. This code was adapted from Laurens Valk's script at
https://github.com/laurensvalk/segway.

N)deque)PowerSupply)
LargeMotorOUTPUT_AOUTPUT_D)
GyroSensorTouchSensor)Sound)OrderedDictg333333?ih<c@seZdZdZddddddddd    d
eeddd

ZddZddZddZ    ddZ
ddZddZddZ
ddZdddd d!Zdd"d#Zdd$d%Zdd&d'Zdd(d)Zd*d+ZdS),GyroBalancerzt
    Base class for a robot that stands on two wheels and uses a gyro sensor.

    Robot will keep its balance.
    ix    g @g?FcCs||_||_||_||_||_||_||_||_|    |_|
|_    t
|_t|_
|j
j|j
_t|_t||_t||_t|_t|j
jdd|_t|jjdd|_t|jjdd|_t|jjdd|_t|jjdd|_t|jjdd|_tj |_!t"j#|_$|
|_%t&j&t&j'|j(t&j&t&j)|j*dS)zCreate GyroBalancer.z/value0rbz    /positionz/duty_cycle_spwN)+gain_gyro_anglegain_gyro_rategain_motor_anglegain_motor_angular_speed"gain_motor_angle_error_accumulatedpower_voltage_nominalpwr_friction_offset_nomtiming_loop_msecmotor_angle_history_lengthgyro_drift_compensation_factorrpower_supplyrZgyroZMODE_GYRO_RATEmodertouchr
motor_leftmotor_rightr    soundopen_path    gyro_file
touch_fileencoder_left_fileencoder_right_filedc_left_file
dc_right_filequeueZQueuedrive_queue    threadingZEventstop_balancedebugsignalSIGINTsignal_int_handlerSIGTERMsignal_term_handler)selfrrrrrrrr r!r"Zleft_motor_portZright_motor_portr5r<>/usr/lib/python3/dist-packages/ev3dev2/control/GyroBalancer.py__init__Us>                                            zGyroBalancer.__init__cCsy|jj|jj|jj|jj|jj|jj|j    j|j
j|jjdS)z+Close all file handles and stop all motors.N)r4setr&stopr'r+closer,r-r.r/r0)r;r<r<r=shutdowns







zGyroBalancer.shutdowncCs)|jdt|jjjS)z,Function for fast reading from sensor files.r)seekintreaddecodestrip)r;Zinfiler<r<r=
_fast_reads
zGyroBalancer._fast_readcCs4|jd|jtt||jdS)z)Function for fast writing to motor files.rN)truncatewritestrrDflush)r;Zoutfilevaluer<r<r=_fast_writes
zGyroBalancer._fast_writecCsktt||}|dkr8td||}n|dkrWtd||}|j||dS)z-Function to set the duty cycle of the motors.rdNi)rDroundminmaxrN)r;Zmotor_duty_fileZdutyfriction_offsetvoltage_compZduty_intr<r<r=    _set_dutyszGyroBalancer._set_dutycCs$tjd|jtdS)zSignal handler for SIGINT.z"Caught SIGINTN)loginforBGracefulShutdown)r;signumframer<r<r=r8s

zGyroBalancer.signal_int_handlercCs$tjd|jtdS)zSignal handler for SIGTERM.z"Caught SIGTERMN)rVrWrBrX)r;rYrZr<r<r=r:s

z GyroBalancer.signal_term_handlercCs#tjd|j}|jdS)z$Run the _balance method as a thread.targetN)r3ZThread_balancestart)r;Zbalance_threadr<r<r=balanceszGyroBalancer.balancec$
Csyxrdrt|jjrt|jj|jj|jj|jjd}d}d}d}d}d}d}d}d}    d}
d}d}d}
tjd|jj    d|j
j|jj
}|j|}tt|j|}|jd}d}tdg|j}|j|t}tjdtjdd}x7t|D])}|
|j|j}
tjd    q]W|
|}
tjd
t|
tjdtjdtjdtjdtjd|jjtj}|jr=t}t}||d
<t}||d<d}d\}}tj}x;|r|jjr|d7}y&|j j!\}}|j j"Wnt#j$k
rYnX|j|j%}|j|j&|j|j'd}|t(}|j|j}
d}x-||krFtj|}tjdqWtj|}tj}|jrtj|} |
|| <|d|| <|
|
t}|t)}|||}||}||d|j|}|}|j*||j+||j,||j-||j.||j/|}    |j0|j1|    ||||j0|j2|    ||||||}d||
||
}
|||}q^Wtj}!|j3|j2d|j3|j1dx|j
j4rtjd    qW|!||}"tjdt|"ddtjdtjdtjd|jrt5dd}#t6j7||#WdQRXqWdS)zMake the robot balance.Trz0Hold robot upright. Press touch sensor to start.zPress touch sensor to start.iz#-----------------------------------zCalibrating...rOg{Gz?z
gyro_offset: zGO!zPress Touch Sensor to re-start.
loop_times
gyro_readingsFrg@gMbP?g@@z
Loop time:ZmsZSTOPzdata.txtrN)rr)8r4Zis_setr&resetr'Z
run_directrVrWr(Zspeakr%Z
wait_for_bumpr#Zmeasured_voltsrrDrPrr rr!r"RAD_PER_SEC_PER_RAW_GYRO_UNITrangerHr+timesleeprKZbeepr5r
r2Z
get_nowaitZ    task_doner1ZEmptyr,r-r.RAD_PER_RAW_MOTOR_UNITRAD_PER_SEC_PER_PERCENT_SPEEDappendrrrrrrUr0r/rNZ
is_pressedr)jsondump)$r;Zmotor_angle_rawZmotor_angleZmotor_angle_refZmotor_angle_errorZmotor_angle_error_accZmotor_angular_speedZmotor_angular_speed_refZmotor_angular_speed_errorZmotor_duty_cycleZ
gyro_rate_rawZ    gyro_rateZgyro_est_angleZgyro_offsetZvoltage_idlerTrSZloop_time_targetZ
loop_countZmotor_angle_histZgyro_drift_comp_rateZgyro_calibrate_countiZprog_start_timedatar_r`Z
touch_pressedspeedsteeringZloop_start_timeZ    loop_timeZtime_of_sampleZ
prog_end_timeZ
avg_loop_timeZ    data_filer<r<r=r\s

















            
    


    



+




    zGyroBalancer._balancerNcCsP|jj||f|dk    r?tj||jjd|jjdS)zMove robot.Nr)rr)r2Zputrdrejoin)r;rmrnsecondsr<r<r=_moves

zGyroBalancer._movecCs |jdtddd|dS)zMove robot forward.rmrnrrpN)rq    SPEED_MAX)r;rpr<r<r=move_forwardszGyroBalancer.move_forwardcCs!|jdtddd|dS)zMove robot backward.rmrnrrpN)rqrr)r;rpr<r<r=
move_backwardszGyroBalancer.move_backwardcCs |jdddtd|dS)zRotate robot left.rmrrnrpN)rq    STEER_MAX)r;rpr<r<r=rotate_leftszGyroBalancer.rotate_leftcCs!|jdddtd|dS)zRotate robot right.rmrrnrpN)rqru)r;rpr<r<r=rotate_rightszGyroBalancer.rotate_rightcCs|jdddddS)z%Stop robot (balancing will continue).rmrrnN)rq)r;r<r<r=r@szGyroBalancer.stop)__name__
__module____qualname____doc__rrr>rBrHrNrUr8r:r^r\rqrsrtrvrwr@r<r<r<r=rNs:@rc@seZdZdZdS)rXz(Custom exception for SIGINT and SIGTERM.N)rxryrzr{r<r<r<r=rXsrX)(r{Zloggingrdrir1r3Zmathr6collectionsrZev3dev.powerrZev3dev.motorrrrZev3dev.sensor.legorrZev3dev.soundr    r
Z    getLoggerrxrVZpiZRAD_PER_DEGZDEG_PER_SEC_PER_RAW_GYRO_UNITrbZDEG_PER_RAW_MOTOR_UNITrfZRPM_PER_PERCENT_SPEEDZDEG_PER_SEC_PER_PERCENT_SPEEDrgrrruobjectr    ExceptionrXr<r<r<r=<module>s6