Pastebin
API
tools
faq
paste
Login
Sign up
Please fix the following errors:
New Paste
Syntax Highlighting
#!/usr/bin/python3 from pathlib import Path from uio.utils import fix_ctypes_struct from uio.ti.icss import Icss from uio.ti.pwmss import Pwmss import ctypes from ctypes import c_uint32 as uint32, c_uint16 as uint16, c_int32 as int32, c_int16 as int16, c_int8 as int8 from time import sleep from sys import exit import numpy as np import csv class Gpio: def __init__( self, name ): self.name = name self._value_path = Path( '/dev/gpio', name, 'value' ) def get_value( self ): return int( self._value_path.read_text() ) def set_value( self, value ): self._value_path.write_text( str( value ) ) class Pru0_Poll_Vars( ctypes.Structure ): _fields_ = [ ("position", uint32), ] class Message( ctypes.Structure ): _fields_ = [ ( 'id', uint32 ), ('timestamp', uint32), ('position', uint32), ('control_signal', int32), ('counter', uint32), ('force', int16), ('motor_effort', int16) ] # used to communicate ddr layout to C program class DDRLayout( ctypes.Structure ): _fields_ = [ ( 'msgbuf', uint32 ), ( 'num_msgs', uint16 ), ( 'msg_size', uint16 ), ] # volatile variables in pruss shared memory class SharedVars( ctypes.Structure ): _fields_ = [ ( 'abort_file', uint32 ), ( 'abort_line', uint32 ), ( 'ridx', uint16 ), ( 'signal', uint16*1000), ] data_file = open('control_data.csv', 'w', newline='') writer = csv.writer(data_file) header = ['timestamp', 'motor_input','position', 'motor_effort'] writer.writerow(header) pruss = Icss( "/dev/uio/pruss/module" ) pruss.initialize() ddr = pruss.ddr pruss.uart.initialize(baudrate = 460800) frequency= 10000 divider = 1 period = round( 100e6 / divider / frequency ) motor = Pwmss( "/dev/uio/pwmss1" ).pwm motor.initialize(period,divider) motor_direction = Gpio('motor_dir') core_1 = pruss.core1 core_1.load( 'fw-c/pid_control.out' ) core_0 = pruss.core0 core_0.load( 'fw/decoder.bin' ) pru0_vars = pruss.dram0.map( Pru0_Poll_Vars ) # if you don't want the ringbuffer at the start of the ddr region, specify offset here MSGBUF_OFFSET = 0 # you can use a fixed ringbuffer size: #NUM_MSGS = 1024 # or you can scale the ringbuffer to fit the size of the ddr region: NUM_MSGS = ( ddr.size - MSGBUF_OFFSET - ctypes.sizeof( uint16 ) ) // ctypes.sizeof( Message ) NUM_MSGS = min( NUM_MSGS, 65535 ) # make sure number of messages fits in u16 # map shared memory variables ddr_layout = core_1.map( DDRLayout, 0x10000 ) shmem = core_1.map( SharedVars, 0x10100 ) msgbuf = ddr.map( Message * NUM_MSGS, MSGBUF_OFFSET ) ddr_widx = ddr.map( uint16, MSGBUF_OFFSET + ctypes.sizeof( msgbuf ) ) # inform pru about layout of shared ddr memory region ddr_layout.msgbuf = ddr.address + MSGBUF_OFFSET ddr_layout.num_msgs = NUM_MSGS ddr_layout.msg_size = ctypes.sizeof( Message ) # local copies of read-pointer and write-pointer ridx = 0 widx = 0 id_value = 0 timestamp_cycles = 0 # initialize pointers in shared memory shmem.ridx = ridx ddr_widx.value = widx line_count = 0 with open('./double_hump_profile.csv','r') as f: signal_reader = csv.reader(f) for line in signal_reader: shmem.signal[line_count] = int(line[0]) line_count += 1 print(f' Loaded {line_count} lines in the file') # ready, set, go! pruss.ecap.pwm.initialize( 2**32 ) core_0.run() pru0_vars.position = 0 core_1.run() def check_core(): if not core_1.halted: return if core_1.state.crashed: msg = f'core crashed at pc={core_1.pc}' elif shmem.abort_file == 0: msg = f'core halted at pc={core_1.pc}' else: # FIXME figure out better way to read C-string from PRU memory abort_file = core_1.read( ctypes.c_char * 32, shmem.abort_file ).value abort_file = abort_file.decode("ascii") msg = f'core aborted at pc={core_1.pc} ({abort_file}:{shmem.abort_line})' # dump some potentially interesting information: msg += f'\n ridx = {ridx}' msg += f'\n shmem.ridx = {shmem.ridx}' msg += f'\n ddr_widx = {ddr_widx.value}' exit( msg ) lastid = 0 def recv_messages(): global ridx, widx, lastid, timestamp_cycles # read updated write-pointer widx = ddr_widx.value assert widx < NUM_MSGS # sanity-check if ridx == widx: return # no messages received # process messages while ridx != widx: # note: it may be faster to copy a batch of messages from shared memory # instead of directly accessing individual messages and their fields. msg = msgbuf[ ridx ] # sanity-check that message id increments monotonically lastid = ( lastid + 1 ) & 0xffffffff assert msg.id == lastid # get 32-bit timestamp (in cycles) from message and unwrap it: timestamp_cycles += ( msg.timestamp - timestamp_cycles ) & 0xffffffff # process rest of message position = msg.position force = msg.force motor_input = msg.control_signal motor_effort = msg.motor_effort counter = msg.counter if (motor_effort > 0): motor_direction.set_value( 0 ) else: motor_direction.set_value( 1 ) # consume message and update read pointer del msg # direct access to message forbidden beyond this point ridx += 1 if ridx == NUM_MSGS: ridx = 0 shmem.ridx = ridx # update user interface ts_ms = timestamp_cycles // 200000 ts_s = ( ts_ms % 60000 ) / 1000 ts_m = ts_ms // 60000 ts_h = ts_m // 60 ts_m = ts_m % 60 test = timestamp_cycles *-1 test2 = timestamp_cycles // 200000 data = [f'{ts_m:02d}:{ts_s:06.3f}', f'{motor_input:+09d}',f'{position:+09d}', f'{motor_effort:+09d}', f'{counter:+05d}'] writer.writerow(data) print(f'\rtime={ts_h:02d}:{ts_m:02d}:{ts_s:06.3f} position={position:08d} force={force:08d} signal = {motor_input:+09d} effort = {motor_effort:+09d}', end='', flush = True ) try: while True: recv_messages() check_core() sleep( 0.01 ) except KeyboardInterrupt: pass finally: print( '', flush=True ) core_0.halt() core_1.halt() pruss.uart.io.write( b'FFV\r', discard=True, flush=True ) # interrupt continuous transmission sleep( 0.01 ) # make sure that response has been received pruss.uart.io.discard() # discard response control_signal += A[0] * error[0] + A[1] * error[1] + A[2] * error[2]; if( control_signal < 0 ) { if( control_signal < -(4000 << 16) ) control_signal = -(4000 << 16); EPWM1A = (-control_signal) >> 16; gpio_set_one_low( &GPIO0, 31 ); } else { if( control_signal > (4000 << 16) ) control_signal = (4000 << 16); EPWM1A = control_signal >> 16; gpio_set_one_high( &GPIO0, 31 ); }
Optional Paste Settings
Category:
None
Cryptocurrency
Cybersecurity
Fixit
Food
Gaming
Haiku
Help
History
Housing
Jokes
Legal
Money
Movies
Music
Pets
Photo
Science
Software
Source Code
Spirit
Sports
Travel
TV
Writing
Tags:
Syntax Highlighting:
None
Bash
C
C#
C++
CSS
HTML
JSON
Java
JavaScript
Lua
Markdown (PRO members only)
Objective C
PHP
Perl
Python
Ruby
Swift
4CS
6502 ACME Cross Assembler
6502 Kick Assembler
6502 TASM/64TASS
ABAP
AIMMS
ALGOL 68
APT Sources
ARM
ASM (NASM)
ASP
ActionScript
ActionScript 3
Ada
Apache Log
AppleScript
Arduino
Asymptote
AutoIt
Autohotkey
Avisynth
Awk
BASCOM AVR
BNF
BOO
Bash
Basic4GL
Batch
BibTeX
Blitz Basic
Blitz3D
BlitzMax
BrainFuck
C
C (WinAPI)
C Intermediate Language
C for Macs
C#
C++
C++ (WinAPI)
C++ (with Qt extensions)
C: Loadrunner
CAD DCL
CAD Lisp
CFDG
CMake
COBOL
CSS
Ceylon
ChaiScript
Chapel
Clojure
Clone C
Clone C++
CoffeeScript
ColdFusion
Cuesheet
D
DCL
DCPU-16
DCS
DIV
DOT
Dart
Delphi
Delphi Prism (Oxygene)
Diff
E
ECMAScript
EPC
Easytrieve
Eiffel
Email
Erlang
Euphoria
F#
FO Language
Falcon
Filemaker
Formula One
Fortran
FreeBasic
FreeSWITCH
GAMBAS
GDB
GDScript
Game Maker
Genero
Genie
GetText
Go
Godot GLSL
Groovy
GwBasic
HQ9 Plus
HTML
HTML 5
Haskell
Haxe
HicEst
IDL
INI file
INTERCAL
IO
ISPF Panel Definition
Icon
Inno Script
J
JCL
JSON
Java
Java 5
JavaScript
Julia
KSP (Kontakt Script)
KiXtart
Kotlin
LDIF
LLVM
LOL Code
LScript
Latex
Liberty BASIC
Linden Scripting
Lisp
Loco Basic
Logtalk
Lotus Formulas
Lotus Script
Lua
M68000 Assembler
MIX Assembler
MK-61/52
MPASM
MXML
MagikSF
Make
MapBasic
Markdown (PRO members only)
MatLab
Mercury
MetaPost
Modula 2
Modula 3
Motorola 68000 HiSoft Dev
MySQL
Nagios
NetRexx
Nginx
Nim
NullSoft Installer
OCaml
OCaml Brief
Oberon 2
Objeck Programming Langua
Objective C
Octave
Open Object Rexx
OpenBSD PACKET FILTER
OpenGL Shading
Openoffice BASIC
Oracle 11
Oracle 8
Oz
PARI/GP
PCRE
PHP
PHP Brief
PL/I
PL/SQL
POV-Ray
ParaSail
Pascal
Pawn
Per
Perl
Perl 6
Phix
Pic 16
Pike
Pixel Bender
PostScript
PostgreSQL
PowerBuilder
PowerShell
ProFTPd
Progress
Prolog
Properties
ProvideX
Puppet
PureBasic
PyCon
Python
Python for S60
QBasic
QML
R
RBScript
REBOL
REG
RPM Spec
Racket
Rails
Rexx
Robots
Roff Manpage
Ruby
Ruby Gnuplot
Rust
SAS
SCL
SPARK
SPARQL
SQF
SQL
SSH Config
Scala
Scheme
Scilab
SdlBasic
Smalltalk
Smarty
StandardML
StoneScript
SuperCollider
Swift
SystemVerilog
T-SQL
TCL
TeXgraph
Tera Term
TypeScript
TypoScript
UPC
Unicon
UnrealScript
Urbi
VB.NET
VBScript
VHDL
VIM
Vala
Vedit
VeriLog
Visual Pro Log
VisualBasic
VisualFoxPro
WHOIS
WhiteSpace
Winbatch
XBasic
XML
XPP
Xojo
Xorg Config
YAML
YARA
Z80 Assembler
ZXBasic
autoconf
jQuery
mIRC
newLISP
q/kdb+
thinBasic
Paste Expiration:
Never
Burn after read
10 Minutes
1 Hour
1 Day
1 Week
2 Weeks
1 Month
6 Months
1 Year
Paste Exposure:
Public
Unlisted
Private
Folder:
(members only)
Password
NEW
Enabled
Disabled
Burn after read
NEW
Paste Name / Title:
Create New Paste
Hello
Guest
Sign Up
or
Login
Sign in with Facebook
Sign in with Twitter
Sign in with Google
You are currently not logged in, this means you can not edit or delete anything you paste.
Sign Up
or
Login
Public Pastes
Untitled
JSON | 1 hour ago | 1.15 KB
2026_06_updated_lune
10 hours ago | 1.82 KB
other setups
13 hours ago | 0.15 KB
Untitled
13 hours ago | 0.16 KB
DROP20
19 hours ago | 1.44 KB
API 'ping' test
Python | 1 day ago | 0.53 KB
Slide notes
1 day ago | 2.46 KB
AMP floater example implementation
1 day ago | 2.32 KB
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the
Cookies Policy
.
OK, I Understand
Not a member of Pastebin yet?
Sign Up
, it unlocks many cool features!