Shop OBEX P1 Docs P2 Docs Learn Events
Flip and propellerC language; RESOLVED - Page 7 — Parallax Forums

Flip and propellerC language; RESOLVED

123457

Comments

  • Here is the updated code that will process the serial data coming from the Arduino. The problem is that the values from the Arduino are not scaled.
    0 = 0/255 = 0%
    100 = 100/255 = 39%
    150 = 150/255 = 59%
    200 = 200/255 = 78%
    250 = 250/255 = 98%

    On the Propeller these translate to
    0 = 0/600 = 0%
    100 = 100/600 = 17%
    150 = 150/600 = 25%
    200 = 200/600 = 33%
    250 = 250/600 = 42%
    
    /*
      Blank Simple Project.c
      http://learn.parallax.com/propeller-c-tutorials 
    */
    #include "simpletools.h" 
    #include "propeller.h"                     // Include simple tools
    #include "fdserial.h"
    
    
    #define RmotorPin 17
    #define DmotorPin 19
    #define RdirPin 18
    #define DdirPin 20
    #define FDRX 0
    #define FDTX 1
    
    
    fdserial *fd;
    int b1, b2, b3, b4;
    
    int ck;
    int data;
    char Buffer[32];
    
    int i;  
    
    int main()
    { 
      low(RmotorPin);                 //<-- set pin as output
      low(RdirPin);                   //<-- default direction
      low(DmotorPin);
      low(DdirPin);
      
      fd = fdserial_open(FDRX, FDTX, 0, 115200);
      
      clkset(0x6b,5000000);           //<-- Not needed, does nothing.
      
      pwm_start(600);                 //<-- generate a pulse every 600 milliseconds
      //pwm_start(20000);
     
      i = 0;
      while(1)
      {
        ck = fdserial_rxCheck(fd);    //<-- if we have serial data use it
        while(fdserial_rxCheck(fd) > 0)
        {
          Buffer[i++] = fdserial_rxChar(fd); //<-- Capture one of the four bytes
        }
        
        if (i > 0)                   //<-- we have serial data
        {
          if (i == 4)                //<-- valid serial packet?
            data = 'A';
          else
            data = ' ';
          i = 0;
          print("%d,%d,%d,%d\n", Buffer[0], Buffer[1], Buffer[2], Buffer[3]);
        }
    
        b1 = input(3);               //<-- get button input which overrides serial
        b2 = input(4);
        b3 = input(5);
        b4 = input(6); 
        
        if (b1 == 1)
          data = 'W';
         
        if (b2 == 1)
          data = 'N';
         
        if (b3 == 1)
          data = 'E';
         
        if(b4 == 1)
          data = 'S';
         
        switch(data)
        {
          case 'W':
             high(RdirPin);                //<-- set direction before starting motor
             pwm_set(RmotorPin,0,500);     //<-- turn on motor to 83% - 500/600
             if (b1 == 0)                  //<-- button released
              data = ' ';
             break;
             
          case 'N': 
             high(DdirPin);
             pwm_set(DmotorPin,1,500);
             if (b2 == 0)
              data = ' ';
             break;
             
          case 'E':
             low(RdirPin);
             pwm_set(RmotorPin,0,500);
             if (b3 == 0)
              data = ' ';
             break;
             
          case 'S':
             low(DdirPin);
             pwm_set(DmotorPin,1,500);
             if (b4 == 0)
              data = ' ';
             break;
             
          case 'A':
             set_output(RdirPin, Buffer[1]);
             set_output(DdirPin, Buffer[3]);
             pwm_set(RmotorPin, 0, Buffer[0]);
             pwm_set(DmotorPin, 1, Buffer[2]);
             break;
             
          default:                          //<-- no buttons or serial data all stop
             pwm_set(RmotorPin,0,50);       //<-- set motor speed to 8% - 50/600
             pwm_set(DmotorPin,1, 0);       //<-- set motor to stop 0% - 0/600
             low(RdirPin);                  //<-- set direction to default
             low(DdirPin);
             break;
        }
        pause(250);                        //<-- wait 250 milliseconds or 1/4 of a second.
      }
    }
    

    Mike
  • I tried the new program but I don't see anything in the IDE monitor and n movement in motors when I press buttons on the hand controller but the switches on the propeller do illicit reaction on the motors as needed.
  • There could be few things wrong with serial data. This is where a Serial Dongle comes in handy. You can test with hardware that works.

    1) The Arduino is 5v and the Propeller is 3.3v which means you need a level shifter otherwise you will damage the Propeller.
    2) The wrong pins are being used. What pin is 2 and 3 now?
    3) Tx and Rx are reversed. Are they saying this is the Tx pin or that the Tx pin goes here or what?

    I see you are using soft serial to output to the Propeller. The documentation shows SoftwareSerial softserial(Rx, Tx), but your code is showing just the opposite. "SoftwareSerial serial_flip(txpin,rxpin);".

    Maybe you need another flip board to replace the Nano board you have. The code would be easy enough to port over to it and would remove the 5v to 3.3v issue.

    Mike
  • I am using a 3k ohm resistor in series with the arduino tx pin, The pin D3 is the softserial tx pin on the arduino. I rechecked and the rx=2 and tx = 3 and softwareserial serial_flip(rx,tx). This is my current sketch.
  • Your soft serial config looks wrong to me.
    const byte rxpin                        = 2 ;
     const byte txpin                        = 3 ;
     byte  directionFlag                     = 0 ;
     byte  westPinState                      = 0 ;
     byte  lastwestPinState                  = 0 ;
     byte  northPinState                     = 0 ;
     byte  lastnorthPinState                 = 0 ;
     byte  eastPinState                      = 0 ;
     byte  lasteastPinState                  = 0 ;
     byte  southPinState                     = 0 ;
     byte  lastsouthPinState                 = 0 ;
     byte  currentModeSwitchState            = 0 ;
     byte  lastModeSwitchState               = 0 ;
     byte  mode                              = 0 ;
     unsigned long  currentMillis            = 0 ;
     unsigned long  lastMillis               = 0 ;
     volatile byte RmotorPin                 = 0 ;
     volatile byte DmotorPin                 = 0 ;
     volatile byte RdirPin                   = 0 ;
     volatile byte DdirPin                   = 0 ;
     SoftwareSerial serial_flip(txpin,rxpin); <-- Is this backwards?
    

    Mike
  • look at my latest version of the program mar 09 a(3) zip.( My last post, There is no (txPin,rxPin) It reads serial_flip(rx,tx)
  • For some reason it didn't update my local copy. I see where you fixed it. That should work.

    Mike
  • But as before I receive no information from the rx pin to the serial monitor or to the motors and I do have some readable data to the rx pin from the arduino. perhaps it is time to convert the arduino sketch to a propeller but I may need some help.
  • Lets backup up a little here. We need to asses the serial problem first.

    Here is a simple program that will take data in from a serial pin and output it to the monitor on the flip. It will also take data from the flip and send it out the serial pin.
    #include "simpletools.h"
    #include "fdserial.h"
    
    fdserial *trm;
    terminal *dft;
    int c;
    
    int main()
    {
      simpleterm_close();
      
      trm = fdserial_open(31, 30, 0, 115200);
      dft = fdserial_open(0, 1, 0, 115200);
    
      pause(1000);
      dprint(trm, "Ready\n");
      
      while (1)
      {
        c = fdserial_rxCheck(trm);
        if (c > 0)
          fdserial_txChar(dft, c);
        c = fdserial_rxCheck(dft);
        if (c > 0)
          fdserial_txChar(trm, c);
      }
      fdserial_close(trm);
      fdserial_close(dft);
    }
    

    I don't have a nano board and there seems to be a couple of suppliers that are different out there but here is a diagram of one.

    If you tied pin 1 of the nano to pin 0 of the flip with the resistor it should mirror everything that you send from the nano.

    Also you are using pin 2 for receive and 3 for data out which would be connect to pin 0 on the flip. This should work.

    Also here is the Arduino program converted to Propeller. The Propeller does not support pull-up on pins but I believe you are using resistors on the switches so this is not needed anyway.

    Mike

  • bbrienbbrien Posts: 561
    edited 2020-05-12 06:37
    I have redone the system to two flips. I am trying the program you sent, will this go without using set_pwm or should change it to include set_pwm.
  • Not following, In which program are you referring to.

    Mike
  • The one for the flip to replace the arduino.
  • I see no pwm output in this progam so no don't see a need for it.

    Mike
  • How do I scale the values to the pwm values in the output of the mount.
  • You need to decide what ratio you want to output. Based on the motors you would say 25% power for low, 50% power for medium, and 75% for fast and 100% for all out move.

    If your PWM value is say 2000 microseconds then 1000 microseconds is 50% = 1000/2000.

    Here is a sample program to drive one motor at the different values:
    #include "simpletools.h"
    
    #define PWM1 15
    
    int i;
    
    
    int main()
    {
      pwm_start(2000); //<-- 2 milliseconds
    
      i = 4; //<-- set speed
      
      switch (i)
      {
        case 1:
          pwm_set(PWM1, 0, 500); //<-- .5 milliseconds 25%
          break;
        case 2:
          pwm_set(PWM1, 0, 1000); //<-- 1 milliseconds 50%
          break;
        case 3:
          pwm_set(PWM1, 0, 1500); //<-- 1.5 milliseconds 75%
          break;
        case 4:
          pwm_set(PWM1, 0, 2000); //<-- 2 milliseconds 100%
          break;
        default:
          break;
      }          
    
      while(1)
      {
        pause(1000);
        
      }  
    }
    

    Setting "i" to the 4 values generates the follow output shown on the scope. You can see at 1 the pulse is high only 25% of the time and 2 it is 50% and so on.

    This only works if you are using the UDN2993 chips.

    Mike
    1272 x 657 - 178K
    1272 x 657 - 184K
    1272 x 657 - 178K
    1272 x 657 - 174K
  • I will try to add or replace parts of the program(LX50 ver 3) but I still have no transmission to the second flip. I tried to use the serial monitor on the hand controller, I get "value1: 200, value2:0, value3: 0, value4:
    value1: 0, value2: 0, value3: 0, value4: 0" after I press the 'w' button on the hand controller. When I press the button the value 1 on the second line reads 200 and rest are zeros. I would like to print the input values of the mount flip.
  • I just looked at the last version of the flip program and there is print statements:
    print("%d,%d,%d,%d\n", Buffer[0], Buffer[1], Buffer[2], Buffer[3]);
    

    If you don't see anything then the serial isn't working.

    Mike
  • That's what I'm thinking, maybe I need a new Flip board. Thanks.
  • I am lost. about your post on5/13 4:10:37 I looked at the sketch but I don't know how it works with the serial input which uses a group of buffers. I need to convert the variables to the (pwm_set (600)) since that is the level I am using.
  • What about something similar to arduino's mapping function?
  • It's not that complicated. You want to drive the motors at some percentage of full power to make the telescope move at the rate you want. The problem is I don't know how fast the motors move at each of those percentages.

    So if we drive the UDN2993 at 50% what speed does that look like on the telescope. Is it too fast too slow or just right.

    On the flip you are using x/600 for the percentage. On the Arduino they are using x/255 for the percentages.

    You can see from the graph of the scope output that the pulse last x percent based on the value you pick.

    On the Arduino you picked these values

    Mode 1: --> R 100/255 = 39%
    Mode 1: --> D 75/255 = 29%
    Mode 2: --> R 150/255 = 59%
    Mode 2: --> D 100/255 = 39%
    Mode 3: --> R 200/255 = 78%
    Mode 3: --> D 150/255 = 59%
    Mode 4: --> R 250/255 = 98%
    Mode 4: --> D 200/255 = 78%

    If these percentage of full power are what you want then you just build the same percentages on the flip.

    To match the Arduino program you would start pwm at 255 for your value.

    pwm_start(255); //<-- repeat every 255 microseconds.

    Now the output from the pwm_set(x, 0, 100); //<-- 100/255 or 39% of full power.

    Now 0 - 255 doesn't give you a lot of choices for the percentage you want to drive the motors so if you want to use 600 then you just need to multiply to get the percentage you want.

    100 = 39% = .39 X 600 = 234
    75 = 29% = .29 X 600 = 174

    We can see that the difference is 255/600 = 2.35 which would be the factor you need to multiply by to get the same percentages.

    100 X 2.35 = 235 = 235/600 = 39%
    250 X 2.35 = 587 = 587/600 = 98%

    Now since we don't want to use floating point math just use 100 times the value or 235 and divide by 100 before you use it.

    100 X 235 = 23500/100 = 235.
    250 X 235 = 58750/100 = 587.

    Mike


  • You missed the second question about a mapping function or was the multiplication factors a mapping means.
  • Right, no mapping needed just multiple or use 255 for set frequency and it will work like the Arduino.

    Mike
  • I am running two flips now but the transmitted signal can be reset in the hand controller to higher values . the pwm_set can be used in the modes, right.
  • Please give an example of what you want to do with pwm_set.

    Is serial data now working?

    Mike
  • // mode 1
    case 1:
    switch(directionFlag)
    {
    // mode 1
    case 1:
    {
    pwm_set(RmotorPin, 0, 100);
    pwm_set(DmotorPin, 1, 0);
    break;

    No function yet and the mount 'flip' is a new one and I have a signal (high going low and back to high, one long then two small and another long) on the P1 pin which is tx pin.
  • So there is going to be a motor attached to the Arduino flip? You would need a pwm_start(255) at the beginning of the program to do that.

    Mike
  • No arduino's used in new set-up only propellers and no motors used on hand controller. hand controller talks to mount.
  • Great, then you don't need any pwm_set in the program.

    Mike
  • Concerning your post dated2020/05/10 05:29:24 can you identify these terms fdserial *trm ; terminal *dft; and simpleterm_close. and what do they do.
Sign In or Register to comment.