View unanswered posts | View active topics It is currently Thu Aug 28, 2014 9:44 am






Reply to topic  [ 5 posts ] 
SMUX and sonar 
Author Message
Rookie

Joined: Thu May 17, 2012 3:35 pm
Posts: 35
Post SMUX and sonar
Hi there!

I have a problem. Nothing happens with my Sonar and SMUX

Here the code:
Code:
#pragma config(Hubs,  S1, HTMotor,  HTServo,  none,     none)
#pragma config(Sensor, S2,     HTSMUX,         sensorI2CCustom)
#pragma config(Sensor, S4,     ultraL,         sensorSONAR)
#pragma config(Motor,  mtr_S1_C1_1,     motorD,        tmotorTetrix, openLoop)
#pragma config(Motor,  mtr_S1_C1_2,     motorE,        tmotorTetrix, openLoop)

#include "hitechnic-sensormux.h"
const tMUXSensor ultraR = msensor_S2_1;

task main()
{
  int abstand = 20;   
 
while (true)  //(true)
  {

     motor[motorD] = 30;
      motor[motorE] = -30;

       if(SensorValue(ultraR) < abstand)
       {
      motor[motorD] = -30;
      motor[motorE] = -30;
      wait1Msec(800);
      }

       if(SensorValue(ultraL) < abstand)
       {
      motor[motorD] = 30;
      motor[motorE] = 30;
      wait1Msec(800);
      }

      else
      motor[motorD] = 30;
      motor[motorE] = -30;
  }
}




Where is the problem?
Tanks


Tue Sep 10, 2013 9:02 am
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: SMUX and sonar
You did not mention what is your code's expected behavior. Nevertheless, there are many problems in this code. First, it seems you have two sonar sensors (left and right). The right one is on the sensor MUX. You have included Xander's hitechnic-sensormux.h but you did not include the lego-ultrasound.h driver. Please refer to Xander's sample code lego-ultrasound-SMUX-test1.c for an example on how to access the sonar sensors especially the one on the sensor MUX. Secondly, what if both the left and the right sonar sensors give you a value less than abstand? Your code will fight with itself trying to program the motors with both +30 and -30 with 800msec wait in between. Thirdly, there are no braces around the last two statement after else. I think you meant to execute both of these statements in the else clause. Again, since you did not mention the intention of the code, I don't know what your code is supposed to do.


Tue Sep 10, 2013 7:15 pm
Profile
Rookie

Joined: Thu May 17, 2012 3:35 pm
Posts: 35
Post Re: SMUX and sonar
Thank you

I made changes to the program and hope it´s much better to read and understand:

Code:
#pragma config(Hubs,  S1, HTMotor,  HTServo,  none,     none)
#pragma config(Sensor, S2,     HTSMUX,         sensorI2CCustom)
#pragma config(Sensor, S4,     ultraL,         sensorSONAR)
#pragma config(Motor,  mtr_S1_C1_1,     motorD,        tmotorTetrix, openLoop)
#pragma config(Motor,  mtr_S1_C1_2,     motorE,        tmotorTetrix, openLoop)

#include "hitechnic-sensormux.h"
#include "lego-ultrasound.h"

const tMUXSensor ULTRAR = msensor_S2_1;


task main()
{
  int speed = 0;
  int abstand = 30;
   
  while(true)                 
   {
   if ((SensorValue(ULTRAR) < abstand) || (SensorValue(ultraL) < abstand))     // Naeher als 30 cm..   
    {
    motor[motorD] = motor[motorE] = speed = 10; //Left forward --- right backward
    }
   
  else {
    motor[motorD] = 30;        //forward
    motor[motorE] = -30;     //forward
    wait1Msec(4000);
    }   
    nxtDisplayCenteredTextLine(0, "Sonar Wert R");                     
    nxtDisplayCenteredBigTextLine(2, "%d", SensorValue(ULTRAR));
    nxtDisplayCenteredTextLine(4, "Sonar Wert L");                     
    nxtDisplayCenteredBigTextLine(6, "%d", SensorValue(ultraL));
 
  }
}


But when I start, the motors run and on the display:

ultraL is o.k. but nothing else than 0 on ULTRAR

I don´t know the mistake. Please help

Thanks

Image


Wed Sep 11, 2013 1:28 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: SMUX and sonar
Did you look at Xander's sample code? It showed you how to read the sonar sensor connected to a sensor MUX. I copied and pasted the sample code below.
Code:
#pragma config(Sensor, S1,     HTSMUX,              sensorLowSpeed)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

/*
 * $Id: lego-ultrasound-SMUX-test1.c 133 2013-03-10 15:15:38Z xander $
 */

/**
 * lego-ultrasound.h provides an API for the Lego US sensor.  This program
 * demonstrates how to use that API.
 *
 * Changelog:
 * - 0.1: Initial release
 * - 0.2: More comments
 *
 * License: You may use this code as you wish, provided you give credit where it's due.
 *
 * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER.

 * Xander Soldaat (xander_at_botbench.com)
 * 25 November 2009
 * version 0.2
 */

#include "drivers/hitechnic-sensormux.h"
#include "drivers/lego-ultrasound.h"

// The sensor is connected to the first port
// of the SMUX which is connected to the NXT port S1.
// To access that sensor, we must use msensor_S1_1.  If the sensor
// were connected to 3rd port of the SMUX connected to the NXT port S4,
// we would use msensor_S4_3

// Give the sensor a nice easy to use name
const tMUXSensor LEGOUS = msensor_S1_1;

task main () {
  int dist = 0;

  nxtDisplayCenteredTextLine(0, "Lego");
  nxtDisplayCenteredBigTextLine(1, "US");
  nxtDisplayCenteredTextLine(3, "SMUX Test");
  nxtDisplayCenteredTextLine(5, "Connect SMUX to");
  nxtDisplayCenteredTextLine(6, "S1 and US sensor");
  nxtDisplayCenteredTextLine(7, "to SMUX Port 1");
  wait1Msec(2000);

  eraseDisplay();
  nxtDisplayTextLine(0, "Lego US Sensor");

  while(true) {
    // Read the current distance detected.
    dist = USreadDist(LEGOUS);

    // display the info from the sensor
    nxtDisplayTextLine(3, "Dist:  %3d cm", dist);
    wait10Msec(50);
  }
}

/*
 * $Id: lego-ultrasound-SMUX-test1.c 133 2013-03-10 15:15:38Z xander $
 */


Wed Sep 11, 2013 6:54 pm
Profile
Rookie

Joined: Thu May 17, 2012 3:35 pm
Posts: 35
Post Re: SMUX and sonar
Hi

Yes I´ve seen Xander´s sample code. I found the mistake. It´s not Sensor Value.....

Code:
#pragma config(Hubs,  S1, HTMotor,  HTServo,  none,     none)
#pragma config(Sensor, S2,     HTSMUX,         sensorI2CCustom)
#pragma config(Sensor, S4,     LEGOUSL,         sensorSONAR)
#pragma config(Motor,  mtr_S1_C1_1,     motorD,        tmotorTetrix, openLoop)
#pragma config(Motor,  mtr_S1_C1_2,     motorE,        tmotorTetrix, openLoop)

#include "hitechnic-sensormux.h"
#include "lego-ultrasound.h"

const tMUXSensor LEGOUS = msensor_S2_1;

task main()
{
  int speed = 0;
  int dist = 0;

  while(true)
   {
    dist = USreadDist(LEGOUS);

   if ((dist < 25)  || (SensorValue(LEGOUSL) < 25))  //   25 cm..
    {
    motor[motorD] = motor[motorE] = speed = 20;
    }
  else {
    motor[motorD] = -30;
    motor[motorE] = -30;
     }
     }
}


Thanks a lot


Thu Sep 12, 2013 8:52 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 5 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.