<commit_before>#!/usr/bin/env python
"""Denonlinearizing Data Collector

The denonlinearizing data collector is intended to collect data so that the
non-linear PWM -> Flow -> Motion function can be characterized and accounted for.

Usage:
  ./denonlinearizing_data_collector.py [options]

Options:
  -h, --help                              Show this help screen.
  --version                               Show the version information.
  -p=<pulse>, --pulse=<pul                Set the pulse width to sample.  [default: 4000000]
  --pwm=<pwm>                             Set the PWM pin.  P9_21.12 or P9_22.13  [default: P9_21.12]
  -f=<freq>, --frequency=<freq>           Set the feedback polling frequency in Hz.  [default: 100]
  -d=<duration>, --duration=<duration>    Set the duration of the run.  [default: 2]
  -r=<return>, --return=<return>          Set the return stroke duration.  [default: 2]
  -q, --quiet                             Supress printing the raw data.
  -s=<step>, --step=<step>                Sets the step size for the pulse sweep.  [default: 0]
"""

from stop import *
import time
import utilities

def isInFirstNintyPercent(pwmName, reading):
  return "P9_21" in pwmName and reading < .9 or "P9_22" in pwmName and .1 < reading


def doSingleMotion(pulseWidth, pwmName, sampleFreq, runLength, loud=True):
  if loud:
    print "Pin Name, Pulse Width, Speed"
  initialTime = time.time()
  initialRead = utilities.readAin()
  sleepLen = 1. / sampleFreq
  prevTime = initialTime
  prevRead = initialRead
  now = prevTime
  reading = prevRead
  deadline = initialTime + runLength
  samples = 0

  writePwm(pwmName, pulseWidth)
  writePwm("P9_14.14", utilities.PWM_PERIOD - pulseWidth)  # LED
  while time.time() < deadline and isInFirstNintyPercent(pwmName, prevRead):
    samples += 1
    prevTime = now
    prevRead = reading
    time.sleep(sleepLen)
    now = time.time()
    reading = utilities.readAin()
    if loud:
      rate = (reading - prevRead) / (now - prevTime)
      print "%s, %s, %s" % (pwmName, pulseWidth, rate)
  stopEverything()

  return (prevRead - initialRead) / (prevTime - initialTime)

def doParameterSweep(stepSize, pwmName, sampleFreq, runLength, returnLength, loud=True):
  otherPwm = "P9_21.12" if pwmName == "P9_22.13" else "P9_22.13"
  results = []
  for width in range(utilities.PWM_PERIOD, utilities.PWM_PERIOD/5, -stepSize):
    writePwm(otherPwm, 3000000)
    writePwm("P9_16.15", 2000000)  # LED<commit_msg> modified the return pwm signal<commit_after>#!/usr/bin/env python
"""Denonlinearizing Data Collector

The denonlinearizing data collector is intended to collect data so that the
non-linear PWM -> Flow -> Motion function can be characterized and accounted for.

Usage:
  ./denonlinearizing_data_collector.py [options]

Options:
  -h, --help                              Show this help screen.
  --version                               Show the version information.
  -p=<pulse>, --pulse=<pul                Set the pulse width to sample.  [default: 4000000]
  --pwm=<pwm>                             Set the PWM pin.  P9_21.12 or P9_22.13  [default: P9_21.12]
  -f=<freq>, --frequency=<freq>           Set the feedback polling frequency in Hz.  [default: 100]
  -d=<duration>, --duration=<duration>    Set the duration of the run.  [default: 2]
  -r=<return>, --return=<return>          Set the return stroke duration.  [default: 2]
  -q, --quiet                             Supress printing the raw data.
  -s=<step>, --step=<step>                Sets the step size for the pulse sweep.  [default: 0]
"""

from stop import *
import time
import utilities

def isInFirstNintyPercent(pwmName, reading):
  return "P9_21" in pwmName and reading < .9 or "P9_22" in pwmName and .1 < reading


def doSingleMotion(pulseWidth, pwmName, sampleFreq, runLength, loud=True):
  if loud:
    print "Pin Name, Pulse Width, Speed"
  initialTime = time.time()
  initialRead = utilities.readAin()
  sleepLen = 1. / sampleFreq
  prevTime = initialTime
  prevRead = initialRead
  now = prevTime
  reading = prevRead
  deadline = initialTime + runLength
  samples = 0

  writePwm(pwmName, pulseWidth)
  writePwm("P9_14.14", utilities.PWM_PERIOD - pulseWidth)  # LED
  while time.time() < deadline and isInFirstNintyPercent(pwmName, prevRead):
    samples += 1
    prevTime = now
    prevRead = reading
    time.sleep(sleepLen)
    now = time.time()
    reading = utilities.readAin()
    if loud:
      rate = (reading - prevRead) / (now - prevTime)
      print "%s, %s, %s" % (pwmName, pulseWidth, rate)
  stopEverything()

  return (prevRead - initialRead) / (prevTime - initialTime)

def doParameterSweep(stepSize, pwmName, sampleFreq, runLength, returnLength, loud=True):
  otherPwm = "P9_21.12" if pwmName == "P9_22.13" else "P9_22.13"
  results = []
  for width in range(utilities.PWM_PERIOD, utilities.PWM_PERIOD/5, -stepSize):
    writePwm(otherPwm, 1500000)
    writePwm("P9_16.15", 3500000)  # LED
{'fraction_non_alphanumeric': 0.1103030303030303, 'fraction_numerical': 0.03191919191919192, 'mean_word_length': 2.8143297380585515, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6334177', 'n_tokens_mistral': 1750, 'n_tokens_neox': 1615, 'n_words': 482}