{"id":3587,"date":"2015-01-26T03:31:01","date_gmt":"2015-01-26T07:31:01","guid":{"rendered":"http:\/\/www.etotheipiplusone.net\/?p=3587"},"modified":"2015-01-26T03:59:58","modified_gmt":"2015-01-26T07:59:58","slug":"ragebridge-robots-roundup-adc-auto-triggering-candy-paint-gold-teeth-refitting","status":"publish","type":"post","link":"https:\/\/www.etotheipiplusone.net\/?p=3587","title":{"rendered":"RageBridge Version 2 Roundup: ADC Auto-Triggering and Dungeon RPG Logic"},"content":{"rendered":"<p>When I last talked about RageBridge, I was attempting to write current and throttle sampling code <a href=\"https:\/\/www.etotheipiplusone.net\/?p=3505#day1\" target=\"_blank\">while on the plane to Shenzhen<\/a>. I&#8217;m proud to say that every line of that session was scrapped because\u00a0<em>none of it<\/em> made sense whatsoever. Do <em>not<\/em> attempt to code at 39,000 feet altitude after your complimentary in-flight red wine.<\/p>\n<p>But as I promised, here&#8217;s the whole story of RageBridge v2 development up until now. The revision 1 boards came a week and a half before I left for China, so I was pretty much only able to put it together while tending to the end of semester project rush. The firmware was going to be sufficiently different that I couldn&#8217;t just plop the &#8220;production&#8221; RageBridge v1 firmware onto the chip, but some basic pin toggling showed me that all the hardware does work. In fact, the version 1 board claimed its first victim by slamming a poor RS-380 motor back and forth at full power 10 times a second&#8230;<\/p>\n<p><a href=\"\/pics\/moter\/rb2_15.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"\/pics\/moter\/rb2_15-mid.jpg\" alt=\"\" width=\"640\" height=\"480\" \/><\/a><\/p>\n<p>Delicious version 1 boards arrive from Myro. These are 2oz copper boards &#8211; I wanted 4oz, but Myro informed me that their 4oz design rules need 10\/12\u00a0 (trace width \/ minimum spacing).<\/p>\n<p>Maybe I&#8217;ll take the effort to try and route it with 12 mil spacing, which would be an <em>immense<\/em> undertaking and I might as well start from scratch&#8230; anyone have any (preferably inexpensive) PCB house recommendations that can do 10\/10 in 4oz?<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"\/pics\/moter\/rb2_16.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"\/pics\/moter\/rb2_16-mid.jpg\" alt=\"\" width=\"640\" height=\"480\" \/><\/a><\/p>\n<p>The same day, I commandeered the EE lab from the <a href=\"http:\/\/fab.cba.mit.edu\/classes\/863.14\/\" target=\"_blank\">How to Make (a mess out of) Almost Anything<\/a> students to assemble the board. I had, at this point, a few doubts about the sheer density of SMT component placement, such as small passives next to the larger inductor and capacitors in the logic power supply. These were echoed a bit by <a href=\"https:\/\/www.etotheipiplusone.net\/?p=3516\" target=\"_blank\">SeeedStudio<\/a> when I showed them the board, but it is resolvable with a few minor design tweaks.<\/p>\n<p><a href=\"\/pics\/moter\/rb2_17.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"\/pics\/moter\/rb2_17-mid.jpg\" alt=\"\" width=\"640\" height=\"480\" \/><\/a><\/p>\n<p>This firmware hacking session brought to you by <a href=\"http:\/\/www.bawls.com\/\" target=\"_blank\">Bawls Guarana<\/a>. I wish you could find this stuff more readily, but if that were the case, I would have probably already died.<\/p>\n<p>Before I started toggling pins, I first had to make a new <a href=\"http:\/\/hobbyking.com\/hobbyking\/store\/__27195__Atmel_Atmega_Socket_Firmware_Flashing_Tool.html\" target=\"_blank\">Hobbyking Programming Socket Thing<\/a>. I had one that I may have lent out to someone, but forgot who it could have been, so I considered it lost. This happens to something like 1\/3rd of my stuff &#8211; lend it to a wide-eyed froshling, then immediately forget who it was. Some times, it even makes it back to me and I no longer remember the person and why I am being given something of mine that I <em>swear<\/em> was buried deep under a mountain of parts.<\/p>\n<p>One bug I ran into right away: The gate drives wouldn&#8217;t enable. A little bit of prodding with an oscilloscope led me to discover that they were not receiving a power supply. Weird, but the 5v logic rail is feeding into the chip fine, and the chip is enabled, so the charge pump should be running&#8230;<\/p>\n<p>That was when I discovered that I had mislabeled the charge pump pins when transcribing from datasheet to Eagle part.<\/p>\n<p><a href=\"\/pics\/moter\/rb2_27.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"\/pics\/moter\/rb2_27.jpg\" alt=\"\" width=\"652\" height=\"548\" \/><\/a><\/p>\n<p>On the A3941, there are a lot of doubled pins, like CP1\/CP2, VBB, GND, and the like. In my infinite wisdom, I thought that VREG needed its own capacitor like CP1\/CP2 does, instead of just needing to be 2 pins connected together, and a capacitor goes between VREG and GND.<\/p>\n<p>So that&#8217;s why the gate drive rail was not energized &#8211; no capacitor, no fun.<\/p>\n<p><a href=\"\/pics\/moter\/rb2_18.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"\/pics\/moter\/rb2_18-mid.jpg\" alt=\"\" width=\"640\" height=\"480\" \/><\/a><\/p>\n<p>Oops. Pretty much all version 1 boards need some little wire jumps. I upturned the capacitor and soldered a jumper wire to the nearest available GND pin. It will do for firmware work!<\/p>\n<p>Onto the firmware itself. For this version of Rage, I wanted to try having the ADC running synchronously with PWM, to capture a real-time current every time the controller switches. Not only does this simply offer vastly increased current control resolution for the DC controller, but synchronous sampling of current is basically required for torque control of brushless motors. There are some hackish ways around it, of course, like the Chinese e-bike controllers&#8217; hardware-implemented cycle by cycle current limiting, but if your microcontroller has the ADC horsepower, it&#8217;s possible to implement many different closed loop control methods in software. <del>And I looooove software<\/del>.<\/p>\n<p>The ATMega328&#8217;s wimpy ADC is just fast enough to do this, somewhat overclocked, at 15.6kHz PWM. It will up the temporal resolution of the Hysterical Current Limiter by more than 8x &#8211; currently, it runs 1000 times a second using whatever current reading it had last. However, because there is only 1 ADC on the chip, it has to be multiplexed to read the two current sensors on <em>alternating<\/em> PWM cycles. Therefore, the real sampling rate of the current is more like 8kHz &#8211; hence the 8x instead of 15x speedup.<\/p>\n<p>Whatever the case is, I consider it more software practice than anything for Brushless Rage.<\/p>\n<p>So here&#8217;s all the setup that had to happen, written in the form of a code novel:<\/p>\n<blockquote>\n<pre>\u00a0 \/\/Set up Timer 1 for the following:\r\n\u00a0 \/\/Change the WGM bits on TCCR1A and B to Phase Correct 9 bit PWM \r\n\u00a0 TCCR1A &amp;= ~(1 &lt;&lt; WGM10);\r\n\u00a0 TCCR1A |= (1 &lt;&lt; WGM11);\r\n\u00a0 TCCR1B &amp;= ~(1 &lt;&lt; WGM12);\r\n\u00a0 TCCR1B &amp;= ~(1 &lt;&lt; WGM13);\r\n\u00a0 \/\/Change CS1:0 bits to no prescaling (31372hz pwm if we used 8-bit PWM that is standard, 15655hz with an extra bit of resolution)\r\n\u00a0 \/\/As a result of the change to 9-bit, AnalogWrite( ) will take 0-511 instead of 0-255\r\n\u00a0 TCCR1B |= (1 &lt;&lt; CS10);\r\n\u00a0 TCCR1B &amp;= ~(1 &lt;&lt; CS11);\r\n\u00a0 TCCR1B &amp;= ~(1 &lt;&lt; CS12);\r\n\u00a0 \/\/ Enable the Timer 1 overflow interrupt which will trigger a subroutine at the Bottom of every PWM cycle (16khz)\r\n\u00a0 TIMSK1 |= (1 &lt;&lt; TOIE1); \/\/Set TOIE1 and nothing else\r\n\u00a0 \r\n\u00a0 \/\/Set up the ADC for the following:\r\n\u00a0 \/\/ Set ADC prescaler to generate a 1 MHz ADC clock; this increases the sampling speed 8x at a cost of ~ 1 LSB of resolution. \r\n\u00a0 ADCSRA = ADCSRA &amp; 0b11111000 | 0x04; \/\/Set ADSDP2, clear ADSP1:0\r\n\u00a0 \/\/ Set ADC to auto-trigger on Timer 1 overflow interrupt\r\n\u00a0 ADCSRA = ADCSRA | (1 &lt;&lt; ADIE); \/\/Set ADC conversion complete interrupt \r\n\u00a0 ADCSRA = ADCSRA | (1 &lt;&lt; ADATE); \/\/Set ADC auto trigger enable \r\n\u00a0 ADCSRB = ADCSRB &amp; 0b11111000 | 0x06; \/\/Set ADTS2 and ADTS1; clear ADCTS0, to select Timer 1 overflow as the trigger source\r\n\r\n<\/pre>\n<\/blockquote>\n<p>The way I code is to write more comments than there are lines of functioning code, in excruciating depth of exposition. I do this not only because I can&#8217;t be buggered to remember what these registers do in a week, but so other people who are forging into non-Arduino territory can more easily get what&#8217;s going on. Manual register configuration, sadly, is still how much of embedded programming works.<\/p>\n<p>(I promise to rip on manual register configuration EVERY TIME I make a software post. Every. Time.)<\/p>\n<p>Down at the bottom of this short testing code, I have the ISRs for the ADC and the PWM timer:<\/p>\n<blockquote>\n<pre>ISR(TIMER1_OVF_vect) {\r\n\u00a0 \/\/Quick Pin D5 (Port D pin 5, \"COMBINE\" on the RB2 board) toggle to indicate we are in the ISR, for debugging\r\n\u00a0 PORTD &amp;= ~(1 &lt;&lt; 5); \r\n\u00a0 PORTD |= (1 &lt;&lt; 5);\r\n\u00a0 \r\n\u00a0 slow_loop_prescale_counter++;\r\n}\r\n\r\nISR(ADC_vect) {\r\n\u00a0 \/\/Quick Pin D4 (Port D pin 4, \"MIX\" on the RB2 board) toggle to indicate we are in the ISR, for debugging\r\n\u00a0 PORTD |= (1 &lt;&lt; 4);\r\n\u00a0 PORTD &amp;= ~(1 &lt;&lt; 4);\r\n\u00a0 \r\n\u00a0 byte low, high;\r\n\u00a0 low = ADCL;\r\n\u00a0 high = ADCH;\r\n\u00a0 hot_amps = (high &lt;&lt; 8) | low;\r\n\r\n}<\/pre>\n<\/blockquote>\n<p>The result of this is the following beautiful scope photograph:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"\/pics\/moter\/rb2_19-mid.jpg\" alt=\"\" width=\"640\" height=\"480\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>The first trace up top is the PWM timer (Timer 1) overflow interrupt. This is the center of a PWM cycle, and is when the ADC is set off. A few (dozen) microseconds later, the ADC conversion complete interrupt service routine executes, which is the blue trace. At this point, a new current reading is available.<\/p>\n<p>The purple trace is the &#8216;slow loop&#8217;, which runs a few hundred times a second using a software prescaler value. This is where inputs would be taken and new outputs calculated.<\/p>\n<p><a href=\"\/pics\/moter\/rb2_20.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"\/pics\/moter\/rb2_20-mid.jpg\" alt=\"\" width=\"640\" height=\"480\" \/><\/a><\/p>\n<p>So how&#8217;s about the gate drives? Here&#8217;s a low-side trace directly at the gate of the FET. The total switching time is right around 1 microsecond&#8230; which is dismal for what I&#8217;m used to, but serviceable. Small fully-integrated driver chips are going to be wimpier than the <a href=\"http:\/\/www.irf.com\/product-info\/datasheets\/data\/irs2184.pdf\" target=\"_blank\">IRS21844s<\/a> that RageBridge v1 used, which are more often used on higher voltage systems with much larger devices.<\/p>\n<p><a href=\"\/pics\/moter\/rb2_21.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"\/pics\/moter\/rb2_21-mid.jpg\" alt=\"\" width=\"640\" height=\"480\" \/><\/a><\/p>\n<p>Some differential scope probing and a high-side gate waveform also appears. Unless you have a scope with completely isolated channels, don&#8217;t try to probe both the high and low side gates at the same time. This kills the gate.<\/p>\n<p>What happens is, non-isolated scope probe grounds basically short the high-side gate drive through the ground clips, resulting in (usually) the gate driver dying. Don&#8217;t make my past mistakes, kids. You&#8217;ve been (repeatedly) warned.<\/p>\n<p>This was the extent of the firmware work before I left for China. I tried telling myself I&#8217;d work on it while traveling, but <em>hah, yeah right<\/em>.\u00a0Looking ahead, however, I made some design changes while in Beijing, and right before leaving Japan, I fired them off. Here is board revision 2, featuring&#8230;. well, not much difference. Some component placements have been relieved, but otherwise the power side circuitry is identical.<\/p>\n<p><a href=\"\/pics\/moter\/rb2_14.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"\/pics\/moter\/rb2_14.jpg\" alt=\"\" width=\"546\" height=\"693\" \/><\/a><\/p>\n<p>The biggest &#8220;duh&#8221; moment of handling my revision 1 boards was&#8230; there are through-hole headers everywhere. If I&#8217;m going to make a plate-style heat spreader like RageBridge v1, the through-hole header elements should be as closely grouped as possible to minimize holes and cutouts. In board revision 2, I tried shoving as many of the headers to the left as possible &#8211; fortunately, with a little trickery, I got all of them.<\/p>\n<p>The &#8220;Fan&#8221; pins are still located inboard because there&#8217;s no other good place for them. The difference between the 5V supplied by this set of pins vs. what comes out of the 5V pins on the left, and the one that goes to the microcontroller, is that Fan power is taken from the &#8216;dirty side&#8217; of the regulator, before the feedback pin and the 2nd output filter. That means it&#8217;s really dirty, chunky 5V, pretty much only good for running dumb loads like fans or gaudy lighting.<\/p>\n<p>A week and some later, Revision 2 board arrives, along with&#8230;<\/p>\n<p><a href=\"\/pics\/moter\/rb2_22.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"\/pics\/moter\/rb2_22-mid.jpg\" alt=\"\" width=\"640\" height=\"480\" \/><\/a><\/p>\n<p>&#8230; Brushless Rage!<\/p>\n<p>Much to many peoples&#8217; disappointment, Brushless Rage is going to be addressed when Brushed Rage is working and out the door, because it has a more critical ship date.<\/p>\n<p><a href=\"\/pics\/moter\/rb2_23.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"\/pics\/moter\/rb2_23-mid.jpg\" alt=\"\" width=\"640\" height=\"480\" \/><\/a><\/p>\n<p>The backside of these boards is mostly made of soldermask-stop due to the high currents they&#8217;ll be conducting. Between this and the heat spreader plate will be a silicone thermal sheet layer to discourage shorting.<\/p>\n<p>I actually have not assembled the version 2 board yet, because power circuitry-wise, version 1 and version 2 are identical &#8211; only the jumper and header row is different (&#8230;and the mis-routed pins have been routed correctly&#8230;) Since I have a functioning version 1 board already, it will be the focus for firmware development, and probably the first one to get blown up.<\/p>\n<p>Since confirming that the ADC auto-trigger-upon-PWM-timer-interrupt system works, I next had to work out how to differentiate one reading from another. Here&#8217;s the issue, though: it would be easy to say &#8220;every other reading is either one or the other current sensor&#8221;. It&#8217;s not as easy to incorporate reading other analog inputs. I&#8217;m planning on building &#8220;analog mode&#8221;, for small silly rideable things, from the outset this time instead of promising to get around to it eventually.<\/p>\n<p>While it would be &#8220;easy&#8221; to have an Arduino analogRead() plow through my already set-up ADC triggers every once in a while, in the interest of more robust code structure I decided to create a convoluted state machine to have the ADC interrupt routine determine what in the name of <a href=\"http:\/\/www.atmel.com\/devices\/SAM3X8E.aspx\" target=\"_blank\">Sam<\/a> it just read.<\/p>\n<p>The basic idea is this. Every &#8220;slow loop&#8221; will request a new set of inputs (if in analog mode). The ADC ISR will merge these requests in with the current sensor reads automatically and update temporary holding variables just like it would for current. If inputs are being requested at the time the ADC is reading a current, then it will immediately reload the ADC, switch it over to the input under scrutiny, and read again, ideally finishing in time for the next current sample.<\/p>\n<p>The full state logic I wrote down literally read like a bad text-based RPG.<\/p>\n<blockquote>\n<pre>You wake up in a dark room. You are an ADC. In your hand is a conversion result.<\/pre>\n<pre>You must figure out what variable to assign this conversion result to.<\/pre>\n<pre>&gt; look<\/pre>\n<pre> On the wall besides you are three lists of the same words over and over, all scratched out<\/pre>\n<pre>except the bottom-most, and all stating that something is TRUE or FALSE.<\/pre>\n<pre>&gt; read\r\n\r\nThe list says:<\/pre>\n<pre>READING_CURRENT = FALSE;<\/pre>\n<pre>READING_CURRENT = TRUE;<\/pre>\n<pre>...<\/pre>\n<pre>and<\/pre>\n<pre>INPUTS_REQUESTED = FALSE;<\/pre>\n<pre>INPUTS_REQUESTED = TRUE;<\/pre>\n<pre>...<\/pre>\n<pre>and<\/pre>\n<pre>WHICH_CURRENT_TO_READ = FALSE;<\/pre>\n<pre>WHICH_CURRENT_TO_READ = TRUE;<\/pre>\n<pre>...<\/pre>\n<pre>It is clear the lists were made in your handwriting.<\/pre>\n<pre>You can read the last thing you write using (current), (which), or (inputs).<\/pre>\n<pre>When you are ready to guess what variable to assign your conversion result to, you can use (assign).<\/pre>\n<pre>&gt; inputs<\/pre>\n<pre>INPUTS_REQUESTED is false.<\/pre>\n<pre>&gt; current<\/pre>\n<pre>READING_CURRENT is true.<\/pre>\n<pre>&gt; which<\/pre>\n<pre>WHICH_CURRENT_TO_READ is true<\/pre>\n<pre>&gt; assign<\/pre>\n<pre>To take a guess at which variable to assign your conversion result to,<\/pre>\n<pre>type the name of the variable (ia, ib, inputs[0], inputs[1], or inputs [2])<\/pre>\n<pre>&gt; ib<\/pre>\n<pre>Before the memory bus was selected for a write, you were eaten by a grue.<\/pre>\n<p>&nbsp;<\/p><\/blockquote>\n<p>See? It&#8217;s that simple!<\/p>\n<p>The variable names are all real, and here&#8217;s how they interact.<\/p>\n<p>Each PWM timer overflow interrupt, the READING_CURRENT flag is set, and the interrupt handler immediately sets the ADC going. When the ADC finishes about 15 microseconds later, the ADC conversion complete interrupt handler executes and sees that READING_CURRENT is true.<\/p>\n<p>There are only 2 current sensors, so a simple logic inversion flag WHICH_CURRENT determines if it&#8217;s motor channel 1 or channel 2. It will save the current reading in the appropriate variable, invert the logic flag, set READING_CURRENT to false (since we&#8217;re done), and then move onto switching the ADC multiplexer to the other current sensor pin.<\/p>\n<p>Every once in a while, the slow loop will call for an INPUTS_REQUESTED = true condition. When the ADC wakes up from converting, there&#8217;s two possible things that happen:<\/p>\n<ol>\n<li>That READING_CURRENT is true and that INPUTS_REQUESTED = true, which means the next cycle is an input read and has to start immediately, or<\/li>\n<li>That READING_CURRENT is false and that INPUTS_REQUESTED = true; which means the next cycle has to be a current read since THIS one was an input read, or<\/li>\n<\/ol>\n<p>If this cycle was an input read based on the two state variables, then assign the reading to an input variable &#8211; this is in the form of an array, inputs[0:2] so it&#8217;s easy to iterate through.<\/p>\n<p>Finally, based on the condition of the two state variables, the ADC interrupt handler will reassign the multiplexer to reflect the next desired read. If it&#8217;s an input, it will start the reading immediately. If no more inputs should be read, it sets INPUTS_REQUESTED = false so that part of the logic doesn&#8217;t happen until the slow loop causes it again.<\/p>\n<p>It looks like this on a scope:<\/p>\n<p><a href=\"\/pics\/moter\/rb2_24.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"\/pics\/moter\/rb2_24-mid.jpg\" alt=\"\" width=\"640\" height=\"480\" \/><\/a><\/p>\n<p>In this scope shot, channel 1 (yellow) is reading an output PWM that I hardcoded for convenience.<\/p>\n<p>The next one down is the Timer 1 overflow interrupt handler, which sets off the ADC every PWM cycle.<\/p>\n<p>The channel 3 (purple) spike that lags each channel 2 spike by about 15 microseconds is the ADC interrupt handler executing and reading current.<\/p>\n<p>The channel 3 spike that <em>immediately follows<\/em> the rightmost two current reads are input reads. The first is inputs[0], the next inputs[1], and so on.<\/p>\n<p>Channel 4 is the &#8220;slow loop&#8221; which, in this test code, serves only to set INPUTS_REQUESTED = true in order to irritate the ADC.<\/p>\n<p>I was having trouble getting functional readings out of this setup, however. It seems like the current sensors were reading just fine, but the test inputs (which were potentiometers) were exceptionally noisy and inconsistent. I pondered for a while over this, and even refactored the above code from scratch to try and distill away any logic errors on my part.<\/p>\n<p>No change. I even tried slowing down the ADC to only 4x the default Arduino speed &#8211; perhaps my potentiometers were too high impedance and it couldn&#8217;t fill the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Successive_approximation_ADC\" target=\"_blank\">sample-and-hold capacitor<\/a> fast enough? Seemed unlikely, but worth a try.<\/p>\n<p><a href=\"\/pics\/moter\/rb2_25.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"\/pics\/moter\/rb2_25-mid.jpg\" alt=\"\" width=\"640\" height=\"480\" \/><\/a><\/p>\n<p>As the above scope photo shows, when I slowed the ADC down, the readings started running into each other and some of the input reads did not complete at all, being paved over by the externally triggered current reads.<\/p>\n<p>I became suspicious when I noticed that the input reads succeeded when I grabbed the potentiometers in the right away. Uh oh&#8230;<\/p>\n<p>To differentially diagnose, I put on some rubber gloves and tried again, figuring that my body impedance was messing with the potentiometers. Same result.<\/p>\n<p>In disbelief, I grabbed a potentiometer that I knew to be working from using it to test other motor controllers, as well as a Hall Effect twist throttle from one of the scooters. <em>Worked perfectly<\/em>.<\/p>\n<p><strong>In summary, it is absolutely possible to have <em>all three<\/em> potentiometers you harvested from the depths of the shop to be scratchy or oxidized and making poor contact, and have that make you think it&#8217;s entirely your fault that you can&#8217;t write software.<\/strong><\/p>\n<p><a href=\"\/pics\/moter\/rb2_26.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"\/pics\/moter\/rb2_26-mid.jpg\" alt=\"\" width=\"640\" height=\"480\" \/><\/a><\/p>\n<p>Five minutes later, three perfect analog readings with three perfect in-sequence reads interleaved with current sensor reads. In this scope shot, the purple trace is only the portion of the logic that handles writing the inputs variable. It executes three times for three analog inputs.<\/p>\n<p>Gee, <em>that<\/em> was a hilarious, <em>rage<\/em>-filled evening.<\/p>\n<p>At this point, I&#8217;m ready to dive into writing the<em> part of the firmware I actually care about<\/em> &#8211; taking those inputs and doing something with them. I&#8217;m going to put in analog and R\/C driving right away. Here&#8217;s the anticipated &#8216;state table&#8217; of operating modes:<\/p>\n<ul>\n<li><strong>R\/C<\/strong>: Responding to signals in the 1100us &#8211; 1900us range, since it seems like that&#8217;s what radios put out by default. Failsafing behavior will check for valid\/present pulse lengths on Channels 1 and 2, and shut down motor output if the signal is invalid.<br \/>\nIn normal mode, channel 1 controls motor A, channel 2 controls motor B.<br \/>\nIn MIX mode, channel 1 affects differential speed of the motors and channel 2 affects offset &#8211; what is usually called &#8220;Elevon&#8221; mixing, the most common configuration for robots.<br \/>\nIn COMBINE mode, by popular demand, motor A and motor B PWM % is synchronized in the timer to turn rage into a 90-180A single channel controller.<br \/>\nMIX and COMBINE both selected is not a valid mode and will default to MIX.<br \/>\nIn default and MIX modes, INVERT causes motor direction and speed to be swapped between A and B<\/li>\n<li><strong>ANALOG<\/strong>: By default, channels 1 and 2 respond to signals from 0.1 to 4.9 volts with &#8216;zero&#8217; being 2.5 volts. Voltages below 2.5 cause that motor channel to run in reverse, above in forward (for your definitions of forward and reverse). The signal bounds are for fail-safe behavior since if control signals get disconnected, it is more common for them to hit the voltage rails (0 and 5v) instead of hover somewhere in the middle.<br \/>\nChannel 3 has no function. I mean, it COULD be an Invert input, but are you really going to drive your motorized couch upside-down?<br \/>\nFailsafe behavior requires signals be at 2.5v (within the deadband) at startup, or else the signals must be brought to 2.5v first, to prevent unintended couch acceleration.<br \/>\nIn MIX mode, it assumes the use of a centered joystick or similar control setup, and its output behavior is similar to MIX mode in R\/C.<br \/>\nIn COMBINE mode, Rage becomes a 1 channel DC electric vehicle controller. The controller will respond to voltages of 1.0 to 4.0 volts, since that is the most common range for hand and pedal Hall Effect active throttles and potentiometer throttles set up as voltage instead of ohms (increasingly uncommon).<br \/>\nMotor outputs A and B are paralleled <em>by default<\/em>.<br \/>\nChannel 1 controls positive (driving) current from 0 amps to 2* IMAX amps.<br \/>\nChannel 2 controls negative (regen braking) current from 0 amps to 2 * -IMAX amps. Continually holding brake will not result in reversing. This is a bit of an unusual setup, but in my past experience, more people want their vehicle to coast by default instead of brake (drag braking) by default.<br \/>\nChannel 3 becomes a reverse switch depending on its digital logic state.<\/li>\n<li>And of course, <strong>TTL Serial<\/strong> input, which is not high priority right now but will let Rage bust into the comically large servomechanism market. I suspect I will structure this as &#8220;simple serial&#8221;, compatible with many other small robot controllers &#8211; sending byte values 1 through 127 with 63 as center (0x01 to 0x7F with 0x3F center) controls channel 1 in forward and reverse, and 128 to 255 with 192 as center (0x80 to 0xFF with 0xC0 center) respectively for channel 2.<br \/>\nSending 0x00 shuts down all motors to coasting condition.<br \/>\nIn this case, Channel 3 is used as a &#8216;device select&#8217; line.<\/li>\n<\/ul>\n<p>These expectations might change once I get started coding. In particular, with respect to the ADC readings, I have yet to try to take any samples under power. It could be that system noise from the motors and FETs switching might make individual samples worthless. That might imply some more state machine trickery, such as if I have to read currents and inputs more than once, etc.<\/p>\n<p>Right now, it&#8217;s anticipated that Rage will try to find the input mode on its own when it wakes up. After waiting for half a second upon power-up, it will&#8230;<\/p>\n<ol>\n<li>Obtain MIX or COMBINE status by reading those pins<\/li>\n<li>Check for bytes in the serial buffer; if there are, become Serial mode.<\/li>\n<li>Check for analog voltages at the 3 channel inputs, whether 1.0v or 2.5v depending on MIX\/COMBINE mode; if there is the proper voltage present, enter Analog mode.<\/li>\n<li>Barring that, sit quietly for a few more milliseconds to listen for R\/C pulsewidths; if there are valid ones, enter R\/C mode<\/li>\n<li>If all of those steps fail, blink in futility, and re-attempt these steps once per second.<\/li>\n<\/ol>\n<p>Man, it seems like &#8220;make it drive motors&#8221; is the <em>easy<\/em> part.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When I last talked about RageBridge, I was attempting to write current and throttle sampling code while on the plane to Shenzhen. I&#8217;m proud to say that every line of that session was scrapped because\u00a0none of it made sense whatsoever. Do not attempt to code at 39,000 feet altitude after your complimentary in-flight red wine. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,85],"tags":[],"class_list":["post-3587","post","type-post","status-publish","format-standard","hentry","category-bots","category-motor-controllers"],"_links":{"self":[{"href":"https:\/\/www.etotheipiplusone.net\/index.php?rest_route=\/wp\/v2\/posts\/3587","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.etotheipiplusone.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.etotheipiplusone.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.etotheipiplusone.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.etotheipiplusone.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3587"}],"version-history":[{"count":16,"href":"https:\/\/www.etotheipiplusone.net\/index.php?rest_route=\/wp\/v2\/posts\/3587\/revisions"}],"predecessor-version":[{"id":3604,"href":"https:\/\/www.etotheipiplusone.net\/index.php?rest_route=\/wp\/v2\/posts\/3587\/revisions\/3604"}],"wp:attachment":[{"href":"https:\/\/www.etotheipiplusone.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.etotheipiplusone.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.etotheipiplusone.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}